proxy.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2016 by Vladimir Dubrovin <3proxy@3proxy.ru>
  4. please read License Agreement
  5. */
  6. #include "proxy.h"
  7. #define RETURN(xxx) { param->res = xxx; goto CLEANRET; }
  8. char * proxy_stringtable[] = {
  9. /* 0 */ "HTTP/1.0 400 Bad Request\r\n"
  10. "Connection: close\r\n"
  11. "Content-type: text/html; charset=utf-8\r\n"
  12. "\r\n"
  13. "<html><head><title>400 Bad Request</title></head>\r\n"
  14. "<body><h2>400 Bad Request</h2></body></html>\r\n",
  15. /* 1 */ "HTTP/1.0 502 Bad Gateway\r\n"
  16. "Connection: close\r\n"
  17. "Content-type: text/html; charset=utf-8\r\n"
  18. "\r\n"
  19. "<html><head><title>502 Bad Gateway</title></head>\r\n"
  20. "<body><h2>502 Bad Gateway</h2><h3>Host Not Found or connection failed</h3></body></html>\r\n",
  21. /* 2 */ "HTTP/1.0 503 Service Unavailable\r\n"
  22. "Connection: close\r\n"
  23. "Content-type: text/html; charset=utf-8\r\n"
  24. "\r\n"
  25. "<html><head><title>503 Service Unavailable</title></head>\r\n"
  26. "<body><h2>503 Service Unavailable</h2><h3>You have exceeded your traffic limit</h3></body></html>\r\n",
  27. /* 3 */ "HTTP/1.0 503 Service Unavailable\r\n"
  28. "Connection: close\r\n"
  29. "Content-type: text/html; charset=utf-8\r\n"
  30. "\r\n"
  31. "<html><head><title>503 Service Unavailable</title></head>\r\n"
  32. "<body><h2>503 Service Unavailable</h2><h3>Recursion detected</h3></body></html>\r\n",
  33. /* 4 */ "HTTP/1.0 501 Not Implemented\r\n"
  34. "Connection: close\r\n"
  35. "Content-type: text/html; charset=utf-8\r\n"
  36. "\r\n"
  37. "<html><head><title>501 Not Implemented</title></head>\r\n"
  38. "<body><h2>501 Not Implemented</h2><h3>Required action is not supported by proxy server</h3></body></html>\r\n",
  39. /* 5 */ "HTTP/1.0 502 Bad Gateway\r\n"
  40. "Connection: close\r\n"
  41. "Content-type: text/html; charset=utf-8\r\n"
  42. "\r\n"
  43. "<html><head><title>502 Bad Gateway</title></head>\r\n"
  44. "<body><h2>502 Bad Gateway</h2><h3>Failed to connect parent proxy</h3></body></html>\r\n",
  45. /* 6 */ "HTTP/1.0 500 Internal Error\r\n"
  46. "Connection: close\r\n"
  47. "Content-type: text/html; charset=utf-8\r\n"
  48. "\r\n"
  49. "<html><head><title>500 Internal Error</title></head>\r\n"
  50. "<body><h2>500 Internal Error</h2><h3>Internal proxy error during processing your request</h3></body></html>\r\n",
  51. /* 7 */ "HTTP/1.0 407 Proxy Authentication Required\r\n"
  52. "Proxy-Authenticate: Basic realm=\"proxy\"\r\n"
  53. "Connection: close\r\n"
  54. "Content-type: text/html; charset=utf-8\r\n"
  55. "\r\n"
  56. "<html><head><title>407 Proxy Authentication Required</title></head>\r\n"
  57. "<body><h2>407 Proxy Authentication Required</h2><h3>Access to requested resource disallowed by administrator or you need valid username/password to use this resource</h3></body></html>\r\n",
  58. /* 8 */ "HTTP/1.0 200 Connection established\r\n\r\n",
  59. /* 9 */ "HTTP/1.0 200 Connection established\r\n"
  60. "Content-Type: text/html\r\n\r\n",
  61. /* 10*/ "HTTP/1.0 404 Not Found\r\n"
  62. "Connection: close\r\n"
  63. "Content-type: text/html; charset=utf-8\r\n"
  64. "\r\n"
  65. "<html><head><title>404 Not Found</title></head>\r\n"
  66. "<body><h2>404 Not Found</h2><h3>File not found</body></html>\r\n",
  67. /* 11*/ "HTTP/1.0 403 Forbidden\r\n"
  68. "Connection: close\r\n"
  69. "Content-type: text/html; charset=utf-8\r\n"
  70. "\r\n"
  71. "<html><head><title>403 Access Denied</title></head>\r\n"
  72. "<body><h2>403 Access Denied</h2><h3>Access control list denies you to access this resource</body></html>\r\n",
  73. /* 12*/ "HTTP/1.0 407 Proxy Authentication Required\r\n"
  74. #ifndef NOCRYPT
  75. "Proxy-Authenticate: NTLM\r\n"
  76. #endif
  77. "Proxy-Authenticate: Basic realm=\"proxy\"\r\n"
  78. "Connection: close\r\n"
  79. "Content-type: text/html; charset=utf-8\r\n"
  80. "\r\n"
  81. "<html><head><title>407 Proxy Authentication Required</title></head>\r\n"
  82. "<body><h2>407 Proxy Authentication Required</h2><h3>Access to requested resource disallowed by administrator or you need valid username/password to use this resource</h3></body></html>\r\n",
  83. /* 13*/ "HTTP/1.0 407 Proxy Authentication Required\r\n"
  84. "Connection: keep-alive\r\n"
  85. "Content-Length: 0\r\n"
  86. "Proxy-Authenticate: NTLM ",
  87. /* 14*/ "HTTP/1.0 403 Forbidden\r\n"
  88. "Connection: close\r\n"
  89. "Content-type: text/html; charset=utf-8\r\n"
  90. "\r\n"
  91. "<pre>",
  92. /* 15*/ "HTTP/1.0 503 Service Unavailable\r\n"
  93. "Connection: close\r\n"
  94. "Content-type: text/html; charset=utf-8\r\n"
  95. "\r\n"
  96. "<html><head><title>503 Service Unavailable</title></head>\r\n"
  97. "<body><h2>503 Service Unavailable</h2><h3>Your request violates configured policy</h3></body></html>\r\n",
  98. /* 16*/ "HTTP/1.0 401 Authentication Required\r\n"
  99. "WWW-Authenticate: Basic realm=\"FTP Server\"\r\n"
  100. "Connection: close\r\n"
  101. "Content-type: text/html; charset=utf-8\r\n"
  102. "\r\n"
  103. "<html><head><title>401 FTP Server requires authentication</title></head>\r\n"
  104. "<body><h2>401 FTP Server requires authentication</h2><h3>This FTP server rejects anonymous access</h3></body></html>\r\n",
  105. /* 17*/ "HTTP/1.1 100 Continue\r\n"
  106. "\r\n",
  107. NULL
  108. };
  109. #define LINESIZE 4096
  110. #define BUFSIZE (LINESIZE*2)
  111. #define FTPBUFSIZE 1536
  112. static void logurl(struct clientparam * param, char * buf, char * req, int ftp){
  113. char *sb;
  114. char *se;
  115. int len;
  116. if(!buf) req = NULL;
  117. if(req) {
  118. len = (int)strlen(req);
  119. if(len > (LINESIZE - 1)) len = LINESIZE - 1;
  120. memcpy(buf, req, len + 1);
  121. buf[LINESIZE - 1] = 0;
  122. sb = strchr(buf, '\r');
  123. if(sb)*sb = 0;
  124. if(ftp && (se = strchr(buf + 10, ':')) && (sb = strchr(se, '@')) ) {
  125. strcpy(se, sb);
  126. }
  127. }
  128. if(param->res != 555 && param->res != 508)dolog(param, (unsigned char *)(req?buf:NULL));
  129. }
  130. void decodeurl(unsigned char *s, int allowcr){
  131. unsigned char *d = s;
  132. unsigned u;
  133. while(*s){
  134. if(*s == '%' && ishex(s[1]) && ishex(s[2])){
  135. sscanf((char *)s+1, "%2x", &u);
  136. if(allowcr && u != '\r')*d++ = u;
  137. else if (u != '\r' && u != '\n') {
  138. if (u == '\"' || u == '\\') *d++ = '\\';
  139. else if (u == 255) *d++ = 255;
  140. *d++ = u;
  141. }
  142. s+=3;
  143. }
  144. else if(!allowcr && *s == '?') {
  145. break;
  146. }
  147. else if(*s == '+') {
  148. *d++ = ' ';
  149. s++;
  150. }
  151. else {
  152. *d++ = *s++;
  153. }
  154. }
  155. *d = 0;
  156. }
  157. void file2url(unsigned char *sb, unsigned char *buf, unsigned bufsize, int * inbuf, int skip255){
  158. for(; *sb; sb++){
  159. if((bufsize - *inbuf)<16)break;
  160. if(*sb=='\r'||*sb=='\n')continue;
  161. if(isallowed(*sb))buf[(*inbuf)++]=*sb;
  162. else if(*sb == '\"'){
  163. memcpy(buf+*inbuf, "%5C%22", 6);
  164. (*inbuf)+=6;
  165. }
  166. else if(skip255 && *sb == 255 && *(sb+1) == 255) {
  167. memcpy(buf+*inbuf, "%ff", 3);
  168. (*inbuf)+=3;
  169. sb++;
  170. }
  171. else {
  172. sprintf((char *)buf+*inbuf, "%%%.2x", (unsigned)*sb);
  173. (*inbuf)+=3;
  174. }
  175. }
  176. }
  177. void * proxychild(struct clientparam* param) {
  178. int res=0, i=0;
  179. unsigned char* buf = NULL, *newbuf;
  180. int inbuf;
  181. int bufsize;
  182. unsigned reqlen = 0;
  183. unsigned char *sb=NULL, *sg=NULL, *se=NULL, *sp=NULL,
  184. *req=NULL, *su=NULL, *ss = NULL;
  185. unsigned char *ftpbase=NULL;
  186. unsigned char username[1024];
  187. int keepalive = 0;
  188. uint64_t contentlength64 = 0;
  189. int hascontent =0;
  190. int isconnect = 0;
  191. int redirect = 0;
  192. int prefix = 0, ckeepalive=0;
  193. int ftp = 0;
  194. int anonymous;
  195. int sleeptime = 0;
  196. int reqsize, reqbufsize;
  197. int authenticate;
  198. struct pollfd fds[2];
  199. SOCKET ftps;
  200. char ftpbuf[FTPBUFSIZE];
  201. int inftpbuf = 0;
  202. #ifndef WITHMAIN
  203. FILTER_ACTION action;
  204. #endif
  205. if(!(buf = myalloc(BUFSIZE))) {RETURN(21);}
  206. bufsize = BUFSIZE;
  207. anonymous = param->srv->anonymous;
  208. for(;;){
  209. memset(buf, 0, bufsize);
  210. inbuf = 0;
  211. if(keepalive && (param->cliinbuf == param->clioffset) && (param->remsock != INVALID_SOCKET)){
  212. memset(fds, 0, sizeof(fds));
  213. fds[0].fd = param->clisock;
  214. fds[0].events = POLLIN;
  215. fds[1].fd = param->remsock;
  216. fds[1].events = POLLIN;
  217. res = so._poll(fds, 2, conf.timeouts[STRING_S]*1000);
  218. if(res<=0) {
  219. RETURN(555);
  220. }
  221. if((fds[1].revents & (POLLIN|POLLHUP|POLLERR|POLLNVAL))) {
  222. if(param->transparent || (!param->redirected && param->redirtype == R_HTTP)) RETURN(555);
  223. ckeepalive = 0;
  224. so._shutdown(param->remsock, SHUT_RDWR);
  225. so._closesocket(param->remsock);
  226. param->remsock = INVALID_SOCKET;
  227. param->redirected = 0;
  228. param->redirtype = 0;
  229. memset(&param->sinsl, 0, sizeof(param->sinsl));
  230. memset(&param->sinsr, 0, sizeof(param->sinsr));
  231. memset(&param->req, 0, sizeof(param->req));
  232. }
  233. }
  234. i = sockgetlinebuf(param, CLIENT, buf, LINESIZE - 1, '\n', conf.timeouts[STRING_L]);
  235. if(i<=0) {
  236. RETURN((keepalive)?555:(i)?507:508);
  237. }
  238. if (i==2 && buf[0]=='\r' && buf[1]=='\n') continue;
  239. buf[i] = 0;
  240. if(req) {
  241. if(!param->transparent && !param->srv->transparent && (i<=prefix || strncasecmp((char *)buf, (char *)req, prefix))){
  242. ckeepalive = 0;
  243. if(param->remsock != INVALID_SOCKET){
  244. so._shutdown(param->remsock, SHUT_RDWR);
  245. so._closesocket(param->remsock);
  246. }
  247. param->remsock = INVALID_SOCKET;
  248. param->redirected = 0;
  249. param->redirtype = 0;
  250. memset(&param->sinsl, 0, sizeof(param->sinsl));
  251. memset(&param->sinsr, 0, sizeof(param->sinsr));
  252. memset(&param->req, 0, sizeof(param->req));
  253. }
  254. myfree(req);
  255. }
  256. req = (unsigned char *)mystrdup((char *)buf);
  257. if(!req){RETURN(510);}
  258. if(i<10) {
  259. RETURN(511);
  260. }
  261. if(buf[i-3] == '1') keepalive = 2;
  262. param->transparent = 0;
  263. if((isconnect = !strncasecmp((char *)buf, "CONNECT", 7))) keepalive = 2;
  264. if ((sb=(unsigned char *)(unsigned char *)strchr((char *)buf, ' ')) == NULL) {RETURN(512);}
  265. ss = ++sb;
  266. if(!isconnect) {
  267. if (!strncasecmp((char *)sb, "http://", 7)) {
  268. sb += 7;
  269. }
  270. else if (!strncasecmp((char *)sb, "ftp://", 6)) {
  271. ftp = 1;
  272. sb += 6;
  273. }
  274. else if(*sb == '/') {
  275. param->transparent = 1;
  276. }
  277. else {
  278. RETURN (513);
  279. }
  280. }
  281. else {
  282. if ((se=(unsigned char *)(unsigned char *)strchr((char *)sb, ' ')) == NULL || sb==se) {RETURN (514);}
  283. *se = 0;
  284. }
  285. if(!param->transparent || isconnect) {
  286. if(!isconnect) {
  287. if ((se=(unsigned char *)(unsigned char *)strchr((char *)sb, '/')) == NULL
  288. || sb==se
  289. || !(sg=(unsigned char *)strchr((char *)sb, ' '))) {RETURN (515);}
  290. if(se > sg) se=sg;
  291. *se = 0;
  292. }
  293. prefix = (int)(se - buf);
  294. su = (unsigned char*)strrchr((char *)sb, '@');
  295. if(su) {
  296. su = (unsigned char *)mystrdup((char *)sb);
  297. decodeurl(su, 0);
  298. if(parseconnusername((char *)su, (struct clientparam *)param, 1, (unsigned short)((ftp)?21:80))) RETURN (100);
  299. myfree(su);
  300. }
  301. else if(parsehostname((char *)sb, (struct clientparam *)param, (unsigned short)((ftp)? 21:80))) RETURN(100);
  302. if(!isconnect){
  303. if(se==sg)*se-- = ' ';
  304. *se = '/';
  305. memmove(ss, se, i - (se - sb) + 1);
  306. }
  307. }
  308. reqlen = i = (int)strlen((char *)buf);
  309. if(!strncasecmp((char *)buf, "CONNECT", 7))param->operation = HTTP_CONNECT;
  310. else if(!strncasecmp((char *)buf, "GET", 3))param->operation = (ftp)?FTP_GET:HTTP_GET;
  311. else if(!strncasecmp((char *)buf, "PUT", 3))param->operation = (ftp)?FTP_PUT:HTTP_PUT;
  312. else if(!strncasecmp((char *)buf, "POST", 4)||!strncasecmp((char *)buf, "BITS_POST", 9))param->operation = HTTP_POST;
  313. else if(!strncasecmp((char *)buf, "HEAD", 4))param->operation = HTTP_HEAD;
  314. else param->operation = HTTP_OTHER;
  315. do {
  316. buf[inbuf+i]=0;
  317. /*printf("Got: %s\n", buf+inbuf);*/
  318. #ifndef WITHMAIN
  319. if(i > 25 && !param->srv->transparent && (!strncasecmp((char *)(buf+inbuf), "proxy-authorization", 19))){
  320. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  321. if(!sb)continue;
  322. ++sb;
  323. while(isspace(*sb))sb++;
  324. if(!*sb) continue;
  325. if(!strncasecmp((char *)sb, "basic", 5)){
  326. sb+=5;
  327. while(isspace(*sb))sb++;
  328. i = de64(sb, username, 255);
  329. if(i<=0)continue;
  330. username[i] = 0;
  331. sb = (unsigned char *)strchr((char *)username, ':');
  332. if(sb){
  333. *sb = 0;
  334. if(param->password)myfree(param->password);
  335. param->password = (unsigned char *)mystrdup((char *)sb+1);
  336. param->pwtype = 0;
  337. }
  338. if(param->username)myfree(param->username);
  339. param->username = (unsigned char *)mystrdup((char *)username);
  340. continue;
  341. }
  342. #ifndef NOCRYPT
  343. if(param->srv->usentlm && !strncasecmp((char *)sb, "ntlm", 4)){
  344. sb+=4;
  345. while(isspace(*sb))sb++;
  346. i = de64(sb, username, 1023);
  347. if(i<=16)continue;
  348. username[i] = 0;
  349. if(strncasecmp((char *)username, "NTLMSSP", 8)) continue;
  350. if(username[8] == 1) {
  351. while( (i = sockgetlinebuf(param, CLIENT, buf, BUFSIZE - 1, '\n', conf.timeouts[STRING_S])) > 2){
  352. if(i> 15 && (!strncasecmp((char *)(buf), "content-length", 14))){
  353. buf[i]=0;
  354. sscanf((char *)buf + 15, "%"PRINTF_INT64_MODIFIER"u", &contentlength64);
  355. }
  356. }
  357. while( contentlength64 > 0 && (i = sockgetlinebuf(param, CLIENT, buf, (BUFSIZE < contentlength64)? BUFSIZE - 1:(int)contentlength64, '\n', conf.timeouts[STRING_S])) > 0){
  358. if ((uint64_t)i > contentlength64) break;
  359. contentlength64-=i;
  360. }
  361. contentlength64 = 0;
  362. if(param->password)myfree(param->password);
  363. param->password = myalloc(32);
  364. param->pwtype = 2;
  365. i = (int)strlen(proxy_stringtable[13]);
  366. memcpy(buf, proxy_stringtable[13], i);
  367. genchallenge(param, (char *)param->password, (char *)buf + i);
  368. memcpy(buf + strlen((char *)buf), "\r\n\r\n", 5);
  369. socksend(param->clisock, buf, (int)strlen((char *)buf), conf.timeouts[STRING_S]);
  370. ckeepalive = keepalive = 1;
  371. goto REQUESTEND;
  372. }
  373. if(username[8] == 3 && param->pwtype == 2 && i>=80) {
  374. unsigned offset, len;
  375. len = username[20] + (((unsigned)username[21]) << 8);
  376. offset = username[24] + (((unsigned)username[25]) << 8);
  377. if(len != 24 || len + offset > (unsigned)i) continue;
  378. memcpy(param->password + 8, username + offset, 24);
  379. len = username[36] + (((unsigned)username[37]) << 8);
  380. offset = username[40] + (((unsigned)username[41]) << 8);
  381. if(len> 255 || len + offset > (unsigned)i) continue;
  382. if(param->username) myfree(param->username);
  383. unicode2text((char *)username+offset, (char *)username+offset, (len>>1));
  384. param->username = (unsigned char *)mystrdup((char *)username+offset);
  385. }
  386. continue;
  387. }
  388. #endif
  389. }
  390. #endif
  391. if(!isconnect && (
  392. (i> 25 && !strncasecmp((char *)(buf+inbuf), "proxy-connection:", 17))
  393. ||
  394. (i> 16 && (!strncasecmp((char *)(buf+inbuf), "connection:", 11)))
  395. )){
  396. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  397. if(!sb)continue;
  398. ++sb;
  399. while(isspace(*sb))sb++;
  400. if(strncasecmp((char *)sb,"upgrade", 7)){
  401. if(!strncasecmp((char *)sb,"keep-alive", 10))keepalive = 1;
  402. else keepalive = 0;
  403. continue;
  404. }
  405. }
  406. if( i > 11 && !strncasecmp((char *)(buf+inbuf), "Expect: 100", 11)){
  407. keepalive = 1;
  408. socksend(param->clisock, (unsigned char *)proxy_stringtable[17], (int)strlen(proxy_stringtable[17]), conf.timeouts[STRING_S]);
  409. continue;
  410. }
  411. if(param->transparent && i > 6 && !strncasecmp((char *)buf + inbuf, "Host:", 5)){
  412. unsigned char c;
  413. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  414. if(!sb)continue;
  415. ++sb;
  416. while(isspace(*sb))sb++;
  417. (se = (unsigned char *)strchr((char *)sb, '\r')) || (se = (unsigned char *)strchr((char *)sb, '\n'));
  418. if(se) {
  419. c = *se;
  420. *se = 0;
  421. }
  422. if(!param->hostname){
  423. if(parsehostname((char *)sb, param, 80)) RETURN(100);
  424. }
  425. newbuf = myalloc(strlen((char *)req) + strlen((char *)(buf+inbuf)) + 8);
  426. if(newbuf){
  427. sp = (unsigned char *)strchr((char *)req+1, '/');
  428. memcpy(newbuf, req, (sp - req));
  429. sprintf((char*)newbuf + (sp - req), "http://%s%s",sb,sp);
  430. myfree(req);
  431. req = newbuf;
  432. }
  433. if(se)*se = c;
  434. }
  435. if(ftp && i > 13 && (!strncasecmp((char *)(buf+inbuf), "authorization", 13))){
  436. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  437. if(!sb)continue;
  438. ++sb;
  439. while(isspace(*sb))sb++;
  440. if(!*sb) continue;
  441. if(!strncasecmp((char *)sb, "basic", 5)){
  442. sb+=5;
  443. while(isspace(*sb))sb++;
  444. i = de64(sb, username, 255);
  445. if(i<=0)continue;
  446. username[i] = 0;
  447. sb = (unsigned char *)strchr((char *)username, ':');
  448. if(sb){
  449. *sb = 0;
  450. if(param->extpassword)myfree(param->extpassword);
  451. param->extpassword = (unsigned char *)mystrdup((char *)sb+1);
  452. }
  453. if(param->extusername)myfree(param->extusername);
  454. param->extusername = (unsigned char *)mystrdup((char *)username);
  455. continue;
  456. }
  457. }
  458. if(i> 15 && (!strncasecmp((char *)(buf+inbuf), "content-length", 14))){
  459. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  460. if(!sb)continue;
  461. ++sb;
  462. while(isspace(*sb))sb++;
  463. sscanf((char *)sb, "%"PRINTF_INT64_MODIFIER"u",&contentlength64);
  464. if(param->maxtrafout64 && (param->maxtrafout64 < param->statscli64 || contentlength64 > param->maxtrafout64 - param->statscli64)){
  465. RETURN(10);
  466. }
  467. if(param->ndatfilterscli > 0 && contentlength64 > 0) continue;
  468. }
  469. inbuf += i;
  470. if((bufsize - inbuf) < LINESIZE){
  471. if (bufsize > (LINESIZE * 16)){
  472. RETURN (516);
  473. }
  474. if(!(newbuf = myrealloc(buf, bufsize + BUFSIZE))){RETURN (21);}
  475. buf = newbuf;
  476. bufsize += BUFSIZE;
  477. }
  478. } while( (i = sockgetlinebuf(param, CLIENT, buf + inbuf, LINESIZE - 2, '\n', conf.timeouts[STRING_S])) > 2);
  479. buf[inbuf] = 0;
  480. reqsize = (int)strlen((char *)req);
  481. reqbufsize = reqsize + 1;
  482. #ifndef WITHMAIN
  483. action = handlereqfilters(param, &req, &reqbufsize, 0, &reqsize);
  484. if(action == HANDLED){
  485. RETURN(0);
  486. }
  487. if(action != PASS) RETURN(517);
  488. action = handlehdrfilterscli(param, &buf, &bufsize, 0, &inbuf);
  489. if(action == HANDLED){
  490. RETURN(0);
  491. }
  492. if(action != PASS) RETURN(517);
  493. param->nolongdatfilter = 0;
  494. if (conf.filtermaxsize && contentlength64 > (uint64_t)conf.filtermaxsize) {
  495. param->nolongdatfilter = 1;
  496. }
  497. else if(param->ndatfilterscli > 0 && contentlength64 > 0){
  498. uint64_t newlen64;
  499. newlen64 = sockfillbuffcli(param, (unsigned long)contentlength64, CONNECTION_S);
  500. if(newlen64 == contentlength64) {
  501. action = handlepredatflt(param);
  502. if(action == HANDLED){
  503. RETURN(0);
  504. }
  505. if(action != PASS) RETURN(19);
  506. action = handledatfltcli(param, &param->clibuf, (int *)&param->clibufsize, 0, (int *)&param->cliinbuf);
  507. if(action == HANDLED){
  508. RETURN(0);
  509. }
  510. if(action != PASS) RETURN(517);
  511. contentlength64 = param->cliinbuf;
  512. param->nolongdatfilter = 1;
  513. }
  514. sprintf((char*)buf+strlen((char *)buf), "Content-Length: %"PRINTF_INT64_MODIFIER"u\r\n", contentlength64);
  515. }
  516. #endif
  517. if(param->srv->needuser > 1 && !param->username) {RETURN(4);}
  518. if((res = (*param->srv->authfunc)(param))) {RETURN(res);}
  519. if(ftp && param->redirtype != R_HTTP){
  520. SOCKET s;
  521. int mode = 0;
  522. int i=0;
  523. inftpbuf = 0;
  524. if(!ckeepalive){
  525. inftpbuf = FTPBUFSIZE - 20;
  526. res = ftplogin(param, ftpbuf, &inftpbuf);
  527. if(res){
  528. RETURN(res);
  529. }
  530. }
  531. ckeepalive = 1;
  532. if(ftpbase) myfree(ftpbase);
  533. ftpbase = NULL;
  534. if(!(sp = (unsigned char *)strchr((char *)ss, ' '))){RETURN(799);}
  535. *sp = 0;
  536. decodeurl(ss, 0);
  537. i = (int)strlen((char *)ss);
  538. if(!(ftpbase = myalloc(i+2))){RETURN(21);}
  539. memcpy(ftpbase, ss, i);
  540. if(ftpbase[i-1] != '/') ftpbase[i++] = '/';
  541. ftpbase[i] = 0;
  542. memcpy(buf, "<pre><hr>\n", 10);
  543. inbuf = 10;
  544. if(inftpbuf) {
  545. memcpy(buf+inbuf, ftpbuf, inftpbuf);
  546. inbuf += inftpbuf;
  547. memcpy(buf+inbuf, "<hr>", 4);
  548. inbuf += 4;
  549. }
  550. if(ftpbase[1] != 0){
  551. memcpy(buf+inbuf, "[<A HREF=\"..\">..</A>]\n", 22);
  552. inbuf += 22;
  553. }
  554. inftpbuf = FTPBUFSIZE - (20 + inftpbuf);
  555. res = ftpcd(param, ftpbase, ftpbuf, &inftpbuf);
  556. if(res){
  557. res = ftptype(param, (unsigned char *)"I");
  558. if(res)RETURN(res);
  559. ftpbase[--i] = 0;
  560. ftps = ftpcommand(param, param->operation == FTP_PUT? (unsigned char *)"PUT" : (unsigned char *)"RETR", ftpbase);
  561. }
  562. else {
  563. if(inftpbuf){
  564. memcpy(buf+inbuf, ftpbuf, inftpbuf);
  565. inbuf += inftpbuf;
  566. memcpy(buf+inbuf, "<hr>", 4);
  567. inbuf += 4;
  568. }
  569. ftps = ftpcommand(param, (unsigned char *)"LIST", NULL);
  570. mode = 1;
  571. }
  572. if(ftps == INVALID_SOCKET){RETURN(780);}
  573. if(!mode){
  574. socksend(param->clisock, (unsigned char *)proxy_stringtable[8], (int)strlen(proxy_stringtable[8]), conf.timeouts[STRING_S]);
  575. s = param->remsock;
  576. param->remsock = ftps;
  577. if((param->operation == FTP_PUT) && (contentlength64 > 0)) param->waitclient64 = contentlength64;
  578. res = mapsocket(param, conf.timeouts[CONNECTION_L]);
  579. if (res == 99) res = 0;
  580. so._closesocket(ftps);
  581. ftps = INVALID_SOCKET;
  582. param->remsock = s;
  583. }
  584. else {
  585. int headsent = 0;
  586. int gotres = -1;
  587. s = param->remsock;
  588. if(param->srvoffset < param->srvinbuf){
  589. gotres = ftpres(param, buf+inbuf, bufsize-(inbuf+100));
  590. if(gotres) inbuf= (int)strlen((char *)buf);
  591. }
  592. param->remsock = ftps;
  593. if(gotres <= 0) for(; (res = sockgetlinebuf(param, SERVER, (unsigned char *)ftpbuf, FTPBUFSIZE - 20, '\n', conf.timeouts[STRING_S])) > 0; i++){
  594. int isdir = 0;
  595. int islink = 0;
  596. int filetoken =-1;
  597. int sizetoken =-1;
  598. int modetoken =-1;
  599. int datetoken =-1;
  600. int spaces = 1;
  601. unsigned char * tokens[10];
  602. unsigned wordlen [10];
  603. unsigned char j=0;
  604. int space = 1;
  605. ftpbuf[res] = 0;
  606. if(!i && ftpbuf[0] == 't' && ftpbuf[1] == 'o' && ftpbuf[2] == 't'){
  607. mode = 2;
  608. continue;
  609. }
  610. if(!isnumber(*ftpbuf) && mode == 1) mode = 2;
  611. for(sb=(unsigned char *)ftpbuf; *sb; sb++){
  612. if(!space && isspace(*sb)){
  613. space = 1;
  614. wordlen[j]=(unsigned)(sb-tokens[j]);
  615. j++;
  616. }
  617. if(space && !isspace(*sb)){
  618. space = 0;
  619. tokens[j] = sb;
  620. if(j==8)break;
  621. }
  622. }
  623. if(mode == 1){
  624. if(j < 4) continue;
  625. if(!(isdir = !memcmp(tokens[2], "<DIR>", wordlen[2])) && !isnumber(*tokens[2])){
  626. continue;
  627. }
  628. datetoken = 0;
  629. wordlen[datetoken] = ((unsigned)(tokens[1] - tokens[0])) + wordlen[1];
  630. sizetoken = 2;
  631. filetoken = 3;
  632. spaces = 10;
  633. }
  634. else {
  635. if(j < 8 || wordlen[0]!=10) continue;
  636. if(j < 8 || !isnumber(*tokens[4])) mode = 3;
  637. if(*tokens[0] == 'd') isdir = 1;
  638. if(*tokens[0] == 'l') islink = 1;
  639. modetoken = 0;
  640. sizetoken = (mode == 2)? 4:3;
  641. filetoken = (mode == 2)? 8:7;
  642. datetoken = (mode == 2)? 5:4;
  643. tokens[filetoken] = tokens[filetoken-1];
  644. while(*tokens[filetoken] && !isspace(*tokens[filetoken]))tokens[filetoken]++;
  645. if(*tokens[filetoken]){
  646. tokens[filetoken]++;
  647. }
  648. wordlen[datetoken] = (unsigned)(tokens[filetoken] - tokens[datetoken]);
  649. wordlen[filetoken] = (unsigned)strlen((char *)tokens[filetoken]);
  650. }
  651. if(modetoken >= 0) memcpy(buf+inbuf, tokens[modetoken], 11);
  652. else memcpy(buf+inbuf, "---------- ", 11);
  653. inbuf += 11;
  654. if((int) wordlen[datetoken]+256 > bufsize-inbuf) continue;
  655. memcpy(buf+inbuf, tokens[datetoken], wordlen[datetoken]);
  656. inbuf += wordlen[datetoken];
  657. if(isdir){
  658. memcpy(buf+inbuf, " DIR", 10);
  659. inbuf+=10;
  660. }
  661. else if(islink){
  662. memcpy(buf+inbuf, " LINK", 10);
  663. inbuf+=10;
  664. }
  665. else{
  666. unsigned k;
  667. if(wordlen[sizetoken]>10) wordlen[sizetoken] = 10;
  668. for(k=10; k > wordlen[sizetoken]; k--){
  669. buf[inbuf++] = ' ';
  670. }
  671. memcpy(buf+inbuf, tokens[sizetoken], wordlen[sizetoken]);
  672. inbuf+=wordlen[sizetoken];
  673. }
  674. memcpy(buf+inbuf, " <A HREF=\"", 10);
  675. inbuf+=10;
  676. sb = NULL;
  677. if(islink) sb = (unsigned char *)strstr((char *)tokens[filetoken], " -> ");
  678. if(sb) sb+=4;
  679. else sb=tokens[filetoken];
  680. if(*sb != '/' && ftpbase)file2url(ftpbase, buf, bufsize, (int *)&inbuf, 1);
  681. file2url(sb, buf, bufsize, (int *)&inbuf, 0);
  682. if(isdir)buf[inbuf++] = '/';
  683. memcpy(buf+inbuf, "\">", 2);
  684. inbuf+=2;
  685. for(sb=tokens[filetoken]; *sb; sb++){
  686. if((bufsize - inbuf)<16)break;
  687. if(*sb == '<'){
  688. memcpy(buf+inbuf, "&lt;", 4);
  689. inbuf+=4;
  690. }
  691. else if(*sb == '>'){
  692. memcpy(buf+inbuf, "&gt;", 4);
  693. inbuf+=4;
  694. }
  695. else if(*sb == '\r' || *sb=='\n'){
  696. continue;
  697. }
  698. else if(islink && sb[0] == ' ' && sb[1] == '-'
  699. && sb[2] == '>'){
  700. memcpy(buf+inbuf, "</A> ", 5);
  701. inbuf+=5;
  702. }
  703. else buf[inbuf++]=*sb;
  704. }
  705. if(islink!=2){
  706. memcpy(buf+inbuf, "</A>", 4);
  707. inbuf+=4;
  708. }
  709. buf[inbuf++] = '\n';
  710. if((bufsize - inbuf) < LINESIZE){
  711. if (bufsize > 20000){
  712. if(!headsent++){
  713. socksend(param->clisock, (unsigned char *)proxy_stringtable[9], (int)strlen(proxy_stringtable[9]), conf.timeouts[STRING_S]);
  714. }
  715. if((unsigned)socksend(param->clisock, buf, inbuf, conf.timeouts[STRING_S]) != inbuf){
  716. RETURN(781);
  717. }
  718. inbuf = 0;
  719. }
  720. else {
  721. if(!(newbuf = myrealloc(buf, bufsize + BUFSIZE))){RETURN (21);}
  722. buf = newbuf;
  723. bufsize += BUFSIZE;
  724. }
  725. }
  726. }
  727. memcpy(buf+inbuf, "<hr>", 4);
  728. inbuf += 4;
  729. so._closesocket(ftps);
  730. ftps = INVALID_SOCKET;
  731. param->remsock = s;
  732. if(inbuf){
  733. buf[inbuf] = 0;
  734. if(gotres < 0 ) res = ftpres(param, buf+inbuf, bufsize-inbuf);
  735. else res = gotres;
  736. inbuf = (int)strlen((char *)buf);
  737. if(!headsent){
  738. sprintf(ftpbuf,
  739. "HTTP/1.0 200 OK\r\n"
  740. "Content-Type: text/html\r\n"
  741. "Connection: keep-alive\r\n"
  742. "Content-Length: %d\r\n\r\n",
  743. inbuf);
  744. socksend(param->clisock, (unsigned char *)ftpbuf, (int)strlen(ftpbuf), conf.timeouts[STRING_S]);
  745. }
  746. socksend(param->clisock, buf, inbuf, conf.timeouts[STRING_S]);
  747. if(res){RETURN(res);}
  748. if(!headsent)goto REQUESTEND;
  749. }
  750. RETURN(0);
  751. }
  752. RETURN(res);
  753. }
  754. if(isconnect && param->redirtype != R_HTTP) {
  755. socksend(param->clisock, (unsigned char *)proxy_stringtable[8], (int)strlen(proxy_stringtable[8]), conf.timeouts[STRING_S]);
  756. if(param->redirectfunc) {
  757. if(req)myfree(req);
  758. if(buf)myfree(buf);
  759. return (*param->redirectfunc)(param);
  760. }
  761. param->res = mapsocket(param, conf.timeouts[CONNECTION_L]);
  762. if(param->redirectfunc) {
  763. if(req)myfree(req);
  764. if(buf)myfree(buf);
  765. return (*param->redirectfunc)(param);
  766. }
  767. RETURN(param->res);
  768. }
  769. if(!req || param->redirtype != R_HTTP) {
  770. reqlen = 0;
  771. }
  772. else {
  773. #ifdef TCP_CORK
  774. int opt = 1;
  775. so._setsockopt(param->remsock, IPPROTO_TCP, TCP_CORK, (unsigned char *)&opt, sizeof(int));
  776. #endif
  777. redirect = 1;
  778. res = (int)strlen((char *)req);
  779. if(socksend(param->remsock, req , res, conf.timeouts[STRING_L]) != res) {
  780. RETURN(518);
  781. }
  782. param->statscli64 += res;
  783. param->nwrites++;
  784. }
  785. inbuf = 0;
  786. #ifndef ANONYMOUS
  787. if(!anonymous){
  788. int len = strlen((char *)buf);
  789. len += sprintf((char*)buf + len, "Forwarded: for=");
  790. if(*SAFAMILY(&param->sincr) == AF_INET6) len += sprintf((char*)buf + len, "\"[");
  791. len += myinet_ntop(*SAFAMILY(&param->sincr), SAADDR(&param->sincr), (char *)buf + len, 128);
  792. if(*SAFAMILY(&param->sincr) == AF_INET6) len += sprintf((char*)buf + len, "]:%d\";by=", (int)ntohs(*SAPORT(&param->sincr)));
  793. else len += sprintf((char*)buf + len, ":%d;by=", (int)ntohs(*SAPORT(&param->sincr)));
  794. gethostname((char *)(buf + len), 256);
  795. sprintf((char*)buf+strlen((char *)buf), ":%d\r\n", (int)ntohs(*SAPORT(&param->sincl)));
  796. }
  797. else if(anonymous>1){
  798. sprintf((char*)buf+strlen((char *)buf), "Via: 1.1 ");
  799. gethostname((char *)(buf+strlen((char *)buf)), 256);
  800. sprintf((char*)buf+strlen((char *)buf), ":%d (%s %s)\r\nX-Forwarded-For: ", (int)ntohs(*SAPORT(&param->srv->intsa)), conf.stringtable?conf.stringtable[2]:(unsigned char *)"", conf.stringtable?conf.stringtable[3]:(unsigned char *)"");
  801. if(anonymous != 2)myinet_ntop(*SAFAMILY(&param->sincr), SAADDR(&param->sincr), (char *)buf + strlen((char *)buf), 128);
  802. else {
  803. unsigned long tmp;
  804. tmp = ((unsigned long)myrand(param, sizeof(struct clientparam))<<16)^(unsigned long)rand();
  805. myinet_ntop(AF_INET, &tmp, (char *)buf + strlen((char *)buf), 64);
  806. }
  807. sprintf((char*)buf+strlen((char *)buf), "\r\n");
  808. }
  809. #endif
  810. if(keepalive <= 1) {
  811. sprintf((char*)buf+strlen((char *)buf), "Connection: %s\r\n", keepalive? "keep-alive":"close");
  812. }
  813. if(param->extusername){
  814. sprintf((char*)buf + strlen((char *)buf), "%s: Basic ", (redirect)?"Proxy-Authorization":"Authorization");
  815. sprintf((char*)username, "%.128s:%.128s", param->extusername, param->extpassword?param->extpassword:(unsigned char*)"");
  816. en64(username, buf+strlen((char *)buf), (int)strlen((char *)username));
  817. sprintf((char*)buf + strlen((char *)buf), "\r\n");
  818. }
  819. sprintf((char*)buf+strlen((char *)buf), "\r\n");
  820. if ((res = socksend(param->remsock, buf+reqlen, (int)strlen((char *)buf+reqlen), conf.timeouts[STRING_S])) != (int)strlen((char *)buf+reqlen)) {
  821. RETURN(518);
  822. }
  823. #ifdef TCP_CORK
  824. {
  825. int opt = 0;
  826. so._setsockopt(param->remsock, IPPROTO_TCP, TCP_CORK, (unsigned char *)&opt, sizeof(int));
  827. }
  828. #endif
  829. param->statscli64 += res;
  830. param->nwrites++;
  831. if(param->bandlimfunc) {
  832. sleeptime = param->bandlimfunc(param, 0, (int)strlen((char *)buf));
  833. }
  834. if(contentlength64 > 0){
  835. param->waitclient64 = contentlength64;
  836. res = mapsocket(param, conf.timeouts[CONNECTION_S]);
  837. param->waitclient64 = 0;
  838. if(res != 99) {
  839. RETURN(res);
  840. }
  841. }
  842. contentlength64 = 0;
  843. inbuf = 0;
  844. ckeepalive = keepalive;
  845. res = 0;
  846. authenticate = 0;
  847. param->chunked = 0;
  848. hascontent = 0;
  849. while( (i = sockgetlinebuf(param, SERVER, buf + inbuf, LINESIZE - 1, '\n', conf.timeouts[(res)?STRING_S:STRING_L])) > 2) {
  850. if(!res && i>9)param->status = res = atoi((char *)buf + inbuf + 9);
  851. if(((i >= 25 && !strncasecmp((char *)(buf+inbuf), "proxy-connection:", 17))
  852. ||
  853. (i> 16 && !strncasecmp((char *)(buf+inbuf), "connection:", 11))
  854. )){
  855. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  856. if(!sb)continue;
  857. ++sb;
  858. while(isspace(*sb))sb++;
  859. if(strncasecmp((char *)sb,"keep-alive", 10))ckeepalive = 0;
  860. if(!param->srv->transparent && res >= 200)continue;
  861. }
  862. else if(i> 6 && !param->srv->transparent && (!strncasecmp((char *)(buf+inbuf), "proxy-", 6))){
  863. continue;
  864. }
  865. else if(i> 6 && (!strncasecmp((char *)(buf+inbuf), "www-authenticate", 16))){
  866. authenticate = 1;
  867. }
  868. else if(i > 15 && (!strncasecmp((char *)(buf+inbuf), "content-length", 14))){
  869. buf[inbuf+i]=0;
  870. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  871. if(!sb)continue;
  872. ++sb;
  873. while(isspace(*sb))sb++;
  874. sscanf((char *)sb, "%"PRINTF_INT64_MODIFIER"u", &contentlength64);
  875. hascontent = 1;
  876. if(param->unsafefilter && param->ndatfilterssrv > 0) {
  877. hascontent = 2;
  878. continue;
  879. }
  880. if(param->maxtrafin64 && (param->maxtrafin64 < param->statssrv64 || contentlength64 + param->statssrv64 > param->maxtrafin64)){
  881. RETURN(10);
  882. }
  883. }
  884. else if(i>25 && (!strncasecmp((char *)(buf+inbuf), "transfer-encoding", 17))){
  885. buf[inbuf+i]=0;
  886. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  887. if(!sb)continue;
  888. ++sb;
  889. while(isspace(*sb))sb++;
  890. if(!strncasecmp((char *)sb, "chunked", 7)){
  891. param->chunked = 1;
  892. }
  893. }
  894. inbuf += i;
  895. if((bufsize - inbuf) < LINESIZE){
  896. if (bufsize > 20000){
  897. RETURN (516);
  898. }
  899. if(!(newbuf = myrealloc(buf, bufsize + BUFSIZE))){RETURN (21);}
  900. buf = newbuf;
  901. bufsize += BUFSIZE;
  902. }
  903. }
  904. if(res < 200 || res > 499) {
  905. ckeepalive = 0;
  906. }
  907. if((res == 304 || res == 204) && !hascontent){
  908. hascontent = 1;
  909. contentlength64 = 0;
  910. }
  911. if(param->bandlimfunc) {
  912. int st1;
  913. st1 = (*param->bandlimfunc)(param, inbuf, 0);
  914. if(st1 > sleeptime) sleeptime = st1;
  915. if(sleeptime > 0){
  916. /* if(sleeptime > 30) sleeptime = 30; */
  917. usleep(sleeptime * SLEEPTIME);
  918. }
  919. }
  920. buf[inbuf] = 0;
  921. if(inbuf < 9) {RETURN (522);}
  922. #ifndef WITHMAIN
  923. action = handlehdrfilterssrv(param, &buf, &bufsize, 0, &inbuf);
  924. if(action == HANDLED){
  925. RETURN(0);
  926. }
  927. if(action != PASS) RETURN(517);
  928. param->nolongdatfilter = 0;
  929. if (conf.filtermaxsize && contentlength64 > (uint64_t)conf.filtermaxsize) {
  930. param->nolongdatfilter = 1;
  931. }
  932. else if(param->ndatfilterssrv > 0 && contentlength64 > 0 && param->operation != HTTP_HEAD && res != 204 && res != 304){
  933. uint64_t newlen;
  934. newlen = (uint64_t)sockfillbuffsrv(param, (unsigned long) contentlength64, CONNECTION_S);
  935. if(newlen == contentlength64) {
  936. action = handlepredatflt(param);
  937. if(action == HANDLED){
  938. RETURN(0);
  939. }
  940. if(action != PASS) RETURN(19);
  941. action = handledatfltsrv(param, &param->srvbuf, (int *)&param->srvbufsize, 0, (int *)&param->srvinbuf);
  942. param->nolongdatfilter = 1;
  943. if(action == HANDLED){
  944. RETURN(0);
  945. }
  946. if(action != PASS) RETURN(517);
  947. contentlength64 = param->srvinbuf;
  948. sprintf((char*)buf+strlen((char *)buf), "Content-Length: %"PRINTF_INT64_MODIFIER"u\r\n", contentlength64);
  949. hascontent = 1;
  950. }
  951. }
  952. if (contentlength64 > 0 && hascontent != 1) ckeepalive = 0;
  953. #else
  954. #endif
  955. if(!isconnect || param->operation){
  956. if(authenticate && !param->transparent) sprintf((char*)buf+strlen((char *)buf),
  957. "Proxy-support: Session-Based-Authentication\r\n"
  958. "Connection: Proxy-support\r\n"
  959. );
  960. if(!param->srv->transparent && res>=200){
  961. if(ckeepalive <= 1) sprintf((char*)buf+strlen((char *)buf), "Connection: %s\r\n",
  962. (hascontent && ckeepalive)?"keep-alive":"close");
  963. }
  964. sprintf((char*)buf + strlen((char *)buf), "\r\n");
  965. if((socksend(param->clisock, buf, (int)strlen((char *)buf), conf.timeouts[STRING_S])) != (int)strlen((char *)buf)) {
  966. RETURN(521);
  967. }
  968. }
  969. if((param->chunked || contentlength64 > 0) && param->operation != HTTP_HEAD && res != 204 && res != 304) {
  970. do {
  971. if(param->chunked){
  972. unsigned char smallbuf[32];
  973. while ((i = sockgetlinebuf(param, SERVER, smallbuf, 30, '\n', conf.timeouts[STRING_S])) == 2) {
  974. if (socksend(param->clisock, smallbuf, i, conf.timeouts[STRING_S]) != i){
  975. RETURN(533);
  976. }
  977. if(param->chunked == 2) break;
  978. }
  979. if(i<3) {
  980. keepalive = 0;
  981. break;
  982. }
  983. if (socksend(param->clisock, smallbuf, i, conf.timeouts[STRING_S]) != i){
  984. RETURN(535);
  985. }
  986. if(param->chunked == 2) {
  987. if((i = sockgetlinebuf(param, SERVER, smallbuf, 30, '\n', conf.timeouts[STRING_S])) != 2) RETURN(534);
  988. if (socksend(param->clisock, smallbuf, i, conf.timeouts[STRING_S]) != i){
  989. RETURN(533);
  990. }
  991. break;
  992. }
  993. smallbuf[i] = 0;
  994. contentlength64 = 0;
  995. sscanf((char *)smallbuf, "%"PRINTF_INT64_MODIFIER"x", &contentlength64);
  996. if(contentlength64 == 0) {
  997. param->chunked = 2;
  998. }
  999. }
  1000. if(param->chunked != 2){
  1001. param->waitserver64 = contentlength64;
  1002. if((res = mapsocket(param, conf.timeouts[CONNECTION_S])) != 98){
  1003. RETURN(res);
  1004. }
  1005. param->waitserver64 = 0;
  1006. }
  1007. } while(param->chunked);
  1008. }
  1009. if(isconnect && res == 200 && param->operation){
  1010. RETURN (mapsocket(param, conf.timeouts[CONNECTION_S]));
  1011. }
  1012. else if(isconnect){
  1013. ckeepalive = keepalive = 1;
  1014. }
  1015. else if(!hascontent && !param->chunked) {
  1016. RETURN(mapsocket(param, conf.timeouts[CONNECTION_S]));
  1017. }
  1018. contentlength64 = 0;
  1019. REQUESTEND:
  1020. if((!ckeepalive || !keepalive) && param->remsock != INVALID_SOCKET){
  1021. so._shutdown(param->remsock, SHUT_RDWR);
  1022. so._closesocket(param->remsock);
  1023. param->remsock = INVALID_SOCKET;
  1024. RETURN(0);
  1025. }
  1026. if(param->transparent && (!ckeepalive || !keepalive)) {RETURN (0);}
  1027. logurl(param, (char *)buf, (char *)req, ftp);
  1028. param->status = 0;
  1029. }
  1030. CLEANRET:
  1031. if(param->res != 555 && param->res && param->clisock != INVALID_SOCKET && (param->res < 90 || param->res >=800 || param->res == 100 ||(param->res > 500 && param->res< 800))) {
  1032. if((param->res>=509 && param->res < 517) || param->res > 900) while( (i = sockgetlinebuf(param, CLIENT, buf, BUFSIZE - 1, '\n', conf.timeouts[STRING_S])) > 2);
  1033. if(param->res == 10) {
  1034. socksend(param->clisock, (unsigned char *)proxy_stringtable[2], (int)strlen(proxy_stringtable[2]), conf.timeouts[STRING_S]);
  1035. }
  1036. else if (res == 700 || res == 701){
  1037. socksend(param->clisock, (unsigned char *)proxy_stringtable[16], (int)strlen(proxy_stringtable[16]), conf.timeouts[STRING_S]);
  1038. socksend(param->clisock, (unsigned char *)ftpbuf, inftpbuf, conf.timeouts[STRING_S]);
  1039. }
  1040. else if(param->res == 100 || (param->res >10 && param->res < 20) || (param->res >701 && param->res <= 705)) {
  1041. socksend(param->clisock, (unsigned char *)proxy_stringtable[1], (int)strlen(proxy_stringtable[1]), conf.timeouts[STRING_S]);
  1042. }
  1043. else if(param->res >=20 && param->res < 30) {
  1044. socksend(param->clisock, (unsigned char *)proxy_stringtable[6], (int)strlen(proxy_stringtable[6]), conf.timeouts[STRING_S]);
  1045. }
  1046. else if(param->res >=30 && param->res < 80) {
  1047. socksend(param->clisock, (unsigned char *)proxy_stringtable[5], (int)strlen(proxy_stringtable[5]), conf.timeouts[STRING_S]);
  1048. }
  1049. else if(param->res == 1 || (!param->srv->needuser && param->res < 10)) {
  1050. socksend(param->clisock, (unsigned char *)proxy_stringtable[11], (int)strlen(proxy_stringtable[11]), conf.timeouts[STRING_S]);
  1051. }
  1052. else if(param->res < 10) {
  1053. socksend(param->clisock, (unsigned char *)proxy_stringtable[param->srv->usentlm?12:7], (int)strlen(proxy_stringtable[param->srv->usentlm?12:7]), conf.timeouts[STRING_S]);
  1054. }
  1055. else if(param->res == 999) {
  1056. socksend(param->clisock, (unsigned char *)proxy_stringtable[4], (int)strlen(proxy_stringtable[4]), conf.timeouts[STRING_S]);
  1057. }
  1058. else if(param->res == 519) {
  1059. socksend(param->clisock, (unsigned char *)proxy_stringtable[3], (int)strlen(proxy_stringtable[3]), conf.timeouts[STRING_S]);
  1060. }
  1061. else if(param->res == 517) {
  1062. socksend(param->clisock, (unsigned char *)proxy_stringtable[15], (int)strlen(proxy_stringtable[15]), conf.timeouts[STRING_S]);
  1063. }
  1064. else if(param->res == 780) {
  1065. socksend(param->clisock, (unsigned char *)proxy_stringtable[10], (int)strlen(proxy_stringtable[10]), conf.timeouts[STRING_S]);
  1066. }
  1067. else if(param->res >= 511 && param->res<=516){
  1068. socksend(param->clisock, (unsigned char *)proxy_stringtable[0], (int)strlen(proxy_stringtable[0]), conf.timeouts[STRING_S]);
  1069. }
  1070. }
  1071. logurl(param, (char *)buf, (char *)req, ftp);
  1072. if(req)myfree(req);
  1073. if(buf)myfree(buf);
  1074. if(ftpbase)myfree(ftpbase);
  1075. freeparam(param);
  1076. return (NULL);
  1077. }
  1078. #ifdef WITHMAIN
  1079. struct proxydef childdef = {
  1080. proxychild,
  1081. 3128,
  1082. 0,
  1083. S_PROXY,
  1084. "-a - anonymous proxy\r\n"
  1085. "-a1 - anonymous proxy with random client IP spoofing\r\n"
  1086. };
  1087. #include "proxymain.c"
  1088. #endif