Skip to content

Commit

Permalink
v5.30: fix TerminalTextAlign, work on HttpServer and PasswordFile
Browse files Browse the repository at this point in the history
  • Loading branch information
ColumPaget committed Oct 29, 2024
1 parent bb197e2 commit 3e25743
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 107 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v5.30 (2024-10-29)
* Fix crash if TerminalTextAlign has NULL for the Terminal
* Added HttpGet and HttpPost functions
* Improvements to httpserver authentication.
* Add 'extra' field to passwords file to allow storing data against a user.

v5.29 (2024-10-29)
* much more seccomp work
* add 'data' option to pty config (PtyParseConfig) so if a pipe or pty is going to be used for raw data transfer, use FLUSH_ALWAYS not FLUSH_LINE
Expand Down
52 changes: 26 additions & 26 deletions Encodings.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ char *EncodeQuoted(char *Return, const char *Input, int len, char QuoteChar)
{
if ( (*ptr < 32) || (*ptr > 127) || (*ptr == '=') )
{
snprintf(Hex, 3, "%02x", (*ptr) & 0xFF);
Return=MCatStr(Return, "=", Hex, NULL);
snprintf(Hex, 3, "%02x", (*ptr) & 0xFF);
Return=MCatStr(Return, "=", Hex, NULL);
}
else Return=AddCharToStr(Return, *ptr);
}
Expand Down Expand Up @@ -43,16 +43,16 @@ int DecodeQuoted(char **Return, const char *Text, char QuoteChar)
{
strncpy(Hex, ptr, 2);
*Return=AddCharToBuffer(*Return, len, strtol(Hex, NULL, 16));
len++;
len++;
ptr++;
if (*ptr=='\0') break;
}
}
else
{
*Return=AddCharToBuffer(*Return, len, *ptr);
len++;
}
else
{
*Return=AddCharToBuffer(*Return, len, *ptr);
len++;
}
}

return(len);
Expand All @@ -63,18 +63,18 @@ int DecodeQuoted(char **Return, const char *Text, char QuoteChar)
char *EncodeYenc(char *Return, const char *Input, int len, char QuoteChar)
{
const char *ptr;
char echar;
char echar;

for (ptr=Input; ptr < (Input + len); ptr++)
{
//shift the character up some bytes, because the null byte is a common occurance
//in binary files, and we don't want to escape it, increasing the size of the
//file. We'll escape some less common byte in it's place
echar = (*ptr + 42) % 256; //of course it's 42
//shift the character up some bytes, because the null byte is a common occurance
//in binary files, and we don't want to escape it, increasing the size of the
//file. We'll escape some less common byte in it's place
echar = (*ptr + 42) % 256; //of course it's 42
if ( (echar==0) || (echar=='\r') || (echar=='\n') || (echar==QuoteChar))
{
Return=AddCharToStr(Return, QuoteChar);
Return=AddCharToStr(Return, (echar + 62) % 256);
Return=AddCharToStr(Return, QuoteChar);
Return=AddCharToStr(Return, (echar + 62) % 256);
}
else Return=AddCharToStr(Return, echar);
}
Expand All @@ -94,12 +94,12 @@ int DecodeYenc(char **Return, const char *Text, char QuoteChar)
{
ptr++;
if (*ptr=='\0') break;
echar=*ptr - 62;
echar=*ptr - 62;
}
else echar=*ptr;
*Return=AddCharToBuffer(*Return, len, echar - 42);
len++;

*Return=AddCharToBuffer(*Return, len, echar - 42);
len++;
}

return(len);
Expand Down Expand Up @@ -231,11 +231,11 @@ int EncodingParse(const char *Str)
else if (strcasecmp(Str,"xxenc")==0) Encode=ENCODE_XXENC;
break;

