From 424c0bd3e14d3ddda6c2d945b6465d51c462c76b Mon Sep 17 00:00:00 2001 From: Dave Hibberd Date: Tue, 5 Nov 2024 21:03:15 +0000 Subject: New upstream version 6.0.24.49 --- HTTPcode.c | 230 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 200 insertions(+), 30 deletions(-) (limited to 'HTTPcode.c') diff --git a/HTTPcode.c b/HTTPcode.c index 1e4502c..bd96157 100644 --- a/HTTPcode.c +++ b/HTTPcode.c @@ -106,12 +106,13 @@ char * strlop(char * buf, char delim); VOID sendandcheck(SOCKET sock, const char * Buffer, int Len); int CompareNode(const void *a, const void *b); int CompareAlias(const void *a, const void *b); -void ProcessMailHTTPMessage(struct HTTPConnectionInfo * Session, char * Method, char * URL, char * input, char * Reply, int * RLen, int InputLen); +void ProcessMailHTTPMessage(struct HTTPConnectionInfo * Session, char * Method, char * URL, char * input, char * Reply, int * RLen, int InputLen, char * Token); void ProcessChatHTTPMessage(struct HTTPConnectionInfo * Session, char * Method, char * URL, char * input, char * Reply, int * RLen); struct PORTCONTROL * APIENTRY GetPortTableEntryFromSlot(int portslot); int SetupNodeMenu(char * Buff, int SYSOP); int StatusProc(char * Buff); int ProcessMailSignon(struct TCPINFO * TCP, char * MsgPtr, char * Appl, char * Reply, struct HTTPConnectionInfo ** Session, BOOL WebMail, int LOCAL); +int ProcessMailAPISignon(struct TCPINFO * TCP, char * MsgPtr, char * Appl, char * Reply, struct HTTPConnectionInfo ** Session, BOOL WebMail, int LOCAL); int ProcessChatSignon(struct TCPINFO * TCP, char * MsgPtr, char * Appl, char * Reply, struct HTTPConnectionInfo ** Session, int LOCAL); VOID APRSProcessHTTPMessage(SOCKET sock, char * MsgPtr, BOOL LOCAL, BOOL COOKIE); @@ -1166,6 +1167,9 @@ int SendMessageFile(SOCKET sock, char * FN, BOOL OnlyifExists, int allowDeflate) if (_stricmp(ptr, "js") == 0) strcpy(Type, "Content-Type: text/javascript\r\n"); + + if (_stricmp(ptr, "css") == 0) + strcpy(Type, "Content-Type: text/css\r\n"); if (_stricmp(ptr, "pdf") == 0) strcpy(Type, "Content-Type: application/pdf\r\n"); @@ -1180,7 +1184,8 @@ int SendMessageFile(SOCKET sock, char * FN, BOOL OnlyifExists, int allowDeflate) Compressed = MsgBytes; } - if (_stricmp(ptr, "jpg") == 0 || _stricmp(ptr, "jpeg") == 0 || _stricmp(ptr, "png") == 0 || _stricmp(ptr, "gif") == 0 || _stricmp(ptr, "ico") == 0) + if (_stricmp(ptr, "jpg") == 0 || _stricmp(ptr, "jpeg") == 0 || _stricmp(ptr, "png") == 0 || + _stricmp(ptr, "gif") == 0 || _stricmp(ptr, "bmp") == 0 || _stricmp(ptr, "ico") == 0) strcpy(Type, "Content-Type: image\r\n"); HeaderLen = sprintf(Header, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n" @@ -1793,37 +1798,60 @@ int InnerProcessHTTPMessage(struct ConnectionInfo * conn) if (_memicmp(Context, "/api/", 5) == 0 || _stricmp(Context, "/api") == 0) { char * Compressed; - ReplyLen = APIProcessHTTPMessage(_REPLYBUFFER, Method, Context, MsgPtr, LOCAL, COOKIE); - - if (memcmp(_REPLYBUFFER, "HTTP", 4) == 0) + + // if for mail api process signon here and rearrange url from + // api/v1/mail to mail/api/v1 so it goes to mail handler later + + if (_memicmp(Context, "/api/v1/mail/", 13) == 0) { - // Full Message - just send it + memcpy(MsgPtr, "GET /mail/api/v1/", 17); + + if (memcmp(&Context[13], "login", 5) == 0) + { + ReplyLen = ProcessMailAPISignon(TCP, MsgPtr, "M", Reply, &Session, FALSE, LOCAL); + memcpy(MsgPtr, "GET /mail/api/v1/", 17); - sendandcheck(sock, _REPLYBUFFER, ReplyLen); + if (ReplyLen) // Error message + goto Returnit; + } - return 0; + memcpy(Context, "/mail/api/v1/", 13); + goto doHeader; } - - if (allowDeflate) - Compressed = Compressit(_REPLYBUFFER, ReplyLen, &ReplyLen); else - Compressed = _REPLYBUFFER; + { + ReplyLen = APIProcessHTTPMessage(_REPLYBUFFER, Method, Context, MsgPtr, LOCAL, COOKIE); + + if (memcmp(_REPLYBUFFER, "HTTP", 4) == 0) + { + // Full Message - just send it - HeaderLen = sprintf(Header, "HTTP/1.1 200 OK\r\n" - "Content-Length: %d\r\n" - "Content-Type: application/json\r\n" - "Connection: close\r\n" - "%s\r\n", ReplyLen, Encoding); + sendandcheck(sock, _REPLYBUFFER, ReplyLen); - sendandcheck(sock, Header, HeaderLen); - sendandcheck(sock, Compressed, ReplyLen); + return 0; + } - if (allowDeflate) - free (Compressed); + if (allowDeflate) + Compressed = Compressit(_REPLYBUFFER, ReplyLen, &ReplyLen); + else + Compressed = _REPLYBUFFER; - return 0; - } + HeaderLen = sprintf(Header, "HTTP/1.1 200 OK\r\n" + "Content-Length: %d\r\n" + "Content-Type: application/json\r\n" + "Connection: close\r\n" + "Access-Control-Allow-Origin: *\r\n" + "%s\r\n", ReplyLen, Encoding); + + sendandcheck(sock, Header, HeaderLen); + sendandcheck(sock, Compressed, ReplyLen); + if (allowDeflate) + free (Compressed); + + return 0; + } + } // APRS process internally @@ -2095,11 +2123,7 @@ Returnit: Compressed = Reply; if (NodeURL && _memicmp(NodeURL, "/mail/api/", 10) == 0) - HeaderLen = sprintf(Header, "HTTP/1.1 200 OK\r\n" - "Content-Length: %d\r\n" - "Content-Type: application/json\r\n" - "Connection: close\r\n" - "%s\r\n", ReplyLen, Encoding); + HeaderLen = sprintf(Header, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: application/json\r\nConnection: close\r\n%s\r\n", ReplyLen, Encoding); else HeaderLen = sprintf(Header, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: text/html\r\n%s\r\n", ReplyLen, Encoding); @@ -2122,15 +2146,82 @@ doHeader: char _REPLYBUFFER[250000]; struct HTTPConnectionInfo Dummy = {0}; int Sent, Loops = 0; + char token[16] = ""; + + // look for auth header + + const char * auth_header = "Authorization: Bearer "; + char * token_begin = strstr(MsgPtr, auth_header); + int Flags = 0, n; + + char * Tok; + char * param; + + if (token_begin) + { + // Using Auth Header + + // Extract the token from the request (assuming it's present in the request headers) + + token_begin += strlen(auth_header); // Move to the beginning of the token + strncpy(token, token_begin, 13); + token[13] = '\0'; // Null-terminate the token + } ReplyLen = 0; if (Session == 0) Session = &Dummy; - Session->TNC = (void *)LOCAL; // TNC only used for Web Terminal Sessions + if (LOCAL) + Session->TNC = (void *)1; // TNC only used for Web Terminal Sessions + else + Session->TNC = (void *)0; - ProcessMailHTTPMessage(Session, Method, Context, MsgPtr, _REPLYBUFFER, &ReplyLen, MsgLen); + ProcessMailHTTPMessage(Session, Method, Context, MsgPtr, _REPLYBUFFER, &ReplyLen, MsgLen, token); + + if (Context && _memicmp(Context, "/mail/api/", 10) == 0) + { + if (memcmp(_REPLYBUFFER, "HTTP", 4) == 0) + { + // Full Header provided by appl - just send it + + // Send may block + + Sent = send(sock, _REPLYBUFFER, ReplyLen, 0); + + while (Sent != ReplyLen && Loops++ < 3000) // 100 secs max + { + // Debugprintf("%d out of %d sent %d Loops", Sent, InputLen, Loops); + + if (Sent > 0) // something sent + { + InputLen -= Sent; + memmove(_REPLYBUFFER, &_REPLYBUFFER[Sent], ReplyLen); + } + + Sleep(30); + Sent = send(sock, _REPLYBUFFER, ReplyLen, 0); + } + return 0; + } + + // compress if allowed + + if (allowDeflate) + Compressed = Compressit(_REPLYBUFFER, ReplyLen, &ReplyLen); + else + Compressed = _REPLYBUFFER; + + HeaderLen = sprintf(Header, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: application/json\r\nConnection: close\r\n%s\r\n", ReplyLen, Encoding); + sendandcheck(sock, Header, HeaderLen); + sendandcheck(sock, Compressed, ReplyLen); + + if (allowDeflate) + free (Compressed); + + return 0; + } if (memcmp(_REPLYBUFFER, "HTTP", 4) == 0) { @@ -2156,11 +2247,16 @@ doHeader: return 0; } + if (Context && _memicmp(Context, "/mail/api/", 10) != 0) + { + // Add tail strcpy(&_REPLYBUFFER[ReplyLen], Tail); ReplyLen += strlen(Tail); + } + // compress if allowed if (allowDeflate) @@ -2250,6 +2346,8 @@ doHeader: // Pass to MailChat if active + NodeURL = Context; + if ((_memicmp(Context, "/MAIL/", 6) == 0) || (_memicmp(Context, "/WebMail", 8) == 0)) { // If for Mail, Pass to Mail Server via Named Pipe @@ -4183,6 +4281,78 @@ int ProcessNodeSignon(SOCKET sock, struct TCPINFO * TCP, char * MsgPtr, char * A return ReplyLen; } +int ProcessMailAPISignon(struct TCPINFO * TCP, char * MsgPtr, char * Appl, char * Reply, struct HTTPConnectionInfo ** Session, BOOL WebMail, int LOCAL) +{ + int ReplyLen = 0; + char * input = strstr(MsgPtr, "\r\n\r\n"); // End of headers + char * user, * password, * Key; + struct HTTPConnectionInfo * NewSession; + int i; + struct UserRec * USER; + + if (strchr(MsgPtr, '?')) + { + // Check Password + + user = strlop(MsgPtr, '?'); + password = strlop(user, '&'); + strlop(password, ' '); + + for (i = 0; i < TCP->NumberofUsers; i++) + { + USER = TCP->UserRecPtr[i]; + + if (user && _stricmp(user, USER->UserName) == 0) + { + if ((strcmp(password, USER->Password) == 0) && (USER->Secure || WebMail)) + { + // ok + + NewSession = AllocateSession(Appl[0], 'M'); + + *Session = NewSession; + + if (NewSession) + { + ReplyLen = 0; + strcpy(NewSession->Callsign, USER->Callsign); + } + else + { + ReplyLen = SetupNodeMenu(Reply, LOCAL); + ReplyLen += sprintf(&Reply[ReplyLen], "%s", BusyError); + } + return ReplyLen; + + } + } + } + + // Pass failed attempt to BBS code so it can try a bbs user login + + // Need to put url back together + + if (user && user[0] && password && password[0]) + { + sprintf(MsgPtr, "%s?%s&%s", MsgPtr, user, password); + } + } + + NewSession = AllocateSession(Appl[0], 'M'); + + *Session = NewSession; + + if (NewSession) + ReplyLen = 0; + else + { + ReplyLen = SetupNodeMenu(Reply, LOCAL); + ReplyLen += sprintf(&Reply[ReplyLen], "%s", BusyError); + } + + return ReplyLen; +} + -- cgit v1.2.3