proxy.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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. if(param->ndatfilterscli > 0 && contentlength64 > 0){
  494. uint64_t newlen64;
  495. newlen64 = sockfillbuffcli(param, (unsigned long)contentlength64, CONNECTION_S);
  496. if(newlen64 == contentlength64) {
  497. action = handledatfltcli(param, &param->clibuf, (int *)&param->clibufsize, 0, (int *)&param->cliinbuf);
  498. if(action == HANDLED){
  499. RETURN(0);
  500. }
  501. if(action != PASS) RETURN(517);
  502. contentlength64 = param->cliinbuf;
  503. param->ndatfilterscli = 0;
  504. }
  505. sprintf((char*)buf+strlen((char *)buf), "Content-Length: %"PRINTF_INT64_MODIFIER"u\r\n", contentlength64);
  506. }
  507. #endif
  508. if(param->srv->needuser > 1 && !param->username) {RETURN(4);}
  509. if((res = (*param->srv->authfunc)(param))) {RETURN(res);}
  510. if(ftp && param->redirtype != R_HTTP){
  511. SOCKET s;
  512. int mode = 0;
  513. int i=0;
  514. inftpbuf = 0;
  515. if(!ckeepalive){
  516. inftpbuf = FTPBUFSIZE - 20;
  517. res = ftplogin(param, ftpbuf, &inftpbuf);
  518. if(res){
  519. RETURN(res);
  520. }
  521. }
  522. ckeepalive = 1;
  523. if(ftpbase) myfree(ftpbase);
  524. ftpbase = NULL;
  525. if(!(sp = (unsigned char *)strchr((char *)ss, ' '))){RETURN(799);}
  526. *sp = 0;
  527. decodeurl(ss, 0);
  528. i = (int)strlen((char *)ss);
  529. if(!(ftpbase = myalloc(i+2))){RETURN(21);}
  530. memcpy(ftpbase, ss, i);
  531. if(ftpbase[i-1] != '/') ftpbase[i++] = '/';
  532. ftpbase[i] = 0;
  533. memcpy(buf, "<pre><hr>\n", 10);
  534. inbuf = 10;
  535. if(inftpbuf) {
  536. memcpy(buf+inbuf, ftpbuf, inftpbuf);
  537. inbuf += inftpbuf;
  538. memcpy(buf+inbuf, "<hr>", 4);
  539. inbuf += 4;
  540. }
  541. if(ftpbase[1] != 0){
  542. memcpy(buf+inbuf, "[<A HREF=\"..\">..</A>]\n", 22);
  543. inbuf += 22;
  544. }
  545. inftpbuf = FTPBUFSIZE - (20 + inftpbuf);
  546. res = ftpcd(param, ftpbase, ftpbuf, &inftpbuf);
  547. if(res){
  548. res = ftptype(param, (unsigned char *)"I");
  549. if(res)RETURN(res);
  550. ftpbase[--i] = 0;
  551. ftps = ftpcommand(param, param->operation == FTP_PUT? (unsigned char *)"PUT" : (unsigned char *)"RETR", ftpbase);
  552. }
  553. else {
  554. if(inftpbuf){
  555. memcpy(buf+inbuf, ftpbuf, inftpbuf);
  556. inbuf += inftpbuf;
  557. memcpy(buf+inbuf, "<hr>", 4);
  558. inbuf += 4;
  559. }
  560. ftps = ftpcommand(param, (unsigned char *)"LIST", NULL);
  561. mode = 1;
  562. }
  563. if(ftps == INVALID_SOCKET){RETURN(780);}
  564. if(!mode){
  565. socksend(param->clisock, (unsigned char *)proxy_stringtable[8], (int)strlen(proxy_stringtable[8]), conf.timeouts[STRING_S]);
  566. s = param->remsock;
  567. param->remsock = ftps;
  568. if((param->operation == FTP_PUT) && (contentlength64 > 0)) param->waitclient64 = contentlength64;
  569. res = mapsocket(param, conf.timeouts[CONNECTION_L]);
  570. if (res == 99) res = 0;
  571. so._closesocket(ftps);
  572. ftps = INVALID_SOCKET;
  573. param->remsock = s;
  574. }
  575. else {
  576. int headsent = 0;
  577. int gotres = -1;
  578. s = param->remsock;
  579. if(param->srvoffset < param->srvinbuf){
  580. gotres = ftpres(param, buf+inbuf, bufsize-(inbuf+100));
  581. if(gotres) inbuf= (int)strlen((char *)buf);
  582. }
  583. param->remsock = ftps;
  584. if(gotres <= 0) for(; (res = sockgetlinebuf(param, SERVER, (unsigned char *)ftpbuf, FTPBUFSIZE - 20, '\n', conf.timeouts[STRING_S])) > 0; i++){
  585. int isdir = 0;
  586. int islink = 0;
  587. int filetoken =-1;
  588. int sizetoken =-1;
  589. int modetoken =-1;
  590. int datetoken =-1;
  591. int spaces = 1;
  592. unsigned char * tokens[10];
  593. unsigned wordlen [10];
  594. unsigned char j=0;
  595. int space = 1;
  596. ftpbuf[res] = 0;
  597. if(!i && ftpbuf[0] == 't' && ftpbuf[1] == 'o' && ftpbuf[2] == 't'){
  598. mode = 2;
  599. continue;
  600. }
  601. if(!isnumber(*ftpbuf) && mode == 1) mode = 2;
  602. for(sb=(unsigned char *)ftpbuf; *sb; sb++){
  603. if(!space && isspace(*sb)){
  604. space = 1;
  605. wordlen[j]=(unsigned)(sb-tokens[j]);
  606. j++;
  607. }
  608. if(space && !isspace(*sb)){
  609. space = 0;
  610. tokens[j] = sb;
  611. if(j==8)break;
  612. }
  613. }
  614. if(mode == 1){
  615. if(j < 4) continue;
  616. if(!(isdir = !memcmp(tokens[2], "<DIR>", wordlen[2])) && !isnumber(*tokens[2])){
  617. continue;
  618. }
  619. datetoken = 0;
  620. wordlen[datetoken] = ((unsigned)(tokens[1] - tokens[0])) + wordlen[1];
  621. sizetoken = 2;
  622. filetoken = 3;
  623. spaces = 10;
  624. }
  625. else {
  626. if(j < 8 || wordlen[0]!=10) continue;
  627. if(j < 8 || !isnumber(*tokens[4])) mode = 3;
  628. if(*tokens[0] == 'd') isdir = 1;
  629. if(*tokens[0] == 'l') islink = 1;
  630. modetoken = 0;
  631. sizetoken = (mode == 2)? 4:3;
  632. filetoken = (mode == 2)? 8:7;
  633. datetoken = (mode == 2)? 5:4;
  634. tokens[filetoken] = tokens[filetoken-1];
  635. while(*tokens[filetoken] && !isspace(*tokens[filetoken]))tokens[filetoken]++;
  636. if(*tokens[filetoken]){
  637. tokens[filetoken]++;
  638. }
  639. wordlen[datetoken] = (unsigned)(tokens[filetoken] - tokens[datetoken]);
  640. wordlen[filetoken] = (unsigned)strlen((char *)tokens[filetoken]);
  641. }
  642. if(modetoken >= 0) memcpy(buf+inbuf, tokens[modetoken], 11);
  643. else memcpy(buf+inbuf, "---------- ", 11);
  644. inbuf += 11;
  645. if((int) wordlen[datetoken]+256 > bufsize-inbuf) continue;
  646. memcpy(buf+inbuf, tokens[datetoken], wordlen[datetoken]);
  647. inbuf += wordlen[datetoken];
  648. if(isdir){
  649. memcpy(buf+inbuf, " DIR", 10);
  650. inbuf+=10;
  651. }
  652. else if(islink){
  653. memcpy(buf+inbuf, " LINK", 10);
  654. inbuf+=10;
  655. }
  656. else{
  657. unsigned k;
  658. if(wordlen[sizetoken]>10) wordlen[sizetoken] = 10;
  659. for(k=10; k > wordlen[sizetoken]; k--){
  660. buf[inbuf++] = ' ';
  661. }
  662. memcpy(buf+inbuf, tokens[sizetoken], wordlen[sizetoken]);
  663. inbuf+=wordlen[sizetoken];
  664. }
  665. memcpy(buf+inbuf, " <A HREF=\"", 10);
  666. inbuf+=10;
  667. sb = NULL;
  668. if(islink) sb = (unsigned char *)strstr((char *)tokens[filetoken], " -> ");
  669. if(sb) sb+=4;
  670. else sb=tokens[filetoken];
  671. if(*sb != '/' && ftpbase)file2url(ftpbase, buf, bufsize, (int *)&inbuf, 1);
  672. file2url(sb, buf, bufsize, (int *)&inbuf, 0);
  673. if(isdir)buf[inbuf++] = '/';
  674. memcpy(buf+inbuf, "\">", 2);
  675. inbuf+=2;
  676. for(sb=tokens[filetoken]; *sb; sb++){
  677. if((bufsize - inbuf)<16)break;
  678. if(*sb == '<'){
  679. memcpy(buf+inbuf, "&lt;", 4);
  680. inbuf+=4;
  681. }
  682. else if(*sb == '>'){
  683. memcpy(buf+inbuf, "&gt;", 4);
  684. inbuf+=4;
  685. }
  686. else if(*sb == '\r' || *sb=='\n'){
  687. continue;
  688. }
  689. else if(islink && sb[0] == ' ' && sb[1] == '-'
  690. && sb[2] == '>'){
  691. memcpy(buf+inbuf, "</A> ", 5);
  692. inbuf+=5;
  693. }
  694. else buf[inbuf++]=*sb;
  695. }
  696. if(islink!=2){
  697. memcpy(buf+inbuf, "</A>", 4);
  698. inbuf+=4;
  699. }
  700. buf[inbuf++] = '\n';
  701. if((bufsize - inbuf) < LINESIZE){
  702. if (bufsize > 20000){
  703. if(!headsent++){
  704. socksend(param->clisock, (unsigned char *)proxy_stringtable[9], (int)strlen(proxy_stringtable[9]), conf.timeouts[STRING_S]);
  705. }
  706. if((unsigned)socksend(param->clisock, buf, inbuf, conf.timeouts[STRING_S]) != inbuf){
  707. RETURN(781);
  708. }
  709. inbuf = 0;
  710. }
  711. else {
  712. if(!(newbuf = myrealloc(buf, bufsize + BUFSIZE))){RETURN (21);}
  713. buf = newbuf;
  714. bufsize += BUFSIZE;
  715. }
  716. }
  717. }
  718. memcpy(buf+inbuf, "<hr>", 4);
  719. inbuf += 4;
  720. so._closesocket(ftps);
  721. ftps = INVALID_SOCKET;
  722. param->remsock = s;
  723. if(inbuf){
  724. buf[inbuf] = 0;
  725. if(gotres < 0 ) res = ftpres(param, buf+inbuf, bufsize-inbuf);
  726. else res = gotres;
  727. inbuf = (int)strlen((char *)buf);
  728. if(!headsent){
  729. sprintf(ftpbuf,
  730. "HTTP/1.0 200 OK\r\n"
  731. "Content-Type: text/html\r\n"
  732. "Connection: keep-alive\r\n"
  733. "Content-Length: %d\r\n\r\n",
  734. inbuf);
  735. socksend(param->clisock, (unsigned char *)ftpbuf, (int)strlen(ftpbuf), conf.timeouts[STRING_S]);
  736. }
  737. socksend(param->clisock, buf, inbuf, conf.timeouts[STRING_S]);
  738. if(res){RETURN(res);}
  739. if(!headsent)goto REQUESTEND;
  740. }
  741. RETURN(0);
  742. }
  743. RETURN(res);
  744. }
  745. if(isconnect && param->redirtype != R_HTTP) {
  746. socksend(param->clisock, (unsigned char *)proxy_stringtable[8], (int)strlen(proxy_stringtable[8]), conf.timeouts[STRING_S]);
  747. if(param->redirectfunc) {
  748. if(req)myfree(req);
  749. if(buf)myfree(buf);
  750. return (*param->redirectfunc)(param);
  751. }
  752. param->res = mapsocket(param, conf.timeouts[CONNECTION_L]);
  753. RETURN(param->res);
  754. }
  755. if(!req || param->redirtype != R_HTTP) {
  756. reqlen = 0;
  757. }
  758. else {
  759. #ifdef TCP_CORK
  760. int opt = 1;
  761. so._setsockopt(param->remsock, IPPROTO_TCP, TCP_CORK, (unsigned char *)&opt, sizeof(int));
  762. #endif
  763. redirect = 1;
  764. res = (int)strlen((char *)req);
  765. if(socksend(param->remsock, req , res, conf.timeouts[STRING_L]) != res) {
  766. RETURN(518);
  767. }
  768. param->statscli64 += res;
  769. param->nwrites++;
  770. }
  771. inbuf = 0;
  772. #ifndef ANONYMOUS
  773. if(!anonymous){
  774. int len = strlen((char *)buf);
  775. len += sprintf((char*)buf + len, "Forwarded: for=");
  776. if(*SAFAMILY(&param->sincr) == AF_INET6) len += sprintf((char*)buf + len, "\"[");
  777. len += myinet_ntop(*SAFAMILY(&param->sincr), SAADDR(&param->sincr), (char *)buf + len, 128);
  778. if(*SAFAMILY(&param->sincr) == AF_INET6) len += sprintf((char*)buf + len, "]:%d\";by=", (int)ntohs(*SAPORT(&param->sincr)));
  779. else len += sprintf((char*)buf + len, ":%d;by=", (int)ntohs(*SAPORT(&param->sincr)));
  780. gethostname((char *)(buf + len), 256);
  781. sprintf((char*)buf+strlen((char *)buf), ":%d\r\n", (int)ntohs(*SAPORT(&param->sincl)));
  782. }
  783. else if(anonymous>1){
  784. sprintf((char*)buf+strlen((char *)buf), "Via: 1.1 ");
  785. gethostname((char *)(buf+strlen((char *)buf)), 256);
  786. 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 *)"");
  787. if(anonymous != 2)myinet_ntop(*SAFAMILY(&param->sincr), SAADDR(&param->sincr), (char *)buf + strlen((char *)buf), 128);
  788. else {
  789. unsigned long tmp;
  790. tmp = ((unsigned long)myrand(param, sizeof(struct clientparam))<<16)^(unsigned long)rand();
  791. myinet_ntop(AF_INET, &tmp, (char *)buf + strlen((char *)buf), 64);
  792. }
  793. sprintf((char*)buf+strlen((char *)buf), "\r\n");
  794. }
  795. #endif
  796. if(keepalive <= 1) {
  797. sprintf((char*)buf+strlen((char *)buf), "Connection: %s\r\n", keepalive? "keep-alive":"close");
  798. }
  799. if(param->extusername){
  800. sprintf((char*)buf + strlen((char *)buf), "%s: Basic ", (redirect)?"Proxy-Authorization":"Authorization");
  801. sprintf((char*)username, "%.128s:%.128s", param->extusername, param->extpassword?param->extpassword:(unsigned char*)"");
  802. en64(username, buf+strlen((char *)buf), (int)strlen((char *)username));
  803. sprintf((char*)buf + strlen((char *)buf), "\r\n");
  804. }
  805. sprintf((char*)buf+strlen((char *)buf), "\r\n");
  806. if ((res = socksend(param->remsock, buf+reqlen, (int)strlen((char *)buf+reqlen), conf.timeouts[STRING_S])) != (int)strlen((char *)buf+reqlen)) {
  807. RETURN(518);
  808. }
  809. #ifdef TCP_CORK
  810. {
  811. int opt = 0;
  812. so._setsockopt(param->remsock, IPPROTO_TCP, TCP_CORK, (unsigned char *)&opt, sizeof(int));
  813. }
  814. #endif
  815. param->statscli64 += res;
  816. param->nwrites++;
  817. if(param->bandlimfunc) {
  818. sleeptime = param->bandlimfunc(param, 0, (int)strlen((char *)buf));
  819. }
  820. if(contentlength64 > 0){
  821. param->nolongdatfilter = 0;
  822. param->waitclient64 = contentlength64;
  823. res = mapsocket(param, conf.timeouts[CONNECTION_S]);
  824. param->waitclient64 = 0;
  825. if(res != 99) {
  826. RETURN(res);
  827. }
  828. }
  829. contentlength64 = 0;
  830. inbuf = 0;
  831. ckeepalive = keepalive;
  832. res = 0;
  833. authenticate = 0;
  834. param->chunked = 0;
  835. hascontent = 0;
  836. while( (i = sockgetlinebuf(param, SERVER, buf + inbuf, LINESIZE - 1, '\n', conf.timeouts[(res)?STRING_S:STRING_L])) > 2) {
  837. if(!res && i>9)param->status = res = atoi((char *)buf + inbuf + 9);
  838. if(((i >= 25 && !strncasecmp((char *)(buf+inbuf), "proxy-connection:", 17))
  839. ||
  840. (i> 16 && !strncasecmp((char *)(buf+inbuf), "connection:", 11))
  841. )){
  842. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  843. if(!sb)continue;
  844. ++sb;
  845. while(isspace(*sb))sb++;
  846. if(strncasecmp((char *)sb,"keep-alive", 10))ckeepalive = 0;
  847. if(!param->srv->transparent && res >= 200)continue;
  848. }
  849. else if(i> 6 && !param->srv->transparent && (!strncasecmp((char *)(buf+inbuf), "proxy-", 6))){
  850. continue;
  851. }
  852. else if(i> 6 && (!strncasecmp((char *)(buf+inbuf), "www-authenticate", 16))){
  853. authenticate = 1;
  854. }
  855. else if(i > 15 && (!strncasecmp((char *)(buf+inbuf), "content-length", 14))){
  856. buf[inbuf+i]=0;
  857. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  858. if(!sb)continue;
  859. ++sb;
  860. while(isspace(*sb))sb++;
  861. sscanf((char *)sb, "%"PRINTF_INT64_MODIFIER"u", &contentlength64);
  862. hascontent = 1;
  863. if(param->unsafefilter && param->ndatfilterssrv > 0) {
  864. hascontent = 2;
  865. continue;
  866. }
  867. if(param->maxtrafin64 && (param->maxtrafin64 < param->statssrv64 || contentlength64 + param->statssrv64 > param->maxtrafin64)){
  868. RETURN(10);
  869. }
  870. }
  871. else if(i>25 && (!strncasecmp((char *)(buf+inbuf), "transfer-encoding", 17))){
  872. buf[inbuf+i]=0;
  873. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  874. if(!sb)continue;
  875. ++sb;
  876. while(isspace(*sb))sb++;
  877. if(!strncasecmp((char *)sb, "chunked", 7)){
  878. param->chunked = 1;
  879. }
  880. }
  881. inbuf += i;
  882. if((bufsize - inbuf) < LINESIZE){
  883. if (bufsize > 20000){
  884. RETURN (516);
  885. }
  886. if(!(newbuf = myrealloc(buf, bufsize + BUFSIZE))){RETURN (21);}
  887. buf = newbuf;
  888. bufsize += BUFSIZE;
  889. }
  890. }
  891. if(res < 200 || res > 499) {
  892. ckeepalive = 0;
  893. }
  894. if((res == 304 || res == 204) && !hascontent){
  895. hascontent = 1;
  896. contentlength64 = 0;
  897. }
  898. if(param->bandlimfunc) {
  899. int st1;
  900. st1 = (*param->bandlimfunc)(param, inbuf, 0);
  901. if(st1 > sleeptime) sleeptime = st1;
  902. if(sleeptime > 0){
  903. /* if(sleeptime > 30) sleeptime = 30; */
  904. usleep(sleeptime * SLEEPTIME);
  905. }
  906. }
  907. buf[inbuf] = 0;
  908. if(inbuf < 9) {RETURN (522);}
  909. #ifndef WITHMAIN
  910. action = handlehdrfilterssrv(param, &buf, &bufsize, 0, &inbuf);
  911. if(action == HANDLED){
  912. RETURN(0);
  913. }
  914. if(action != PASS) RETURN(517);
  915. param->nolongdatfilter = 0;
  916. if (conf.filtermaxsize && contentlength64 > (uint64_t)conf.filtermaxsize) {
  917. param->nolongdatfilter = 1;
  918. }
  919. else if(param->unsafefilter && param->ndatfilterssrv > 0 && contentlength64 > 0 && param->operation != HTTP_HEAD && res != 204 && res != 304){
  920. uint64_t newlen;
  921. newlen = (uint64_t)sockfillbuffsrv(param, (unsigned long) contentlength64, CONNECTION_S);
  922. if(newlen == contentlength64) {
  923. action = handledatfltsrv(param, &param->srvbuf, (int *)&param->srvbufsize, 0, (int *)&param->srvinbuf);
  924. param->nolongdatfilter = 1;
  925. if(action == HANDLED){
  926. RETURN(0);
  927. }
  928. if(action != PASS) RETURN(517);
  929. contentlength64 = param->srvinbuf;
  930. sprintf((char*)buf+strlen((char *)buf), "Content-Length: %"PRINTF_INT64_MODIFIER"u\r\n", contentlength64);
  931. hascontent = 1;
  932. }
  933. }
  934. if (contentlength64 > 0 && hascontent != 1) ckeepalive = 0;
  935. #else
  936. #endif
  937. if(!isconnect || param->operation){
  938. if(authenticate && !param->transparent) sprintf((char*)buf+strlen((char *)buf),
  939. "Proxy-support: Session-Based-Authentication\r\n"
  940. "Connection: Proxy-support\r\n"
  941. );
  942. if(!param->srv->transparent && res>=200){
  943. if(ckeepalive <= 1) sprintf((char*)buf+strlen((char *)buf), "Connection: %s\r\n",
  944. (hascontent && ckeepalive)?"keep-alive":"close");
  945. }
  946. sprintf((char*)buf + strlen((char *)buf), "\r\n");
  947. if((socksend(param->clisock, buf, (int)strlen((char *)buf), conf.timeouts[STRING_S])) != (int)strlen((char *)buf)) {
  948. RETURN(521);
  949. }
  950. }
  951. if((param->chunked || contentlength64 > 0) && param->operation != HTTP_HEAD && res != 204 && res != 304) {
  952. do {
  953. if(param->chunked){
  954. unsigned char smallbuf[32];
  955. while ((i = sockgetlinebuf(param, SERVER, smallbuf, 30, '\n', conf.timeouts[STRING_S])) == 2) {
  956. if (socksend(param->clisock, smallbuf, i, conf.timeouts[STRING_S]) != i){
  957. RETURN(533);
  958. }
  959. if(param->chunked == 2) break;
  960. }
  961. if(i<3) {
  962. keepalive = 0;
  963. break;
  964. }
  965. if (socksend(param->clisock, smallbuf, i, conf.timeouts[STRING_S]) != i){
  966. RETURN(535);
  967. }
  968. if(param->chunked == 2) {
  969. if((i = sockgetlinebuf(param, SERVER, smallbuf, 30, '\n', conf.timeouts[STRING_S])) != 2) RETURN(534);
  970. if (socksend(param->clisock, smallbuf, i, conf.timeouts[STRING_S]) != i){
  971. RETURN(533);
  972. }
  973. break;
  974. }
  975. smallbuf[i] = 0;
  976. contentlength64 = 0;
  977. sscanf((char *)smallbuf, "%"PRINTF_INT64_MODIFIER"x", &contentlength64);
  978. if(contentlength64 == 0) {
  979. param->chunked = 2;
  980. }
  981. }
  982. if(param->chunked != 2){
  983. param->waitserver64 = contentlength64;
  984. if((res = mapsocket(param, conf.timeouts[CONNECTION_S])) != 98){
  985. RETURN(res);
  986. }
  987. param->waitserver64 = 0;
  988. }
  989. } while(param->chunked);
  990. }
  991. if(isconnect && res == 200 && param->operation){
  992. RETURN (mapsocket(param, conf.timeouts[CONNECTION_S]));
  993. }
  994. else if(isconnect){
  995. ckeepalive = keepalive = 1;
  996. }
  997. else if(!hascontent && !param->chunked) {
  998. RETURN(mapsocket(param, conf.timeouts[CONNECTION_S]));
  999. }
  1000. contentlength64 = 0;
  1001. REQUESTEND:
  1002. if((!ckeepalive || !keepalive) && param->remsock != INVALID_SOCKET){
  1003. so._shutdown(param->remsock, SHUT_RDWR);
  1004. so._closesocket(param->remsock);
  1005. param->remsock = INVALID_SOCKET;
  1006. RETURN(0);
  1007. }
  1008. if(param->transparent && (!ckeepalive || !keepalive)) {RETURN (0);}
  1009. logurl(param, (char *)buf, (char *)req, ftp);
  1010. param->status = 0;
  1011. }
  1012. CLEANRET:
  1013. 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))) {
  1014. if((param->res>=509 && param->res < 517) || param->res > 900) while( (i = sockgetlinebuf(param, CLIENT, buf, BUFSIZE - 1, '\n', conf.timeouts[STRING_S])) > 2);
  1015. if(param->res == 10) {
  1016. socksend(param->clisock, (unsigned char *)proxy_stringtable[2], (int)strlen(proxy_stringtable[2]), conf.timeouts[STRING_S]);
  1017. }
  1018. else if (res == 700 || res == 701){
  1019. socksend(param->clisock, (unsigned char *)proxy_stringtable[16], (int)strlen(proxy_stringtable[16]), conf.timeouts[STRING_S]);
  1020. socksend(param->clisock, (unsigned char *)ftpbuf, inftpbuf, conf.timeouts[STRING_S]);
  1021. }
  1022. else if(param->res == 100 || (param->res >10 && param->res < 20) || (param->res >701 && param->res <= 705)) {
  1023. socksend(param->clisock, (unsigned char *)proxy_stringtable[1], (int)strlen(proxy_stringtable[1]), conf.timeouts[STRING_S]);
  1024. }
  1025. else if(param->res >=20 && param->res < 30) {
  1026. socksend(param->clisock, (unsigned char *)proxy_stringtable[6], (int)strlen(proxy_stringtable[6]), conf.timeouts[STRING_S]);
  1027. }
  1028. else if(param->res >=30 && param->res < 80) {
  1029. socksend(param->clisock, (unsigned char *)proxy_stringtable[5], (int)strlen(proxy_stringtable[5]), conf.timeouts[STRING_S]);
  1030. }
  1031. else if(param->res == 1 || (!param->srv->needuser && param->res < 10)) {
  1032. socksend(param->clisock, (unsigned char *)proxy_stringtable[11], (int)strlen(proxy_stringtable[11]), conf.timeouts[STRING_S]);
  1033. }
  1034. else if(param->res < 10) {
  1035. 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]);
  1036. }
  1037. else if(param->res == 999) {
  1038. socksend(param->clisock, (unsigned char *)proxy_stringtable[4], (int)strlen(proxy_stringtable[4]), conf.timeouts[STRING_S]);
  1039. }
  1040. else if(param->res == 519) {
  1041. socksend(param->clisock, (unsigned char *)proxy_stringtable[3], (int)strlen(proxy_stringtable[3]), conf.timeouts[STRING_S]);
  1042. }
  1043. else if(param->res == 517) {
  1044. socksend(param->clisock, (unsigned char *)proxy_stringtable[15], (int)strlen(proxy_stringtable[15]), conf.timeouts[STRING_S]);
  1045. }
  1046. else if(param->res == 780) {
  1047. socksend(param->clisock, (unsigned char *)proxy_stringtable[10], (int)strlen(proxy_stringtable[10]), conf.timeouts[STRING_S]);
  1048. }
  1049. else if(param->res >= 511 && param->res<=516){
  1050. socksend(param->clisock, (unsigned char *)proxy_stringtable[0], (int)strlen(proxy_stringtable[0]), conf.timeouts[STRING_S]);
  1051. }
  1052. }
  1053. logurl(param, (char *)buf, (char *)req, ftp);
  1054. if(req)myfree(req);
  1055. if(buf)myfree(buf);
  1056. if(ftpbase)myfree(ftpbase);
  1057. freeparam(param);
  1058. return (NULL);
  1059. }
  1060. #ifdef WITHMAIN
  1061. struct proxydef childdef = {
  1062. proxychild,
  1063. 3128,
  1064. 0,
  1065. S_PROXY,
  1066. "-a - anonymous proxy\r\n"
  1067. "-a1 - anonymous proxy with random client IP spoofing\r\n"
  1068. };
  1069. #include "proxymain.c"
  1070. #include "log.c"
  1071. #endif