case 'y':
case 'Y':
case 'y':
case 'Y':
if (strcasecmp(Str,"yenc")==0) Encode=ENCODE_YENCODE;
else if (strcasecmp(Str,"yencode")==0) Encode=ENCODE_YENCODE;
break;
break;

case 'z':
case 'Z':
Expand Down Expand Up @@ -309,12 +309,12 @@ char *EncodeBytes(char *Buffer, const char *Bytes, int len, int Encoding)
switch (Encoding)
{
case ENCODE_QUOTED_MIME:
RetStr=EncodeQuoted(RetStr, Bytes, len, '=');
break;
RetStr=EncodeQuoted(RetStr, Bytes, len, '=');
break;

case ENCODE_YENCODE:
RetStr=EncodeYenc(RetStr, Bytes, len, '=');
break;
RetStr=EncodeYenc(RetStr, Bytes, len, '=');
break;

case ENCODE_BASE32:
RetStr=base32encode(RetStr, Bytes, len, BASE32_RFC4648_CHARS, '=');
Expand Down
12 changes: 11 additions & 1 deletion Errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ Errors are injected into the list with RaiseError like this:
ListUsefulSetValue("Error:notty","Y")
If the ERRFLAG_SYSLOG flag is passed, like this:
RaiseError(ERRFLAG_ERRNO|ERRFLAG_SYSLOG, "ServiceConnect", "host=%s port=%d",Host, Port);
Then the error will be logged to the system logger. Alternatively if the LibUseful value 'Error:Syslog' is set, like this:
ListUsefulSetValue("Error:Syslog","Y")
then all error and debug messages are sent to the system logger via syslog.
If the flag ERRFLAG_DEBUG is set then RaiseError won't print anything out by default. So this line:
RaiseError(ERRFLAG_DEBUG, "ServiceConnect", "host=%s port=%d",Host, Port);
Expand All @@ -70,7 +80,6 @@ Errors are injected into the list with RaiseError like this:
If the libUseful internal value 'Error:Silent' is set true/yes/y with ListUsefulSetValue then RaiseError never
prints errors to stderr, regardless of any other config.
If the value 'Error:Syslog' is set true/yes/y then error and debug messages are sent to the system logger via syslog
*/


Expand All @@ -90,6 +99,7 @@ extern "C" {
#endif


//call 'RaiseError' to add an error into the error sytem
#define RaiseError(flags, where, fmt, ...) InternalRaiseError(flags, where, __FILE__, __LINE__, fmt, ##__VA_ARGS__)

//clears the list of errors
Expand Down
34 changes: 25 additions & 9 deletions Http.c
Original file line number Diff line number Diff line change
Expand Up @@ -1244,21 +1244,37 @@ STREAM *HTTPWithConfig(const char *URL, const char *Config)




int HTTPCopyToSTREAM(STREAM *Con, STREAM *S)
{
const char *ptr;
size_t size=0;

//do not check response code of HTTP server streams (strictly speaking STREAM_TYPE_HTTP_ACCEPT)
if (S->Type == STREAM_TYPE_HTTP)
{
ptr=STREAMGetValue(Con, "HTTP:ResponseCode");
if ((! ptr) || (*ptr !='2'))
{
STREAMClose(Con);
return(-1);
}
}

ptr=STREAMGetValue(Con, "HTTP:Content-Length");
if (StrValid(ptr)) size=strtol(ptr, NULL, 10);

return(STREAMSendFile(Con, S, size, SENDFILE_LOOP));
}


int HTTPDownload(char *URL, STREAM *S)
{
STREAM *Con;
const char *ptr;

Con=HTTPGet(URL);
if (! Con) return(-1);

ptr=STREAMGetValue(Con, "HTTP:ResponseCode");
if ((! ptr) || (*ptr !='2'))
{
STREAMClose(Con);
return(-1);
}
return(STREAMSendFile(Con, S, 0, SENDFILE_LOOP));
return(HTTPCopyToSTREAM(Con, S));
}


Expand Down
48 changes: 39 additions & 9 deletions HttpServer.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ static void HTTPServerSetValue(STREAM *S, const char *Name, const char *Value)

//do not allow these to be set, as they will
//overwrite the true HTTP method and url
if (strcmp(Name, "HTTP-Version")==0) return;
if (strcmp(Name, "Method")==0) return;
if (strcmp(Name, "URL")==0) return;

Expand Down Expand Up @@ -48,7 +49,7 @@ void HTTPServerParseClientCookies(ListNode *Vars, const char *Str)

void HTTPServerParseAuthorization(ListNode *Vars, const char *Str)
{
char *Token=NULL;
char *Token=NULL, *Tempstr=NULL, *User=NULL, *Password=NULL;
const char *ptr;

ptr=Str;
Expand All @@ -58,15 +59,20 @@ void HTTPServerParseAuthorization(ListNode *Vars, const char *Str)
if (strcasecmp(Token, "basic")==0)
{
ptr=GetToken(ptr, "\\S", &Token, 0);
SetVar(Vars, "Auth:Basic", Token);
HTTPDecodeBasicAuth(Token, &User, &Password);
SetVar(Vars, "AUTH:User", User);
SetVar(Vars, "AUTH:Password", Password);
}
else if (strcasecmp(Token, "bearer")==0)
{
ptr=GetToken(ptr, "\\S", &Token, 0);
SetVar(Vars, "Auth:Bearer", Token);
SetVar(Vars, "AUTH:Bearer", Token);
}

Destroy(Tempstr);
Destroy(Token);
Destroy(User);
Destroy(Password);
}


Expand All @@ -79,7 +85,9 @@ void HTTPServerParseClientHeaders(STREAM *S)
StripTrailingWhitespace(Tempstr);
ptr=GetToken(Tempstr, "\\S", &Token, 0);
STREAMSetValue(S, "HTTP:Method", Token);
STREAMSetValue(S, "HTTP:URL", ptr);
ptr=GetToken(ptr, "\\S", &Token, 0);
STREAMSetValue(S, "HTTP:URL", Token);
STREAMSetValue(S, "HTTP:HTTP-Version", ptr);

Tempstr=STREAMReadLine(Tempstr, S);
while (Tempstr)
Expand All @@ -89,10 +97,13 @@ void HTTPServerParseClientHeaders(STREAM *S)

ptr=GetToken(Tempstr, ":", &Token, 0);
StripTrailingWhitespace(Token);
if (strcasecmp(Token, "Cookie:")==0) HTTPServerParseClientCookies(S->Values, ptr);
while (isspace(*ptr)) ptr++;

if (strcasecmp(Token, "Cookie")==0) HTTPServerParseClientCookies(S->Values, ptr);
else if (strcasecmp(Token, "Authorization")==0) HTTPServerParseAuthorization(S->Values, ptr);
else if (strcasecmp(Token, "Sec-Websocket-Key") == 0) STREAMSetValue(S, "WEBSOCKET:KEY", ptr);
else if (strcasecmp(Token, "Sec-Websocket-Protocol") == 0) STREAMSetValue(S, "WEBSOCKET:PROTOCOL", ptr);
//HTTPServerSetValue will ignore Method, URL and HTTP-Version so they can't be overridden by extra headers
else HTTPServerSetValue(S, Token, ptr);

Tempstr=STREAMReadLine(Tempstr, S);
Expand All @@ -106,6 +117,7 @@ void HTTPServerParseClientHeaders(STREAM *S)
void HTTPServerAccept(STREAM *S)
{
HTTPServerParseClientHeaders(S);
STREAMAuth(S);
}


Expand Down Expand Up @@ -145,18 +157,36 @@ void HTTPServerSendHeaders(STREAM *S, int ResponseCode, const char *ResponseText
Destroy(Hash);
}


int HTTPServerSendDocument(STREAM *S, const char *Bytes, int Length, const char *ContentType, const char *Headers)
int HTTPServerSendResponse(STREAM *S, const char *ResponseCode, const char *ResponseReason, const char *Content, int Length, const char *ContentType, const char *Headers)
{
char *Tempstr=NULL;
int result;

Tempstr=FormatStr(Tempstr, "Content-Length=%d ", Length);
if (StrValid(ContentType)) Tempstr=MCatStr(Tempstr, "Content-Type=", ContentType, " ", Headers, NULL);
HTTPServerSendHeaders(S, 200, "OKAY", Tempstr);
result=STREAMWriteBytes(S, Bytes, Length);
HTTPServerSendHeaders(S, atoi(ResponseCode), ResponseReason, Tempstr);
result=STREAMWriteBytes(S, Content, Length);

Destroy(Tempstr);
return(result);
}


int HTTPServerSendDocument(STREAM *S, const char *Content, int Length, const char *ContentType, const char *Headers)
{
return(HTTPServerSendResponse(S, "200", "OKAY", Content, Length, ContentType, Headers));
}

int HTTPServerSendStatus(STREAM *S, const char *StatusCode, const char *StatusReason)
{
char *Tempstr=NULL;
int result;

Tempstr=FormatStr(Tempstr, "<html><body><h1>%s - %s</h1></body></html>\r\n", StatusCode, StatusReason);
result=HTTPServerSendResponse(S, StatusCode, StatusReason, Tempstr, StrLen(Tempstr), "text/html", "");

Destroy(Tempstr);

return(result);
}

Expand Down
3 changes: 3 additions & 0 deletions HttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ void HTTPServerParseClientHeaders(STREAM *S);
void HTTPServerSendHeaders(STREAM *S, int ResponseCode, const char *ResponseText, const char *Headers);
void HTTPServerAccept(STREAM *S);

int HTTPServerSendResponse(STREAM *S, const char *ResponseCode, const char *ResponseReason, const char *Content, int Length, const char *ContentType, const char *Headers);
int HTTPServerSendDocument(STREAM *S, const char *Content, int Length, const char *ContentType, const char *Headers);
int HTTPServerSendStatus(STREAM *S, const char *ErrorCode, const char *ErrorReason);
int HTTPServerSendDocument(STREAM *S, const char *Bytes, int Length, const char *ContentType, const char *Headers);
int HTTPServerSendFile(STREAM *S, const char *Path, const char *ContentType, const char *Headers);

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CC = gcc
VERSION = 5.29
VERSION = 5.30
MAJOR=5
LIBFILE=libUseful.so.$(VERSION)
SONAME=libUseful.so.$(MAJOR)
Expand Down Expand Up @@ -270,7 +270,7 @@ LibSettings.o: LibSettings.h LibSettings.c
$(CC) $(FLAGS) -c LibSettings.c

clean:
-rm -f *.o *.so *.so.* *.a *.orig .*.swp
-rm -f *.o *.so *.so.* *.a *.orig .*.swp *~
-rm config.log config.status
-rm -r autom4te.cache config.cache
-$(MAKE) clean -C examples
Expand Down
4 changes: 2 additions & 2 deletions Makefile.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CC = @CC@
VERSION = 5.29
VERSION = 5.30
MAJOR=5
LIBFILE=libUseful.so.$(VERSION)
SONAME=libUseful.so.$(MAJOR)
Expand Down Expand Up @@ -270,7 +270,7 @@ LibSettings.o: LibSettings.h LibSettings.c
$(CC) $(FLAGS) -c LibSettings.c

clean:
-rm -f *.o *.so *.so.* *.a *.orig .*.swp
-rm -f *.o *.so *.so.* *.a *.orig .*.swp *~
-rm config.log config.status
-rm -r autom4te.cache config.cache
-$(MAKE) clean -C examples
Expand Down
Loading

0 comments on commit 3e25743

Please sign in to comment.