icqpr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. #ifndef PORTMAP
  8. #define PORTMAP
  9. #endif
  10. #define RETURN(xxx) { param->res = xxx; goto CLEANRET; }
  11. static void hexdump(unsigned char *data, int len){
  12. for(; len; data++, len--){
  13. printf("%02x", (unsigned)*data);
  14. }
  15. printf("\n");
  16. }
  17. struct flap_header {
  18. unsigned char id;
  19. unsigned char chan;
  20. unsigned short seq;
  21. unsigned short size;
  22. char data[0];
  23. };
  24. struct snack_header {
  25. unsigned family;
  26. unsigned short flags;
  27. unsigned id;
  28. char data[0];
  29. };
  30. struct tlv_header {
  31. unsigned short type;
  32. unsigned short size;
  33. char data[0];
  34. };
  35. typedef enum {
  36. ONBEGIN = 0,
  37. ONCHAN,
  38. ONSEQ1,
  39. ONSEQ2,
  40. ONSIZE1,
  41. ONSIZE2,
  42. ONDATA
  43. } ICQSTATE;
  44. struct icqstate {
  45. ICQSTATE state;
  46. int leftinstate;
  47. unsigned short seq;
  48. unsigned short srvseq;
  49. unsigned short gotseq;
  50. unsigned short resyncseq;
  51. char channel;
  52. };
  53. typedef enum {
  54. ICQUNKNOWN,
  55. ICQCLEAR,
  56. ICQMD5,
  57. ICQCOOKIE
  58. } LOGINTYPE;
  59. struct icq_cookie {
  60. struct icq_cookie *next;
  61. char *id;
  62. int size;
  63. char * cookie;
  64. char * connectstring;
  65. };
  66. static struct icq_cookie *icq_cookies = NULL;
  67. pthread_mutex_t icq_cookie_mutex;
  68. int icq_cookie_mutex_init = 0;
  69. static void icq_clear(void *fo){
  70. };
  71. static void addbuffer(int increment, struct clientparam * param, unsigned char ** buf_p, int * bufsize_p, int * length_p){
  72. int bufsize = *length_p + increment + 40;
  73. unsigned char *newbuf;
  74. int len = 0;
  75. if(bufsize > *bufsize_p){
  76. newbuf = myalloc(bufsize);
  77. if(!newbuf) return;
  78. memcpy(newbuf, *buf_p, *length_p);
  79. myfree(*buf_p);
  80. *buf_p = newbuf;
  81. *bufsize_p = bufsize;
  82. }
  83. if(increment) len = sockrecvfrom(param->remsock, (struct sockaddr *)&param->sinsr, *buf_p + *length_p, increment, conf.timeouts[STRING_S]*1000);
  84. if(len > 0) {
  85. *length_p += len;
  86. param->nreads++;
  87. param->statssrv64 += len;
  88. }
  89. return;
  90. }
  91. static int searchcookie(struct clientparam *param, struct flap_header * flap, int len, int * dif, struct tlv_header *tlv, int extra){
  92. struct icq_cookie *ic;
  93. char smallbuf[64];
  94. struct tlv_header *bostlv = NULL;
  95. struct sockaddr_in sa;
  96. SASIZETYPE size = sizeof(sa);
  97. int movelen = 0;
  98. if(!icq_cookie_mutex_init){
  99. pthread_mutex_init(&icq_cookie_mutex, NULL);
  100. icq_cookie_mutex_init = 1;
  101. }
  102. pthread_mutex_lock(&icq_cookie_mutex);
  103. for(ic = icq_cookies; ic; ic = ic->next)if(!strcmp(param->username, ic->id))break;
  104. if(!ic){
  105. ic = myalloc(sizeof(struct icq_cookie));
  106. memset(ic, 0, sizeof(struct icq_cookie));
  107. ic->id = mystrdup(param->username);
  108. ic->next = icq_cookies;
  109. icq_cookies = ic;
  110. }
  111. for(; ntohs(tlv->size) < 65500 && len >= (ntohs(tlv->size) + 4); len -= (ntohs(tlv->size) + 4), tlv = (struct tlv_header *)(tlv->data + ntohs(tlv->size))){
  112. if(ntohs(tlv->type) == 0x0006){
  113. if(ic->cookie)myfree(ic->cookie);
  114. ic->cookie = myalloc(ntohs(tlv->size));
  115. memcpy(ic->cookie, tlv->data, ntohs(tlv->size));
  116. ic->size = tlv->size;
  117. }
  118. else if(ntohs(tlv->type) == 0x0005){
  119. if(ic->connectstring)myfree(ic->connectstring);
  120. ic->connectstring = myalloc(ntohs(tlv->size)+1);
  121. memcpy(ic->connectstring, tlv->data, ntohs(tlv->size));
  122. ic->connectstring[ntohs(tlv->size)] = 0;
  123. bostlv = tlv;
  124. movelen = extra + (len - 4) - ntohs(bostlv->size);
  125. }
  126. }
  127. if(!ic->connectstring || !ic->cookie){
  128. if(ic->cookie)myfree(ic->cookie);
  129. if(ic->connectstring)myfree(ic->connectstring);
  130. ic->cookie = NULL;
  131. ic->connectstring = NULL;
  132. ic->size = 0;
  133. bostlv = NULL;
  134. }
  135. pthread_mutex_unlock(&icq_cookie_mutex);
  136. if(bostlv){
  137. if(so._getsockname(param->clisock, (struct sockaddr *)&sa, &size)==-1) return 1;
  138. len = myinet_ntop(*SAFAMILY(&sa),SAADDR(&sa), smallbuf, 64);
  139. if(strchr(ic->connectstring, ':'))sprintf(smallbuf+len, ":%hu", ntohs(sa.sin_port));
  140. len = (int)strlen(smallbuf);
  141. *dif = len - (int)ntohs(bostlv->size);
  142. if(*dif != 0 && movelen > 0){
  143. memmove(bostlv->data + len, bostlv->data + ntohs(bostlv->size), movelen);
  144. }
  145. memcpy(bostlv->data, smallbuf, len);
  146. bostlv->size = htons(len);
  147. len = ((int)ntohs(flap->size)) + *dif;
  148. flap->size = htons(len);
  149. }
  150. return 0;
  151. }
  152. static FILTER_ACTION icq_srv(void *fc, struct clientparam * param, unsigned char ** buf_p, int * bufsize_p, int ioffset, int * length_p){
  153. unsigned char * start = *buf_p + ioffset;
  154. int len = *length_p - ioffset;
  155. struct icqstate *state = (struct icqstate *)fc;
  156. int size;
  157. int offset;
  158. while (len > 0){
  159. switch(state->state){
  160. case ONBEGIN:
  161. if((*start) == 0x2A) {
  162. if(len < 6){
  163. offset = (int)(start - *buf_p);
  164. addbuffer(6-len, param, buf_p, bufsize_p, length_p);
  165. start = *buf_p + offset;
  166. len = (int)(*buf_p + *length_p - start);
  167. }
  168. state->state = ONCHAN;
  169. }
  170. else {
  171. if(!state->leftinstate)param->srv->logfunc(param, "Warning: need resync");
  172. state->leftinstate++;
  173. if(state->leftinstate > 65535){
  174. param->srv->logfunc(param, "Out of Sync");
  175. return REJECT;
  176. }
  177. }
  178. start++;
  179. len--;
  180. break;
  181. case ONCHAN:
  182. if (*start >= 10){
  183. param->srv->logfunc(param, "Warning: Wrong channel");
  184. state->state = ONBEGIN;
  185. }
  186. else {
  187. state->state = ONSEQ1;
  188. state->channel = *start;
  189. start++;
  190. len--;
  191. }
  192. break;
  193. case ONSEQ1:
  194. state->gotseq = (((unsigned)*start) << 8);
  195. state->state = ONSEQ2;
  196. *(start) = (state->seq>>8);
  197. start++;
  198. len--;
  199. break;
  200. case ONSEQ2:
  201. state->gotseq += *start;
  202. if(state->gotseq != state->srvseq){
  203. char smallbuf[64];
  204. if(((state->gotseq < state->srvseq) || ((state->gotseq - state->srvseq) > 10 )) && (!state->resyncseq || state->gotseq != state->resyncseq)){
  205. sprintf(smallbuf, "Warning: Wrong sequence, expected: %04hx got: %04hx", state->srvseq, state->gotseq);
  206. param->srv->logfunc(param, smallbuf);
  207. state->state = ONBEGIN;
  208. state->resyncseq = state->gotseq;
  209. break;
  210. }
  211. sprintf(smallbuf, "Warning: %hu flaps are lost on resync", state->gotseq - state->srvseq );
  212. param->srv->logfunc(param, smallbuf);
  213. state->srvseq = state->gotseq;
  214. *(start-1) = (state->seq>>8);
  215. }
  216. *start = (state->seq & 0x00FF);
  217. state->srvseq = state->srvseq + 1;
  218. state->seq = state->seq + 1;
  219. state->state = ONSIZE1;
  220. start++;
  221. len--;
  222. break;
  223. case ONSIZE1:
  224. state->leftinstate = (((unsigned)(*start))<<8);
  225. state->state = ONSIZE2;
  226. start++;
  227. len--;
  228. break;
  229. case ONSIZE2:
  230. state->leftinstate += *start;
  231. state->state = (state->leftinstate)?ONDATA:ONBEGIN;
  232. start++;
  233. len--;
  234. if(state->leftinstate > 30 && state->channel == 2) {
  235. if(len < state->leftinstate) {
  236. offset = (int)(start - *buf_p);
  237. addbuffer(state->leftinstate - len, param, buf_p, bufsize_p, length_p);
  238. start = *buf_p + offset;
  239. len = (int)(*length_p - offset);
  240. }
  241. size = 0;
  242. if ((start[4] & 0x80)) {
  243. size = htons(*(unsigned short *)(start+10)) + 2;
  244. if(size > 8) size = 0;
  245. }
  246. if (start[0] == 0 && start[1] == 1 &&
  247. ((start[2] == 0 && start[3] == 5) || (start[2] == 1 && start[3] == 2))){
  248. int dif = 0;
  249. offset = (int)(start - *buf_p);
  250. addbuffer(0, param, buf_p, bufsize_p, length_p);
  251. start = *buf_p + offset;
  252. searchcookie(param, (struct flap_header *) (start-6), state->leftinstate-(size+10), &dif, (struct tlv_header *) (start + size + 10), len - state->leftinstate);
  253. *length_p += dif;
  254. start += (state->leftinstate + dif);
  255. len -= state->leftinstate;
  256. state->leftinstate = 0;
  257. state->state = ONBEGIN;
  258. }
  259. }
  260. break;
  261. case ONDATA:
  262. size = (state->leftinstate > len)? len : state->leftinstate;
  263. start += size;
  264. len -= size;
  265. state->leftinstate -= size;
  266. if(!state->leftinstate) {
  267. state->state = ONBEGIN;
  268. }
  269. break;
  270. }
  271. }
  272. return CONTINUE;
  273. }
  274. static struct filter icqfilter = {
  275. NULL,
  276. "icqfilter",
  277. NULL,
  278. NULL,
  279. NULL,
  280. NULL,
  281. NULL,
  282. NULL,
  283. NULL,
  284. NULL,
  285. *icq_srv,
  286. *icq_clear,
  287. NULL
  288. };
  289. static int readflap(struct clientparam * param, int direction, unsigned char *buf, int buflen){
  290. int i, len;
  291. struct flap_header *flap = (struct flap_header *)buf;
  292. i = sockgetlinebuf(param, direction, buf, 6, EOF, conf.timeouts[STRING_L]);
  293. if(i!=6) return 1;
  294. if(flap->id != 0x2a) return 2;
  295. len = ntohs(flap->size);
  296. if(len > buflen-6) return 3;
  297. i = sockgetlinebuf(param, direction, flap->data, len, EOF, conf.timeouts[STRING_S]);
  298. if(len != i) return 4;
  299. return 0;
  300. }
  301. #define flap ((struct flap_header *)buf)
  302. #define snack ((struct snack_header *)(buf+6))
  303. void * icqprchild(struct clientparam* param) {
  304. int res;
  305. unsigned char tmpsend[1024];
  306. unsigned char *buf;
  307. int i,j,len,len1;
  308. int offset = 0;
  309. int buflen = 16384;
  310. LOGINTYPE logintype = ICQUNKNOWN;
  311. int greet = 0;
  312. struct icq_cookie *ic;
  313. struct tlv_header *tlv;
  314. struct icqstate mystate = {
  315. ONBEGIN,
  316. 0, 0, 0,
  317. 0
  318. };
  319. struct filterp icqfilterp = {
  320. &icqfilter,
  321. (void *)&mystate
  322. };
  323. struct filterp **newfilters;
  324. char handshake[] = {'\052', '\001', '\000', '\000', '\000', '\004', '\000', '\000', '\000', '\001'};
  325. memcpy(tmpsend, handshake, 10);
  326. if(socksend(param->clisock, tmpsend, 10, conf.timeouts[STRING_S])!=10) {RETURN (1101);}
  327. buf = myalloc(65600);
  328. if((res = readflap(param, CLIENT, buf, 1000))) {RETURN (1180 + res);}
  329. if(ntohs(flap->size) == 4 || ntohs(flap->size) == 12){
  330. tmpsend[2] = buf[2];
  331. tmpsend[3] = buf[3];
  332. greet = 1;
  333. if(readflap(param, CLIENT, buf, 65550)) {RETURN (110);}
  334. }
  335. if(flap->chan != 1 && (flap->chan != 2 || snack->family != htonl(0x00170006))){
  336. RETURN(1104);
  337. }
  338. len = ntohs(flap->size);
  339. if(flap->chan == 1){
  340. tlv = (struct tlv_header *)(flap->data + 4);
  341. len -= 4;
  342. }
  343. else {
  344. tlv = (struct tlv_header *)(flap->data + 10);
  345. len -= 10;
  346. }
  347. for(; len >= (ntohs(tlv->size) + 4); len -= (ntohs(tlv->size) + 4), tlv = (struct tlv_header *)(tlv->data + ntohs(tlv->size))){
  348. switch(ntohs(tlv->type)){
  349. case 0x0001:
  350. if(flap->chan == 2 && !logintype)logintype = ICQMD5;
  351. if(!param->username){
  352. param->username = myalloc(ntohs(tlv->size) + 1);
  353. for(i=0, j=0; i < ntohs(tlv->size); i++){
  354. if(!isspace(tlv->data[i]))param->username[j++]=tolower(tlv->data[i]);
  355. }
  356. param->username[j] = 0;
  357. }
  358. break;
  359. case 0x0002:
  360. logintype = ICQCLEAR;
  361. break;
  362. case 0x0006:
  363. logintype = ICQCOOKIE;
  364. for(ic = icq_cookies; ic; ic=ic->next){
  365. if(ic->size && ic->size == tlv->size && !memcmp(ic->cookie, tlv->data, ntohs(tlv->size))){
  366. parsehostname((char *)ic->connectstring, param, ntohs(param->srv->targetport));
  367. if(!param->username && ic->id) param->username = mystrdup(ic->id);
  368. break;
  369. }
  370. }
  371. if(!ic) RETURN(1132);
  372. break;
  373. }
  374. }
  375. if(!logintype) RETURN(1133);
  376. if(logintype != ICQCOOKIE) {
  377. parsehostname((char *)param->srv->target, param, ntohs(param->srv->targetport));
  378. }
  379. param->operation = CONNECT;
  380. res = (*param->srv->authfunc)(param);
  381. if(res) {RETURN(res);}
  382. if(greet){
  383. if(socksend(param->remsock, tmpsend, 10, conf.timeouts[STRING_S])!=10) {RETURN (1105);}
  384. param->statscli64 += 10;
  385. }
  386. if(readflap(param, SERVER, tmpsend, 1024)) {RETURN (1111);}
  387. param->statssrv64 += (ntohs(((struct flap_header *)tmpsend)->size) + 6);
  388. mystate.srvseq = ntohs(((struct flap_header *)tmpsend)->seq) + 1;
  389. mystate.seq = 1;
  390. len = ntohs(flap->size) + 6;
  391. if((res=handledatfltcli(param, &buf, &buflen, offset, &len))!=PASS) RETURN(res);
  392. if(socksend(param->remsock, buf+offset, len, conf.timeouts[STRING_S])!=(ntohs(flap->size)+6)) {RETURN (1106);}
  393. offset = 0;
  394. param->statscli64 += len;
  395. if(logintype == ICQMD5) {
  396. if(readflap(param, SERVER, buf, 65550)) {RETURN (1112);}
  397. mystate.srvseq = ntohs(flap->seq) + 1;
  398. flap->seq = htons(mystate.seq);
  399. mystate.seq++;
  400. len = ntohs(flap->size) + 6;
  401. if((res=handledatfltsrv(param, &buf, &buflen, offset, &len))!=PASS) RETURN(res);
  402. if(socksend(param->clisock, buf+offset, len, conf.timeouts[STRING_S])!=len) {RETURN (1113);}
  403. offset = 0;
  404. if(readflap(param, CLIENT, buf, 65550)) {RETURN (1114);}
  405. len = ntohs(flap->size) + 6;
  406. if((res=handledatfltcli(param, &buf, &buflen, offset, &len))!=PASS) RETURN(res);
  407. if(socksend(param->remsock, buf+offset, len, conf.timeouts[STRING_S])!=len) {RETURN (1115);}
  408. param->statscli64 += len;
  409. offset = 0;
  410. }
  411. if(logintype != ICQCOOKIE) {
  412. if(readflap(param, SERVER, buf, 65550)) {RETURN (1116);}
  413. mystate.srvseq = ntohs(flap->seq) + 1;
  414. flap->seq = htons(mystate.seq);
  415. mystate.seq++;
  416. len = ntohs(flap->size);
  417. if(!param->username) {RETURN (1117);}
  418. if(flap->chan == 1 || flap->chan == 4){
  419. if(flap->data[0] == 0 && flap->data[1] == 0 && flap->data[2] == 0 && flap->data[3] == 1){
  420. tlv = (struct tlv_header *)(flap->data + 4);
  421. len -= 4;
  422. }
  423. else
  424. tlv = (struct tlv_header *)(flap->data);
  425. }
  426. else {
  427. tlv = (struct tlv_header *)(flap->data + 10);
  428. len -= 10;
  429. }
  430. len1 = ntohs(flap->size);
  431. if(searchcookie(param, flap, len, &len1, tlv, 0)){RETURN (1118);}
  432. len = ntohs(flap->size) + 6;
  433. if((res=handledatfltsrv(param, &buf, &buflen, offset, &len))!=PASS) RETURN(res);
  434. if(socksend(param->clisock, buf+offset, len, conf.timeouts[STRING_S])!=len) {RETURN (1117);}
  435. offset = 0;
  436. }
  437. param->ndatfilterssrv++;
  438. newfilters = myalloc(param->ndatfilterssrv * sizeof(struct filterp *));
  439. if(param->ndatfilterssrv > 1){
  440. memcpy(newfilters, param->datfilterssrv, (param->ndatfilterssrv - 1) * sizeof(struct filterp *));
  441. myfree(param->datfilterssrv);
  442. }
  443. param->datfilterssrv = newfilters;
  444. newfilters[param->ndatfilterssrv - 1] = &icqfilterp;
  445. param->res = sockmap(param, conf.timeouts[CONNECTION_L]);
  446. param->ndatfilterssrv--;
  447. CLEANRET:
  448. (*param->srv->logfunc)(param, NULL);
  449. freeparam(param);
  450. if(buf) myfree(buf);
  451. return (NULL);
  452. }
  453. #ifdef WITHMAIN
  454. struct proxydef childdef = {
  455. icqprchild,
  456. 0,
  457. 0,
  458. S_ICQPR,
  459. ""
  460. };
  461. #include "proxymain.c"
  462. #endif