proxy.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2008 by ZARAZA <3APA3A@security.nnov.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. "Proxy-Connection: close\r\n"
  11. "Content-type: text/html; charset=us-ascii\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. "Proxy-Connection: close\r\n"
  17. "Content-type: text/html; charset=us-ascii\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. "Proxy-Connection: close\r\n"
  23. "Content-type: text/html; charset=us-ascii\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. "Proxy-Connection: close\r\n"
  29. "Content-type: text/html; charset=us-ascii\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. "Proxy-Connection: close\r\n"
  35. "Content-type: text/html; charset=us-ascii\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. "Proxy-Connection: close\r\n"
  41. "Content-type: text/html; charset=us-ascii\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. "Proxy-Connection: close\r\n"
  47. "Content-type: text/html; charset=us-ascii\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. "Proxy-Connection: close\r\n"
  54. "Content-type: text/html; charset=us-ascii\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. "Proxy-Connection: close\r\n"
  63. "Content-type: text/html; charset=us-ascii\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. "Proxy-Connection: close\r\n"
  69. "Content-type: text/html; charset=us-ascii\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. "Proxy-Connection: close\r\n"
  79. "Content-type: text/html; charset=us-ascii\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. "Proxy-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. "Proxy-Connection: close\r\n"
  89. "Content-type: text/html; charset=us-ascii\r\n"
  90. "\r\n"
  91. "<pre>",
  92. /* 15*/ "HTTP/1.0 503 Service Unavailable\r\n"
  93. "Proxy-Connection: close\r\n"
  94. "Content-type: text/html; charset=us-ascii\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. "Proxy-Connection: close\r\n"
  101. "Content-type: text/html; charset=us-ascii\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 BUFSIZE 8192
  110. #define LINESIZE 4096
  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->srv->logfunc)(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->singlepacket;
  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. }
  230. }
  231. i = sockgetlinebuf(param, CLIENT, buf, LINESIZE - 1, '\n', conf.timeouts[STRING_L]);
  232. if(i<=0) {
  233. RETURN((keepalive)?555:(i)?507:508);
  234. }
  235. if (i==2 && buf[0]=='\r' && buf[1]=='\n') continue;
  236. buf[i] = 0;
  237. if(req) {
  238. if(!param->transparent && !param->srv->transparent && param->redirtype != R_HTTP && (i<=prefix || strncasecmp((char *)buf, (char *)req, prefix))){
  239. ckeepalive = 0;
  240. if(param->remsock != INVALID_SOCKET){
  241. so._shutdown(param->remsock, SHUT_RDWR);
  242. so._closesocket(param->remsock);
  243. }
  244. param->remsock = INVALID_SOCKET;
  245. param->redirected = 0;
  246. param->redirtype = 0;
  247. }
  248. myfree(req);
  249. }
  250. req = (unsigned char *)mystrdup((char *)buf);
  251. if(!req){RETURN(510);}
  252. if(i<10) {
  253. RETURN(511);
  254. }
  255. if(buf[i-3] == '1') keepalive = 2;
  256. if((isconnect = !strncasecmp((char *)buf, "CONNECT", 7))) keepalive = 2;
  257. if ((sb=(unsigned char *)(unsigned char *)strchr((char *)buf, ' ')) == NULL) {RETURN(512);}
  258. ss = ++sb;
  259. if(!isconnect) {
  260. if (!strncasecmp((char *)sb, "http://", 7)) {
  261. sb += 7;
  262. }
  263. else if (!strncasecmp((char *)sb, "ftp://", 6)) {
  264. ftp = 1;
  265. sb += 6;
  266. }
  267. else if(*sb == '/') {
  268. param->transparent = 1;
  269. }
  270. else {
  271. RETURN (513);
  272. }
  273. }
  274. else {
  275. if ((se=(unsigned char *)(unsigned char *)strchr((char *)sb, ' ')) == NULL || sb==se) {RETURN (514);}
  276. *se = 0;
  277. }
  278. if(!param->transparent || isconnect) {
  279. if(!isconnect) {
  280. if ((se=(unsigned char *)(unsigned char *)strchr((char *)sb, '/')) == NULL
  281. || sb==se
  282. || !(sg=(unsigned char *)strchr((char *)sb, ' '))) {RETURN (515);}
  283. if(se > sg) se=sg;
  284. *se = 0;
  285. }
  286. prefix = (int)(se - buf);
  287. su = (unsigned char*)strrchr((char *)sb, '@');
  288. if(su) {
  289. su = mystrdup(sb);
  290. decodeurl(su, 0);
  291. parseconnusername((char *)su, (struct clientparam *)param, 1, (unsigned short)((ftp)?21:80));
  292. myfree(su);
  293. }
  294. else parsehostname((char *)sb, (struct clientparam *)param, (unsigned short)((ftp)? 21:80));
  295. if(!isconnect){
  296. if(se==sg)*se-- = ' ';
  297. *se = '/';
  298. memmove(ss, se, i - (se - sb) + 1);
  299. }
  300. }
  301. reqlen = i = (int)strlen((char *)buf);
  302. if(!strncasecmp((char *)buf, "CONNECT", 7))param->operation = HTTP_CONNECT;
  303. else if(!strncasecmp((char *)buf, "GET", 3))param->operation = (ftp)?FTP_GET:HTTP_GET;
  304. else if(!strncasecmp((char *)buf, "PUT", 3))param->operation = (ftp)?FTP_PUT:HTTP_PUT;
  305. else if(!strncasecmp((char *)buf, "POST", 4)||!strncasecmp((char *)buf, "BITS_POST", 9))param->operation = HTTP_POST;
  306. else if(!strncasecmp((char *)buf, "HEAD", 4))param->operation = HTTP_HEAD;
  307. else param->operation = HTTP_OTHER;
  308. do {
  309. buf[inbuf+i]=0;
  310. /*printf("Got: %s\n", buf+inbuf);*/
  311. #ifndef WITHMAIN
  312. if(i > 25 && !param->srv->transparent && (!strncasecmp((char *)(buf+inbuf), "proxy-authorization", 19))){
  313. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  314. if(!sb)continue;
  315. ++sb;
  316. while(isspace(*sb))sb++;
  317. if(!*sb) continue;
  318. if(!strncasecmp((char *)sb, "basic", 5)){
  319. sb+=5;
  320. while(isspace(*sb))sb++;
  321. i = de64(sb, username, 255);
  322. if(i<=0)continue;
  323. username[i] = 0;
  324. sb = (unsigned char *)strchr((char *)username, ':');
  325. if(sb){
  326. *sb = 0;
  327. if(param->password)myfree(param->password);
  328. param->password = (unsigned char *)mystrdup((char *)sb+1);
  329. param->pwtype = 0;
  330. }
  331. if(param->username)myfree(param->username);
  332. param->username = (unsigned char *)mystrdup((char *)username);
  333. continue;
  334. }
  335. #ifndef NOCRYPT
  336. if(param->srv->usentlm && !strncasecmp((char *)sb, "ntlm", 4)){
  337. sb+=4;
  338. while(isspace(*sb))sb++;
  339. i = de64(sb, username, 1023);
  340. if(i<=16)continue;
  341. username[i] = 0;
  342. if(strncasecmp((char *)username, "NTLMSSP", 8)) continue;
  343. if(username[8] == 1) {
  344. while( (i = sockgetlinebuf(param, CLIENT, buf, BUFSIZE - 1, '\n', conf.timeouts[STRING_S])) > 2){
  345. if(i> 15 && (!strncasecmp((char *)(buf), "content-length", 14))){
  346. buf[i]=0;
  347. sscanf((char *)buf + 15, "%"PRINTF_INT64_MODIFIER"u", &contentlength64);
  348. }
  349. }
  350. while( contentlength64 > 0 && (i = sockgetlinebuf(param, CLIENT, buf, (BUFSIZE < contentlength64)? BUFSIZE - 1:(int)contentlength64, '\n', conf.timeouts[STRING_S])) > 0){
  351. if ((uint64_t)i > contentlength64) break;
  352. contentlength64-=i;
  353. }
  354. contentlength64 = 0;
  355. if(param->password)myfree(param->password);
  356. param->password = myalloc(32);
  357. param->pwtype = 2;
  358. i = (int)strlen(proxy_stringtable[13]);
  359. memcpy(buf, proxy_stringtable[13], i);
  360. genchallenge(param, (char *)param->password, (char *)buf + i);
  361. memcpy(buf + strlen((char *)buf), "\r\n\r\n", 5);
  362. socksend(param->clisock, buf, (int)strlen((char *)buf), conf.timeouts[STRING_S]);
  363. ckeepalive = keepalive = 1;
  364. goto REQUESTEND;
  365. }
  366. if(username[8] == 3 && param->pwtype == 2 && i>=80) {
  367. unsigned offset, len;
  368. len = username[20] + (((unsigned)username[21]) << 8);
  369. offset = username[24] + (((unsigned)username[25]) << 8);
  370. if(len != 24 || len + offset > (unsigned)i) continue;
  371. memcpy(param->password + 8, username + offset, 24);
  372. len = username[36] + (((unsigned)username[37]) << 8);
  373. offset = username[40] + (((unsigned)username[41]) << 8);
  374. if(len> 255 || len + offset > (unsigned)i) continue;
  375. if(param->username) myfree(param->username);
  376. unicode2text((char *)username+offset, (char *)username+offset, (len>>1));
  377. param->username = (unsigned char *)mystrdup((char *)username+offset);
  378. }
  379. continue;
  380. }
  381. #endif
  382. }
  383. #endif
  384. if(!isconnect && (
  385. (i> 25 && !strncasecmp((char *)(buf+inbuf), "proxy-connection:", 17))
  386. ||
  387. (i> 16 && (!strncasecmp((char *)(buf+inbuf), "connection:", 11)))
  388. )){
  389. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  390. if(!sb)continue;
  391. ++sb;
  392. while(isspace(*sb))sb++;
  393. if(!strncasecmp((char *)sb,"keep-alive", 10))keepalive = 1;
  394. else keepalive = 0;
  395. continue;
  396. }
  397. if( i > 11 && !strncasecmp((char *)(buf+inbuf), "Expect: 100", 11)){
  398. keepalive = 1;
  399. socksend(param->clisock, (unsigned char *)proxy_stringtable[17], (int)strlen(proxy_stringtable[17]), conf.timeouts[STRING_S]);
  400. continue;
  401. }
  402. if(param->transparent && i > 6 && !strncasecmp((char *)buf + inbuf, "Host:", 5)){
  403. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  404. if(!sb)continue;
  405. ++sb;
  406. while(isspace(*sb))sb++;
  407. se = (unsigned char *)strchr((char *)sb, '\r');
  408. if(se) *se = 0;
  409. if(!param->hostname){
  410. parsehostname((char *)sb, param, 80);
  411. }
  412. newbuf = myalloc(strlen((char *)req) + strlen((char *)(buf+inbuf)) + 8);
  413. if(newbuf){
  414. sp = (unsigned char *)strchr((char *)req+1, '/');
  415. memcpy(newbuf, req, (sp - req));
  416. sprintf((char*)newbuf + (sp - req), "http://%s%s",sb,sp);
  417. myfree(req);
  418. req = newbuf;
  419. }
  420. if(se)*se = '\r';
  421. }
  422. if(ftp && i > 13 && (!strncasecmp((char *)(buf+inbuf), "authorization", 13))){
  423. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  424. if(!sb)continue;
  425. ++sb;
  426. while(isspace(*sb))sb++;
  427. if(!*sb) continue;
  428. if(!strncasecmp((char *)sb, "basic", 5)){
  429. sb+=5;
  430. while(isspace(*sb))sb++;
  431. i = de64(sb, username, 255);
  432. if(i<=0)continue;
  433. username[i] = 0;
  434. sb = (unsigned char *)strchr((char *)username, ':');
  435. if(sb){
  436. *sb = 0;
  437. if(param->extpassword)myfree(param->extpassword);
  438. param->extpassword = (unsigned char *)mystrdup((char *)sb+1);
  439. }
  440. if(param->extusername)myfree(param->extusername);
  441. param->extusername = (unsigned char *)mystrdup((char *)username);
  442. continue;
  443. }
  444. }
  445. if(i> 15 && (!strncasecmp((char *)(buf+inbuf), "content-length", 14))){
  446. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  447. if(!sb)continue;
  448. ++sb;
  449. while(isspace(*sb))sb++;
  450. sscanf(sb, "%"PRINTF_INT64_MODIFIER"u",&contentlength64);
  451. if(param->maxtrafout64 && (param->maxtrafout64 < param->statscli64 || contentlength64 > param->maxtrafout64 - param->statscli64)){
  452. RETURN(10);
  453. }
  454. if(param->ndatfilterscli > 0 && contentlength64 > 0) continue;
  455. }
  456. inbuf += i;
  457. if((bufsize - inbuf) < LINESIZE){
  458. if (bufsize > (LINESIZE * 16)){
  459. RETURN (516);
  460. }
  461. if(!(newbuf = myrealloc(buf, bufsize + BUFSIZE))){RETURN (21);}
  462. buf = newbuf;
  463. bufsize += BUFSIZE;
  464. }
  465. } while( (i = sockgetlinebuf(param, CLIENT, buf + inbuf, LINESIZE - 2, '\n', conf.timeouts[STRING_S])) > 2);
  466. buf[inbuf] = 0;
  467. reqsize = (int)strlen(req);
  468. reqbufsize = reqsize + 1;
  469. #ifndef WITHMAIN
  470. action = handlereqfilters(param, &req, &reqbufsize, 0, &reqsize);
  471. if(action == HANDLED){
  472. RETURN(0);
  473. }
  474. if(action != PASS) RETURN(517);
  475. action = handlehdrfilterscli(param, &buf, &bufsize, 0, &inbuf);
  476. if(action == HANDLED){
  477. RETURN(0);
  478. }
  479. if(action != PASS) RETURN(517);
  480. if(param->ndatfilterscli > 0 && contentlength64 > 0){
  481. uint64_t newlen64;
  482. newlen64 = sockfillbuffcli(param, (unsigned long)contentlength64, CONNECTION_S);
  483. if(newlen64 == contentlength64) {
  484. action = handledatfltcli(param, &param->clibuf, &param->clibufsize, 0, &param->cliinbuf);
  485. if(action == HANDLED){
  486. RETURN(0);
  487. }
  488. if(action != PASS) RETURN(517);
  489. contentlength64 = param->cliinbuf;
  490. param->ndatfilterscli = 0;
  491. }
  492. sprintf((char*)buf+strlen((char *)buf), "Content-Length: %"PRINTF_INT64_MODIFIER"u\r\n", contentlength64);
  493. }
  494. #endif
  495. if((res = (*param->srv->authfunc)(param))) {RETURN(res);}
  496. if(ftp && param->redirtype != R_HTTP){
  497. SOCKET s;
  498. int mode = 0;
  499. int i=0;
  500. inftpbuf = 0;
  501. if(!ckeepalive){
  502. inftpbuf = FTPBUFSIZE - 20;
  503. res = ftplogin(param, ftpbuf, &inftpbuf);
  504. if(res){
  505. RETURN(res);
  506. }
  507. }
  508. ckeepalive = 1;
  509. if(ftpbase) myfree(ftpbase);
  510. ftpbase = NULL;
  511. if(!(sp = (unsigned char *)strchr((char *)ss, ' '))){RETURN(799);}
  512. *sp = 0;
  513. decodeurl(ss, 0);
  514. i = (int)strlen((char *)ss);
  515. if(!(ftpbase = myalloc(i+2))){RETURN(21);}
  516. memcpy(ftpbase, ss, i);
  517. if(ftpbase[i-1] != '/') ftpbase[i++] = '/';
  518. ftpbase[i] = 0;
  519. memcpy(buf, "<pre><hr>\n", 10);
  520. inbuf = 10;
  521. if(inftpbuf) {
  522. memcpy(buf+inbuf, ftpbuf, inftpbuf);
  523. inbuf += inftpbuf;
  524. memcpy(buf+inbuf, "<hr>", 4);
  525. inbuf += 4;
  526. }
  527. if(ftpbase[1] != 0){
  528. memcpy(buf+inbuf, "[<A HREF=\"..\">..</A>]\n", 22);
  529. inbuf += 22;
  530. }
  531. inftpbuf = FTPBUFSIZE - (20 + inftpbuf);
  532. res = ftpcd(param, ftpbase, ftpbuf, &inftpbuf);
  533. if(res){
  534. res = ftptype(param, (unsigned char *)"I");
  535. if(res)RETURN(res);
  536. ftpbase[--i] = 0;
  537. ftps = ftpcommand(param, param->operation == FTP_PUT? (unsigned char *)"PUT" : (unsigned char *)"RETR", ftpbase);
  538. }
  539. else {
  540. if(inftpbuf){
  541. memcpy(buf+inbuf, ftpbuf, inftpbuf);
  542. inbuf += inftpbuf;
  543. memcpy(buf+inbuf, "<hr>", 4);
  544. inbuf += 4;
  545. }
  546. ftps = ftpcommand(param, (unsigned char *)"LIST", NULL);
  547. mode = 1;
  548. }
  549. if(ftps == INVALID_SOCKET){RETURN(780);}
  550. if(!mode){
  551. socksend(param->clisock, (unsigned char *)proxy_stringtable[8], (int)strlen(proxy_stringtable[8]), conf.timeouts[STRING_S]);
  552. s = param->remsock;
  553. param->remsock = ftps;
  554. if((param->operation == FTP_PUT) && (contentlength64 > 0)) param->waitclient64 = contentlength64;
  555. res = sockmap(param, conf.timeouts[CONNECTION_L]);
  556. if (res == 99) res = 0;
  557. so._closesocket(ftps);
  558. ftps = INVALID_SOCKET;
  559. param->remsock = s;
  560. }
  561. else {
  562. int headsent = 0;
  563. int gotres = -1;
  564. s = param->remsock;
  565. if(param->srvoffset < param->srvinbuf){
  566. gotres = ftpres(param, buf+inbuf, bufsize-(inbuf+100));
  567. if(gotres) inbuf= (int)strlen((char *)buf);
  568. }
  569. param->remsock = ftps;
  570. if(gotres <= 0) for(; (res = sockgetlinebuf(param, SERVER, (unsigned char *)ftpbuf, FTPBUFSIZE - 20, '\n', conf.timeouts[STRING_S])) > 0; i++){
  571. int isdir = 0;
  572. int islink = 0;
  573. int filetoken =-1;
  574. int sizetoken =-1;
  575. int modetoken =-1;
  576. int datetoken =-1;
  577. int spaces = 1;
  578. unsigned char * tokens[10];
  579. unsigned wordlen [10];
  580. unsigned char j=0;
  581. int space = 1;
  582. ftpbuf[res] = 0;
  583. if(!i && ftpbuf[0] == 't' && ftpbuf[1] == 'o' && ftpbuf[2] == 't'){
  584. mode = 2;
  585. continue;
  586. }
  587. if(!isnumber(*ftpbuf) && mode == 1) mode = 2;
  588. for(sb=(unsigned char *)ftpbuf; *sb; sb++){
  589. if(!space && isspace(*sb)){
  590. space = 1;
  591. wordlen[j]=(unsigned)(sb-tokens[j]);
  592. j++;
  593. }
  594. if(space && !isspace(*sb)){
  595. space = 0;
  596. tokens[j] = sb;
  597. if(j==8)break;
  598. }
  599. }
  600. if(mode == 1){
  601. if(j < 4) continue;
  602. if(!(isdir = !memcmp(tokens[2], "<DIR>", wordlen[2])) && !isnumber(*tokens[2])){
  603. continue;
  604. }
  605. datetoken = 0;
  606. wordlen[datetoken] = ((unsigned)(tokens[1] - tokens[0])) + wordlen[1];
  607. sizetoken = 2;
  608. filetoken = 3;
  609. spaces = 10;
  610. }
  611. else {
  612. if(j < 8 || wordlen[0]!=10) continue;
  613. if(j < 8 || !isnumber(*tokens[4])) mode = 3;
  614. if(*tokens[0] == 'd') isdir = 1;
  615. if(*tokens[0] == 'l') islink = 1;
  616. modetoken = 0;
  617. sizetoken = (mode == 2)? 4:3;
  618. filetoken = (mode == 2)? 8:7;
  619. datetoken = (mode == 2)? 5:4;
  620. tokens[filetoken] = tokens[filetoken-1];
  621. while(*tokens[filetoken] && !isspace(*tokens[filetoken]))tokens[filetoken]++;
  622. if(*tokens[filetoken]){
  623. tokens[filetoken]++;
  624. }
  625. wordlen[datetoken] = (unsigned)(tokens[filetoken] - tokens[datetoken]);
  626. wordlen[filetoken] = (unsigned)strlen((char *)tokens[filetoken]);
  627. }
  628. if(modetoken >= 0) memcpy(buf+inbuf, tokens[modetoken], 11);
  629. else memcpy(buf+inbuf, "---------- ", 11);
  630. inbuf += 11;
  631. if((int) wordlen[datetoken]+256 > bufsize-inbuf) continue;
  632. memcpy(buf+inbuf, tokens[datetoken], wordlen[datetoken]);
  633. inbuf += wordlen[datetoken];
  634. if(isdir){
  635. memcpy(buf+inbuf, " DIR", 10);
  636. inbuf+=10;
  637. }
  638. else if(islink){
  639. memcpy(buf+inbuf, " LINK", 10);
  640. inbuf+=10;
  641. }
  642. else{
  643. unsigned k;
  644. if(wordlen[sizetoken]>10) wordlen[sizetoken] = 10;
  645. for(k=10; k > wordlen[sizetoken]; k--){
  646. buf[inbuf++] = ' ';
  647. }
  648. memcpy(buf+inbuf, tokens[sizetoken], wordlen[sizetoken]);
  649. inbuf+=wordlen[sizetoken];
  650. }
  651. memcpy(buf+inbuf, " <A HREF=\"", 10);
  652. inbuf+=10;
  653. sb = NULL;
  654. if(islink) sb = (unsigned char *)strstr((char *)tokens[filetoken], " -> ");
  655. if(sb) sb+=4;
  656. else sb=tokens[filetoken];
  657. if(*sb != '/' && ftpbase)file2url(ftpbase, buf, bufsize, (int *)&inbuf, 1);
  658. file2url(sb, buf, bufsize, (int *)&inbuf, 0);
  659. if(isdir)buf[inbuf++] = '/';
  660. memcpy(buf+inbuf, "\">", 2);
  661. inbuf+=2;
  662. for(sb=tokens[filetoken]; *sb; sb++){
  663. if((bufsize - inbuf)<16)break;
  664. if(*sb == '<'){
  665. memcpy(buf+inbuf, "&lt;", 4);
  666. inbuf+=4;
  667. }
  668. else if(*sb == '>'){
  669. memcpy(buf+inbuf, "&gt;", 4);
  670. inbuf+=4;
  671. }
  672. else if(*sb == '\r' || *sb=='\n'){
  673. continue;
  674. }
  675. else if(islink && sb[0] == ' ' && sb[1] == '-'
  676. && sb[2] == '>'){
  677. memcpy(buf+inbuf, "</A> ", 5);
  678. inbuf+=5;
  679. }
  680. else buf[inbuf++]=*sb;
  681. }
  682. if(islink!=2){
  683. memcpy(buf+inbuf, "</A>", 4);
  684. inbuf+=4;
  685. }
  686. buf[inbuf++] = '\n';
  687. if((bufsize - inbuf) < LINESIZE){
  688. if (bufsize > 20000){
  689. if(!headsent++){
  690. socksend(param->clisock, (unsigned char *)proxy_stringtable[9], (int)strlen(proxy_stringtable[9]), conf.timeouts[STRING_S]);
  691. }
  692. if((unsigned)socksend(param->clisock, buf, inbuf, conf.timeouts[STRING_S]) != inbuf){
  693. RETURN(781);
  694. }
  695. inbuf = 0;
  696. }
  697. else {
  698. if(!(newbuf = myrealloc(buf, bufsize + BUFSIZE))){RETURN (21);}
  699. buf = newbuf;
  700. bufsize += BUFSIZE;
  701. }
  702. }
  703. }
  704. memcpy(buf+inbuf, "<hr>", 4);
  705. inbuf += 4;
  706. so._closesocket(ftps);
  707. ftps = INVALID_SOCKET;
  708. param->remsock = s;
  709. if(inbuf){
  710. buf[inbuf] = 0;
  711. if(gotres < 0 ) res = ftpres(param, buf+inbuf, bufsize-inbuf);
  712. else res = gotres;
  713. inbuf = (int)strlen((char *)buf);
  714. if(!headsent){
  715. sprintf(ftpbuf,
  716. "HTTP/1.0 200 OK\r\n"
  717. "Content-Type: text/html\r\n"
  718. "Proxy-Connection: keep-alive\r\n"
  719. "Content-Length: %d\r\n\r\n",
  720. inbuf);
  721. socksend(param->clisock, (unsigned char *)ftpbuf, (int)strlen(ftpbuf), conf.timeouts[STRING_S]);
  722. }
  723. socksend(param->clisock, buf, inbuf, conf.timeouts[STRING_S]);
  724. if(res){RETURN(res);}
  725. if(!headsent)goto REQUESTEND;
  726. }
  727. RETURN(0);
  728. }
  729. RETURN(res);
  730. }
  731. if(isconnect && param->redirtype != R_HTTP) {
  732. socksend(param->clisock, (unsigned char *)proxy_stringtable[8], (int)strlen(proxy_stringtable[8]), conf.timeouts[STRING_S]);
  733. if(param->redirectfunc) return (*param->redirectfunc)(param);
  734. param->res = sockmap(param, conf.timeouts[CONNECTION_L]);
  735. if(param->redirectfunc) return (*param->redirectfunc)(param);
  736. RETURN(param->res);
  737. }
  738. if(!req || param->redirtype != R_HTTP) {
  739. reqlen = 0;
  740. }
  741. else {
  742. redirect = 1;
  743. if(socksend(param->remsock, req , (res = (int)strlen((char *)req)), conf.timeouts[STRING_L]) != res) {
  744. RETURN(518);
  745. }
  746. param->statscli64 += res;
  747. param->nwrites++;
  748. }
  749. inbuf = 0;
  750. #ifndef ANONYMOUS
  751. if(anonymous!=1){
  752. sprintf((char*)buf+strlen((char *)buf), "Via: 1.1 ");
  753. gethostname((char *)(buf+strlen((char *)buf)), 256);
  754. 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 *)"");
  755. if(!anonymous)myinet_ntop(*SAFAMILY(&param->sincr), SAADDR(&param->sincr), (char *)buf + strlen((char *)buf), 128);
  756. else {
  757. unsigned long tmp;
  758. tmp = ((unsigned long)myrand(param, sizeof(struct clientparam))<<16)^(unsigned long)rand();
  759. myinet_ntop(AF_INET, &tmp, (char *)buf + strlen((char *)buf), 64);
  760. }
  761. sprintf((char*)buf+strlen((char *)buf), "\r\n");
  762. }
  763. #endif
  764. if(keepalive <= 1) sprintf((char*)buf+strlen((char *)buf), "%s: %s\r\n", (param->redirtype == R_HTTP)?"Proxy-Connection":"Connection", keepalive? "keep-alive":"close");
  765. if(param->extusername){
  766. sprintf((char*)buf + strlen((char *)buf), "%s: basic ", (redirect)?"Proxy-Authorization":"Authorization");
  767. sprintf((char*)username, "%.128s:%.64s", param->extusername, param->extpassword?param->extpassword:(unsigned char*)"");
  768. en64(username, buf+strlen((char *)buf), (int)strlen((char *)username));
  769. sprintf((char*)buf + strlen((char *)buf), "\r\n");
  770. }
  771. sprintf((char*)buf+strlen((char *)buf), "\r\n");
  772. if ((res = socksend(param->remsock, buf+reqlen, (int)strlen((char *)buf+reqlen), conf.timeouts[STRING_S])) != (int)strlen((char *)buf+reqlen)) {
  773. RETURN(518);
  774. }
  775. param->statscli64 += res;
  776. param->nwrites++;
  777. if(param->bandlimfunc) {
  778. sleeptime = param->bandlimfunc(param, 0, (int)strlen((char *)buf));
  779. }
  780. if(contentlength64 > 0){
  781. param->nolongdatfilter = 0;
  782. param->waitclient64 = contentlength64;
  783. res = sockmap(param, conf.timeouts[CONNECTION_S]);
  784. param->waitclient64 = 0;
  785. if(res != 99) {
  786. RETURN(res);
  787. }
  788. }
  789. contentlength64 = 0;
  790. inbuf = 0;
  791. ckeepalive = keepalive;
  792. res = 0;
  793. authenticate = 0;
  794. param->chunked = 0;
  795. hascontent = 0;
  796. while( (i = sockgetlinebuf(param, SERVER, buf + inbuf, LINESIZE - 1, '\n', conf.timeouts[(res)?STRING_S:STRING_L])) > 2) {
  797. if(!res && i>9)param->status = res = atoi((char *)buf + inbuf + 9);
  798. if(((i >= 25 && !strncasecmp((char *)(buf+inbuf), "proxy-connection:", 17))
  799. ||
  800. (i> 16 && !strncasecmp((char *)(buf+inbuf), "connection:", 11))
  801. )){
  802. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  803. if(!sb)continue;
  804. ++sb;
  805. while(isspace(*sb))sb++;
  806. if(strncasecmp((char *)sb,"keep-alive", 10))ckeepalive = 0;
  807. if(!param->srv->transparent)continue;
  808. }
  809. else if(i> 6 && !param->srv->transparent && (!strncasecmp((char *)(buf+inbuf), "proxy-", 6))){
  810. continue;
  811. }
  812. else if(i> 6 && (!strncasecmp((char *)(buf+inbuf), "www-authenticate", 16))){
  813. authenticate = 1;
  814. }
  815. else if(i > 15 && (!strncasecmp((char *)(buf+inbuf), "content-length", 14))){
  816. buf[inbuf+i]=0;
  817. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  818. if(!sb)continue;
  819. ++sb;
  820. while(isspace(*sb))sb++;
  821. sscanf(sb, "%"PRINTF_INT64_MODIFIER"u", &contentlength64);
  822. hascontent = 1;
  823. if(param->unsafefilter && param->ndatfilterssrv > 0) {
  824. hascontent = 2;
  825. continue;
  826. }
  827. if(param->maxtrafin64 && (param->maxtrafin64 < param->statssrv64 || contentlength64 + param->statssrv64 > param->maxtrafin64)){
  828. RETURN(10);
  829. }
  830. }
  831. else if(i>25 && (!strncasecmp((char *)(buf+inbuf), "transfer-encoding", 17))){
  832. buf[inbuf+i]=0;
  833. sb = (unsigned char *)strchr((char *)(buf+inbuf), ':');
  834. if(!sb)continue;
  835. ++sb;
  836. while(isspace(*sb))sb++;
  837. if(!strncasecmp(sb, "chunked", 7)){
  838. param->chunked = 1;
  839. }
  840. }
  841. inbuf += i;
  842. if((bufsize - inbuf) < LINESIZE){
  843. if (bufsize > 20000){
  844. RETURN (516);
  845. }
  846. if(!(newbuf = myrealloc(buf, bufsize + BUFSIZE))){RETURN (21);}
  847. buf = newbuf;
  848. bufsize += BUFSIZE;
  849. }
  850. }
  851. if(res < 200 || res > 499) {
  852. ckeepalive = 0;
  853. }
  854. if((res == 304 || res == 204) && !hascontent){
  855. hascontent = 1;
  856. contentlength64 = 0;
  857. }
  858. if(param->bandlimfunc) {
  859. int st1;
  860. st1 = (*param->bandlimfunc)(param, inbuf, 0);
  861. if(st1 > sleeptime) sleeptime = st1;
  862. if(sleeptime > 0){
  863. /* if(sleeptime > 30) sleeptime = 30; */
  864. usleep(sleeptime * SLEEPTIME);
  865. }
  866. }
  867. buf[inbuf] = 0;
  868. if(inbuf < 9) {RETURN (522);}
  869. #ifndef WITHMAIN
  870. action = handlehdrfilterssrv(param, &buf, &bufsize, 0, &inbuf);
  871. if(action == HANDLED){
  872. RETURN(0);
  873. }
  874. if(action != PASS) RETURN(517);
  875. param->nolongdatfilter = 0;
  876. if (conf.filtermaxsize && contentlength64 > (uint64_t)conf.filtermaxsize) {
  877. param->nolongdatfilter = 1;
  878. }
  879. else if(param->unsafefilter && param->ndatfilterssrv > 0 && contentlength64 > 0 && param->operation != HTTP_HEAD && res != 204 && res != 304){
  880. uint64_t newlen;
  881. newlen = (uint64_t)sockfillbuffsrv(param, (unsigned long) contentlength64, CONNECTION_S);
  882. if(newlen == contentlength64) {
  883. action = handledatfltsrv(param, &param->srvbuf, &param->srvbufsize, 0, &param->srvinbuf);
  884. param->nolongdatfilter = 1;
  885. if(action == HANDLED){
  886. RETURN(0);
  887. }
  888. if(action != PASS) RETURN(517);
  889. contentlength64 = param->srvinbuf;
  890. sprintf((char*)buf+strlen((char *)buf), "Content-Length: %"PRINTF_INT64_MODIFIER"u\r\n", contentlength64);
  891. hascontent = 1;
  892. }
  893. }
  894. if (contentlength64 > 0 && hascontent != 1) ckeepalive = 0;
  895. #else
  896. #endif
  897. if(!isconnect || param->operation){
  898. if(authenticate && !param->transparent) sprintf((char*)buf+strlen((char *)buf),
  899. "Proxy-support: Session-Based-Authentication\r\n"
  900. "Connection: Proxy-support\r\n"
  901. );
  902. if(!param->srv->transparent){
  903. if(ckeepalive <= 1) sprintf((char*)buf+strlen((char *)buf), "Connection: %s\r\n",
  904. (hascontent && ckeepalive)?"Keep-Alive":"Close");
  905. if(!param->transparent)sprintf((char*)buf+strlen((char *)buf), "Proxy-Connection: %s\r\n",
  906. (hascontent && ckeepalive)?"Keep-Alive":"Close");
  907. }
  908. sprintf((char*)buf + strlen((char *)buf), "\r\n");
  909. if((socksend(param->clisock, buf, (int)strlen((char *)buf), conf.timeouts[STRING_S])) != (int)strlen((char *)buf)) {
  910. RETURN(521);
  911. }
  912. }
  913. if((param->chunked || contentlength64 > 0) && param->operation != HTTP_HEAD && res != 204 && res != 304) {
  914. do {
  915. if(param->chunked){
  916. char smallbuf[32];
  917. while ((i = sockgetlinebuf(param, SERVER, smallbuf, 30, '\n', conf.timeouts[STRING_S])) == 2) {
  918. if (socksend(param->clisock, smallbuf, i, conf.timeouts[STRING_S]) != i){
  919. RETURN(533);
  920. }
  921. if(param->chunked == 2) break;
  922. }
  923. if(i<3) {
  924. keepalive = 0;
  925. break;
  926. }
  927. if (socksend(param->clisock, smallbuf, i, conf.timeouts[STRING_S]) != i){
  928. RETURN(535);
  929. }
  930. if(param->chunked == 2) {
  931. if((i = sockgetlinebuf(param, SERVER, smallbuf, 30, '\n', conf.timeouts[STRING_S])) != 2) RETURN(534);
  932. if (socksend(param->clisock, smallbuf, i, conf.timeouts[STRING_S]) != i){
  933. RETURN(533);
  934. }
  935. break;
  936. }
  937. smallbuf[i] = 0;
  938. contentlength64 = 0;
  939. sscanf(smallbuf, "%"PRINTF_INT64_MODIFIER"x", &contentlength64);
  940. if(contentlength64 == 0) {
  941. param->chunked = 2;
  942. }
  943. }
  944. if(param->chunked != 2){
  945. param->waitserver64 = contentlength64;
  946. if((res = sockmap(param, conf.timeouts[CONNECTION_S])) != 98){
  947. RETURN(res);
  948. }
  949. param->waitserver64 = 0;
  950. }
  951. } while(param->chunked);
  952. }
  953. if(isconnect && res == 200 && param->operation){
  954. RETURN (sockmap(param, conf.timeouts[CONNECTION_S]));
  955. }
  956. else if(isconnect){
  957. ckeepalive = keepalive = 1;
  958. }
  959. else if(!hascontent && !param->chunked) {
  960. RETURN(sockmap(param, conf.timeouts[CONNECTION_S]));
  961. }
  962. contentlength64 = 0;
  963. REQUESTEND:
  964. if((!ckeepalive || !keepalive) && param->remsock != INVALID_SOCKET){
  965. so._shutdown(param->remsock, SHUT_RDWR);
  966. so._closesocket(param->remsock);
  967. param->remsock = INVALID_SOCKET;
  968. RETURN(0);
  969. }
  970. if(param->transparent && (!ckeepalive || !keepalive)) {RETURN (0);}
  971. logurl(param, buf, (char *)req, ftp);
  972. param->status = 0;
  973. }
  974. CLEANRET:
  975. 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))) {
  976. if((param->res>=509 && param->res < 517) || param->res > 900) while( (i = sockgetlinebuf(param, CLIENT, buf, BUFSIZE - 1, '\n', conf.timeouts[STRING_S])) > 2);
  977. if(param->res == 10) {
  978. socksend(param->clisock, (unsigned char *)proxy_stringtable[2], (int)strlen(proxy_stringtable[2]), conf.timeouts[STRING_S]);
  979. }
  980. else if (res == 700 || res == 701){
  981. socksend(param->clisock, (unsigned char *)proxy_stringtable[16], (int)strlen(proxy_stringtable[16]), conf.timeouts[STRING_S]);
  982. socksend(param->clisock, (unsigned char *)ftpbuf, inftpbuf, conf.timeouts[STRING_S]);
  983. }
  984. else if(param->res == 100 || (param->res >10 && param->res < 20) || (param->res >701 && param->res <= 705)) {
  985. socksend(param->clisock, (unsigned char *)proxy_stringtable[1], (int)strlen(proxy_stringtable[1]), conf.timeouts[STRING_S]);
  986. }
  987. else if(param->res >=20 && param->res < 30) {
  988. socksend(param->clisock, (unsigned char *)proxy_stringtable[6], (int)strlen(proxy_stringtable[6]), conf.timeouts[STRING_S]);
  989. }
  990. else if(param->res >=30 && param->res < 80) {
  991. socksend(param->clisock, (unsigned char *)proxy_stringtable[5], (int)strlen(proxy_stringtable[5]), conf.timeouts[STRING_S]);
  992. }
  993. else if(param->res == 1 || (param->srv->nouser && param->res < 10)) {
  994. socksend(param->clisock, (unsigned char *)proxy_stringtable[11], (int)strlen(proxy_stringtable[11]), conf.timeouts[STRING_S]);
  995. }
  996. else if(param->res < 10) {
  997. 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]);
  998. }
  999. else if(param->res == 999) {
  1000. socksend(param->clisock, (unsigned char *)proxy_stringtable[4], (int)strlen(proxy_stringtable[4]), conf.timeouts[STRING_S]);
  1001. }
  1002. else if(param->res == 519) {
  1003. socksend(param->clisock, (unsigned char *)proxy_stringtable[3], (int)strlen(proxy_stringtable[3]), conf.timeouts[STRING_S]);
  1004. }
  1005. else if(param->res == 517) {
  1006. socksend(param->clisock, (unsigned char *)proxy_stringtable[15], (int)strlen(proxy_stringtable[15]), conf.timeouts[STRING_S]);
  1007. }
  1008. else if(param->res == 780) {
  1009. socksend(param->clisock, (unsigned char *)proxy_stringtable[10], (int)strlen(proxy_stringtable[10]), conf.timeouts[STRING_S]);
  1010. }
  1011. else if(param->res >= 511 && param->res<=516){
  1012. socksend(param->clisock, (unsigned char *)proxy_stringtable[0], (int)strlen(proxy_stringtable[0]), conf.timeouts[STRING_S]);
  1013. }
  1014. }
  1015. logurl(param, buf, (char *)req, ftp);
  1016. if(req)myfree(req);
  1017. if(buf)myfree(buf);
  1018. if(ftpbase)myfree(ftpbase);
  1019. freeparam(param);
  1020. return (NULL);
  1021. }
  1022. #ifdef WITHMAIN
  1023. struct proxydef childdef = {
  1024. proxychild,
  1025. 3128,
  1026. 0,
  1027. S_PROXY,
  1028. "-a - anonymous proxy\r\n"
  1029. "-a1 - anonymous proxy with random client IP spoofing\r\n"
  1030. };
  1031. #include "proxymain.c"
  1032. #endif