[0001] [0002] [0003] [0004] [0005] [0006] [0007] [0008] [0009] [0010] [0011] [0012] [0013] [0014] [0015] [0016] [0017] [0018] [0019] [0020] [0021] [0022] [0023] [0024] [0025] [0026] [0027] [0028] [0029] [0030] [0031] [0032] [0033] [0034] [0035] [0036] [0037] [0038] [0039] [0040] [0041] [0042] [0043] [0044] [0045] [0046] [0047] [0048] [0049] [0050] [0051] [0052] [0053] [0054] [0055] [0056] [0057] [0058] [0059] [0060] [0061] [0062] [0063] [0064] [0065] [0066] [0067] [0068] [0069] [0070] [0071] [0072] [0073] [0074] [0075] [0076] [0077] [0078] [0079] [0080] [0081] [0082] [0083] [0084] [0085] [0086] [0087] [0088] [0089] [0090] [0091] [0092] [0093] [0094] [0095] [0096] [0097] [0098] [0099] [0100] [0101] [0102] [0103] [0104] [0105] [0106] [0107] [0108] [0109] [0110] [0111] [0112] [0113] [0114] [0115] [0116] [0117] [0118] [0119] [0120] [0121] [0122] [0123] [0124] [0125] [0126] [0127] [0128] [0129] [0130] [0131] [0132] [0133] [0134] [0135] [0136] [0137] [0138] [0139] [0140] [0141] [0142] [0143] [0144] [0145] [0146] [0147] [0148] [0149] [0150] [0151] [0152] [0153] [0154] [0155] [0156] [0157] [0158] [0159] [0160] [0161] [0162] [0163] [0164] [0165] [0166] [0167] [0168] [0169] [0170] [0171] [0172] [0173] [0174] [0175] [0176] [0177] [0178] [0179] [0180] [0181] [0182] [0183] [0184] [0185] [0186] [0187] [0188] [0189] [0190] [0191] [0192] [0193] [0194] [0195] [0196] [0197] [0198] [0199] [0200] [0201] [0202] [0203] [0204] [0205] [0206] [0207] [0208] [0209] [0210] [0211] [0212] [0213] [0214] [0215] [0216] [0217] [0218] [0219] [0220] [0221] [0222] [0223] [0224] [0225] [0226] [0227] [0228] [0229] [0230] [0231] [0232] [0233] [0234] [0235] [0236] [0237] [0238] [0239] [0240] [0241] [0242] [0243] [0244] [0245] [0246] [0247] [0248] [0249] [0250] [0251] [0252] [0253] [0254] [0255] [0256] [0257] [0258] [0259] [0260] [0261] [0262] [0263] [0264] [0265] [0266] [0267] [0268] [0269] [0270] [0271] [0272] [0273] [0274] [0275] [0276] [0277] [0278] [0279] [0280] [0281] [0282] [0283] [0284] [0285] [0286] [0287] [0288] [0289] [0290] [0291] [0292] [0293] [0294] [0295] [0296] [0297] [0298] [0299] [0300] [0301] [0302] [0303] [0304] [0305] [0306] [0307] [0308] [0309] [0310] [0311] [0312] [0313] [0314] [0315] [0316] [0317] [0318] [0319] [0320] [0321] [0322] [0323] [0324] [0325] [0326] [0327] [0328] [0329] [0330] [0331] [0332] [0333] [0334] [0335] [0336] [0337] [0338] [0339] [0340] [0341] [0342] [0343] [0344] [0345] [0346] [0347] [0348] [0349] [0350] [0351] [0352] [0353] [0354] [0355] [0356] [0357] [0358] [0359] [0360] [0361] [0362] [0363] [0364] [0365] [0366] [0367] [0368] [0369] [0370] [0371] [0372] [0373] [0374] [0375] [0376] [0377] [0378] [0379] [0380] [0381] [0382] [0383] [0384] [0385] [0386] [0387] [0388] [0389] [0390] [0391] [0392] [0393] [0394] [0395] [0396] [0397] [0398] [0399] [0400] [0401] [0402] [0403] [0404] [0405] [0406] [0407] [0408] [0409] [0410] [0411] [0412] [0413] [0414] [0415] [0416] [0417] [0418] [0419] [0420] [0421] [0422] [0423] [0424] [0425] [0426] [0427] [0428] [0429] [0430] [0431] [0432] [0433] [0434] [0435] [0436] [0437] [0438] [0439] [0440] [0441] [0442] [0443] [0444] [0445] [0446] [0447] [0448] [0449] [0450] [0451] [0452] [0453] [0454] [0455] [0456] [0457] [0458] [0459] [0460] [0461] [0462] [0463] [0464] [0465] [0466] [0467] [0468] [0469] [0470] [0471] [0472] [0473] [0474] [0475] [0476] [0477] [0478] [0479] [0480] [0481] [0482] [0483] [0484] [0485] [0486] [0487] [0488] [0489] [0490] [0491] [0492] [0493] [0494] [0495] [0496] [0497] [0498] [0499] [0500] [0501] [0502] [0503] [0504] [0505] [0506] [0507] [0508] [0509] [0510] [0511] [0512] [0513] [0514] [0515] [0516] [0517] [0518] [0519] [0520] [0521] [0522] [0523] [0524] [0525] [0526] [0527] [0528] [0529] [0530] [0531] [0532] [0533] [0534] [0535] [0536] [0537] [0538] [0539] [0540] [0541] [0542]
/*****************************************************************************/ /* ProxyRework.c The proxy rework facility will modify a target string to a replacement string in the request header (e.g. Host:), the response header (e.g. set-cookie:), and in the response body. Rework will be applied to HTML and CSS responses. These are simple string matches. CONFIGURATION ------------- The proxy rework facility must be enabled for a service by setting a maximum size for the HTML response body to be reworked, in kB. # WASD_CONFIG_SERVICE [[*.1924]] [ServiceReworkMax] 128 Specific paths must then be SET in WASD_CONFIG_MAP to have proxy requests reworked. # WASD_CONFIG_MAP [[*:1924]] set * proxy=rework=192.168.1.3=192.168.1.2 VERSION HISTORY --------------- 26-FEB-2021 MGD initial */ /*****************************************************************************/ #ifdef WASD_VMS_V7 #undef _VMS__V6__SOURCE #define _VMS__V6__SOURCE #undef __VMS_VER #define __VMS_VER 70000000 #undef __CRTL_VER #define __CRTL_VER 70000000 #endif /* standard C header files */ #include <stdio.h> #include <ctype.h> /* VMS related header files */ #include <descrip.h> #include <ssdef.h> #include <stsdef.h> #include <secdef.h> /* application related header files */ #include "wasd.h" #define WASD_MODULE "PROXYREWORK" /******************/ /* global storage */ /******************/ int ProxyReworkMax; /********************/ /* external storage */ /********************/ extern int ProxyReadBufferSize; extern int ToLowerCase[], ToUpperCase[]; extern PROXY_ACCOUNTING_STRUCT *ProxyAccountingPtr; extern WATCH_STRUCT Watch; /*****************************************************************************/ /* Initialise the buffering of a response to be reworked. Then manage that buffering as required during the response. Return |true| to continue, |false| not to. */ BOOL ProxyReworkResponse (PROXY_TASK *tkptr) { int count, idx, size; char *cptr, *ctptr; REQUEST_STRUCT *rqptr; /*********/ /* begin */ /*********/ rqptr = tkptr->RequestPtr; if (WATCHMOD (tkptr, WATCH_MOD_PROXY)) WatchThis (WATCHITM(tkptr), WATCH_MOD_PROXY, "ProxyResponseRework() !UL !UL", tkptr->ResponseBodyLength, tkptr->ResponseContentLength); if (!tkptr->ResponseReworkInit) { /*********************/ /* initialise rework */ /*********************/ tkptr->ResponseReworkInit = true; ProxyAccountingPtr->ReworkCount++; if (!tkptr->ResponseContentTypePtr) { /* no content-type in proxied HTTP response - look for a file type */ for (cptr = rqptr->rqHeader.PathInfoPtr; *cptr; cptr++); while (cptr > rqptr->rqHeader.PathInfoPtr && *cptr != '/' && *cptr != '.') cptr--; if (*cptr == '.' && *(cptr+1)) { /* a file extension (perhaps) - try to map to content type */ ctptr = ConfigContentType (NULL, cptr); if (WATCHING (tkptr, WATCH_PROXY_REWORK)) WatchThis (WATCHITM(tkptr), WATCH_PROXY_REWORK, "TYPE !AZ !AZ", cptr, ctptr); tkptr->ResponseContentTypePtr = ctptr; } } if (!tkptr->ResponseContentTypePtr || MATCH9(tkptr->ResponseContentTypePtr, "text/html") || MATCH8(tkptr->ResponseContentTypePtr, "text/css")) { /* unknown (we'll sniff) or reworkable content */ tkptr->ResponseReworkMax = rqptr->ServicePtr->ProxyReworkMax; ProxyReworkBuffer (tkptr); } else return (true); /* initialise the host1=host2 data */ size = strlen(rqptr->rqPathSet.ProxyReworkPtr); cptr = VmGetHeap (rqptr, size); strcpy (cptr, rqptr->rqPathSet.ProxyReworkPtr); for (idx = 0; idx < 4 && *cptr; idx++) { tkptr->ReworkPtr1[idx] = cptr; while (*cptr && *cptr != '=') cptr++; tkptr->ReworkLen1[idx] = cptr - tkptr->ReworkPtr1[idx]; if (*cptr) *cptr++ = '\0'; for (tkptr->ReworkPtr2[idx] = cptr; *cptr && *cptr != '$'; cptr++); tkptr->ReworkLen2[idx] = cptr - tkptr->ReworkPtr2[idx]; if (*cptr) *cptr++ = '\0'; if (WATCHING (tkptr, WATCH_PROXY_REWORK)) WatchThis (WATCHITM(tkptr), WATCH_PROXY_REWORK, "REPLACE !&Z with !&Z", tkptr->ReworkPtr1[idx], tkptr->ReworkPtr2[idx]); } } if (tkptr->ResponseBufferSize > tkptr->ResponseReworkMax * 1024) { /***********/ /* too big */ /***********/ if (WATCHING (tkptr, WATCH_PROXY_REWORK)) WatchThis (WATCHITM(tkptr), WATCH_PROXY_REWORK, "REWORK abort !UL (!UL) exceeds !UL kB", tkptr->ResponseBufferSize / 1024, tkptr->ResponseBufferSize, tkptr->ResponseReworkMax); ProxyAccountingPtr->ReworkTooBig++; tkptr->ResponseStatusCode = 500; ErrorGeneralOverflow (rqptr, FI_LI); return (false); } if (!tkptr->ResponseContentTypePtr) { /************************/ /* content type unknown */ /************************/ if (tkptr->ResponseBodyLength) { ProxyAccountingPtr->ReworkNoType++; /* we (perhaps now) have some body to sniff */ ProxyReworkSniffHtml (tkptr); if (!tkptr->ResponseContentTypePtr) { /***************/ /* quit rework */ /***************/ if (WATCHING (tkptr, WATCH_PROXY_REWORK)) WatchThis (WATCHITM(tkptr), WATCH_PROXY_REWORK, "NO distinctive ODOUR"); /* not reworkable so go back to just proxying the content */ tkptr->ResponseReworkMax = 0; tkptr->ResponseBufferNetPtr = tkptr->ResponseBufferPtr; tkptr->ResponseBufferNetCount = tkptr->ResponseBodyLength; ProxyResponseNetWrite (tkptr); return (true); } } } count = tkptr->ResponseBufferCurrentPtr - tkptr->ResponseBufferPtr; if (tkptr->ResponseBufferSize - count < ProxyReadBufferSize / 2) { /*****************/ /* expand buffer */ /*****************/ tkptr->ResponseBufferSize += ProxyReadBufferSize; tkptr->ResponseBufferPtr = VmReallocHeap (rqptr, tkptr->ResponseBufferPtr, tkptr->ResponseBufferSize, FI_LI); tkptr->ResponseBufferCurrentPtr = tkptr->ResponseBufferPtr + count; tkptr->ResponseBufferRemaining = tkptr->ResponseBufferSize - count; } return (true); } /*****************************************************************************/ /* Adjust the network buffer as required. */ void ProxyReworkBuffer (PROXY_TASK *tkptr) { int len; char *cbptr, *cptr, *ctptr; REQUEST_STRUCT *rqptr; /*********/ /* begin */ /*********/ rqptr = tkptr->RequestPtr; if (WATCHMOD (rqptr, WATCH_MOD_PROXY)) WatchThis (WATCHITM(rqptr), WATCH_MOD_PROXY, "ProxyReworkBuffer()"); if (tkptr->ResponseContentLength) { /* response has specified a content length */ if (tkptr->ResponseContentLength / 1024 > tkptr->ResponseReworkMax) { /* exceeds maximum size - will be reported by caller */ tkptr->ResponseBufferSize = tkptr->ResponseContentLength; return; } } if (tkptr->ResponseContentLength > tkptr->ResponseBufferSize) { /* allocate buffer based on specified content-length */ cbptr = tkptr->ResponseBufferPtr; tkptr->ResponseBufferSize = tkptr->ResponseContentLength; tkptr->ResponseBufferPtr = VmGetHeap (rqptr, tkptr->ResponseBufferSize); tkptr->ResponseBufferCurrentPtr = tkptr->ResponseBufferPtr; tkptr->ResponseBufferRemaining = tkptr->ResponseBufferSize; } else { /* continue to use the existing buffer and grow as required */ cbptr = NULL; tkptr->ResponseBufferCurrentPtr = tkptr->ResponseBufferPtr; tkptr->ResponseBufferRemaining = tkptr->ResponseBufferSize; } if (WATCHING (tkptr, WATCH_PROXY_REWORK)) WatchThis (WATCHITM(tkptr), WATCH_PROXY_REWORK, "REWORK !UL kB", tkptr->ResponseReworkMax); if (tkptr->ResponseBufferNetCount) { /* response body exists beyond the response header */ memcpy (tkptr->ResponseBufferPtr, tkptr->ResponseBufferNetPtr, tkptr->ResponseBufferNetCount); tkptr->ResponseBufferCurrentPtr = tkptr->ResponseBufferPtr + tkptr->ResponseBufferNetCount; tkptr->ResponseBufferRemaining = tkptr->ResponseBufferSize - tkptr->ResponseBufferNetCount; tkptr->ResponseBodyLength = tkptr->ResponseBufferNetCount; } else { /* use the entire buffer */ tkptr->ResponseBufferCurrentPtr = tkptr->ResponseBufferPtr; tkptr->ResponseBufferRemaining = tkptr->ResponseBufferSize; tkptr->ResponseBodyLength = 0; } if (cbptr) { /* dispose of the previous buffer */ VmFreeFromHeap (rqptr, cbptr, FI_LI); } } /*****************************************************************************/ /* The response header and body have been read and buffered. If the content-type is unknown (no header field) then first check if the request path had a file type (e.g. ".html") and try to determine the content-type from that. If it didn't then sniff the body for indications of HTML or CSS. If the body is suitable to rework peform that and return the reworked content. If not just return the body as-is. */ void ProxyReworkProcess (PROXY_TASK *tkptr) { int cnt, delta, idx, len, rblen; char *cptr, *ctptr; REQUEST_STRUCT *rqptr; /*********/ /* begin */ /*********/ rqptr = tkptr->RequestPtr; if (WATCHMOD (rqptr, WATCH_MOD_PROXY)) WatchThis (WATCHITM(rqptr), WATCH_MOD_PROXY, "ProxyReworkProcess()"); ProxyReworkHeader (tkptr); for (idx = 0; idx < 4; idx++) { if (!tkptr->ReworkPtr1[idx]) break; ProxyAccountingPtr->ReworkReplaceSearch++; if (WATCHING (tkptr, WATCH_PROXY_REWORK)) WatchThis (WATCHITM(tkptr), WATCH_PROXY_REWORK, "BODY replacing !&Z with !&Z", tkptr->ReworkPtr1[idx], tkptr->ReworkPtr2[idx]); rblen = tkptr->ResponseBodyLength; len = StringReplace (rqptr, &tkptr->ResponseBufferPtr, tkptr->ReworkPtr1[idx], tkptr->ReworkPtr2[idx]); if (len) { tkptr->ResponseBodyLength = len; delta = tkptr->ReworkLen1[idx] - tkptr->ReworkLen2[idx]; cnt = (len - rblen) / delta; if (cnt < 0) cnt = -cnt; if (WATCHMOD (rqptr, WATCH_MOD_PROXY)) WatchThis (WATCHITM(rqptr), WATCH_MOD_PROXY, "!SL !UL-!UL=!SL !UL", delta, len, rblen, len-rblen, cnt); if (WATCHING (tkptr, WATCH_PROXY_REWORK)) WatchThis (WATCHITM(tkptr), WATCH_PROXY_REWORK, "BODY !UL replaced", cnt); ProxyAccountingPtr->ReworkReplaceCount += cnt; } else if (WATCHING (tkptr, WATCH_PROXY_REWORK)) WatchThis (WATCHITM(tkptr), WATCH_PROXY_REWORK, "BODY 0 replaced"); } if (WATCHMOD (tkptr, WATCH_MOD_PROXY)) { if ((len = strlen(tkptr->ResponseContentTypePtr)) > 15) len = 15; WatchThis (WATCHITM(tkptr), WATCH_MOD_PROXY, "!#AZ !UL !UL !UL", len, tkptr->ResponseContentTypePtr, tkptr->ResponseBufferSize, tkptr->ResponseBodyLength, tkptr->ResponseContentLength); WatchDataDump (tkptr->ResponseBufferPtr, tkptr->ResponseBodyLength); } tkptr->ResponseReworkMax = 0; /* ..ResponseBufferNet.. specify what is sent to the client */ tkptr->ResponseBufferNetPtr = tkptr->ResponseBufferPtr; tkptr->ResponseBufferNetCount = tkptr->ResponseBodyLength; } /*****************************************************************************/ /* ************ *** NOTE *** This function takes a pointer to a request!!! ************ */ void ProxyReworkWriteBody (REQUEST_STRUCT *rqptr) { PROXY_TASK *tkptr; /*********/ /* begin */ /*********/ if (WATCHMOD (rqptr, WATCH_MOD_PROXY)) WatchThis (WATCHITM(rqptr), WATCH_MOD_PROXY, "ProxyReworkWriteBody()"); tkptr = rqptr->ProxyTaskPtr; NetWrite (rqptr, &ProxyReworkEnd, tkptr->ResponseBufferPtr, tkptr->ResponseBodyLength); } /*****************************************************************************/ /* ************ *** NOTE *** This function takes a pointer to a request!!! ************ */ void ProxyReworkEnd (REQUEST_STRUCT *rqptr) { PROXY_TASK *tkptr; /*********/ /* begin */ /*********/ if (WATCHMOD (rqptr, WATCH_MOD_PROXY)) WatchThis (WATCHITM(rqptr), WATCH_MOD_PROXY, "ProxyReworkWriteBody()"); tkptr = rqptr->ProxyTaskPtr; ProxyEnd (tkptr); } /*****************************************************************************/ /* The response did not specify a content-type so sniffing the content is there any odour of HTML. Every response should have a content-type! But... fortunately HTML is fairly easily identified. Or should be. */ BOOL ProxyReworkSniffHtml (PROXY_TASK *tkptr) { BOOL yep = false; char ch = 0; char *cptr; /*********/ /* begin */ /*********/ if (WATCHING (tkptr, WATCH_PROXY_REWORK)) WatchThis (WATCHITM(tkptr), WATCH_PROXY_REWORK, "SNIFF for HTML"); for (cptr = tkptr->ResponseBufferPtr; isspace(*cptr); cptr++); if (!memcmp (cptr, "<!DOCTYPE html", 14)) yep = true; else { /* confine the search */ if (tkptr->ResponseBodyLength > 256) if (tkptr->ResponseBufferPtr[255]) ch = tkptr->ResponseBufferPtr[255]; if (stristr (cptr, "<html") && stristr (cptr, "<head") && stristr (cptr, "<title")) yep = true; if (ch) tkptr->ResponseBufferPtr[255] = '\0'; } if (yep) { tkptr->ResponseContentTypePtr = "text/html"; if (WATCHING (tkptr, WATCH_PROXY_REWORK)) WatchThis (WATCHITM(tkptr), WATCH_PROXY_REWORK, "SMELLS of HTML"); } return (yep); } /*****************************************************************************/ /* Rework strings in the (rebuilt) response header. */ void ProxyReworkHeader (PROXY_TASK *tkptr) { int cnt, delta, idx, len, rblen; REQUEST_STRUCT *rqptr; /*********/ /* begin */ /*********/ rqptr = tkptr->RequestPtr; if (WATCHMOD (rqptr, WATCH_MOD_PROXY)) WatchThis (WATCHITM(rqptr), WATCH_MOD_PROXY, "ProxyReworkHeader()"); for (idx = 0; idx < 4; idx++) { if (!tkptr->ReworkPtr1[idx]) break; if (WATCHING (tkptr, WATCH_PROXY_REWORK)) WatchThis (WATCHITM(tkptr), WATCH_PROXY_REWORK, "HEADER replacing !&Z with !&Z", tkptr->ReworkPtr1[idx], tkptr->ReworkPtr2[idx]); rblen = tkptr->RebuiltHeaderLength; len = StringReplace (rqptr, &tkptr->RebuiltHeaderPtr, tkptr->ReworkPtr1[idx], tkptr->ReworkPtr2[idx]); if (len) { /* see ProxyReworkProcess() for detail */ tkptr->RebuiltHeaderLength = len; delta = tkptr->ReworkLen1[idx] - tkptr->ReworkLen2[idx]; cnt = (len - rblen) / delta; if (cnt < 0) cnt = -cnt; if (WATCHING (tkptr, WATCH_PROXY_REWORK)) WatchThis (WATCHITM(tkptr), WATCH_PROXY_REWORK, "HEADER !UL replaced", cnt); ProxyAccountingPtr->ReworkReplaceCount += cnt; } else if (WATCHING (tkptr, WATCH_PROXY_REWORK)) WatchThis (WATCHITM(tkptr), WATCH_PROXY_REWORK, "HEADER 0 replaced"); } } /*****************************************************************************/