proxy.c 36 KB

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