auth.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  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. int clientnegotiate(struct chain * redir, struct clientparam * param, struct sockaddr * addr){
  8. unsigned char *buf;
  9. unsigned char *username;
  10. int res;
  11. int len=0;
  12. unsigned char * user, *pass;
  13. user = redir->extuser;
  14. pass = redir->extpass;
  15. if (!param->srvbufsize){
  16. param->srvbufsize = SRVBUFSIZE;
  17. param->srvbuf = myalloc(param->srvbufsize);
  18. }
  19. buf = param->srvbuf;
  20. username = buf + 2048;
  21. if(user) {
  22. if (*user == '*') {
  23. if(!param->username) return 4;
  24. user = param->username;
  25. pass = param->password;
  26. }
  27. }
  28. switch(redir->type){
  29. case R_TCP:
  30. case R_HTTP:
  31. return 0;
  32. case R_CONNECT:
  33. case R_CONNECTP:
  34. {
  35. len = sprintf((char *)buf, "CONNECT ");
  36. if(redir->type == R_CONNECTP && param->hostname) {
  37. char * needreplace;
  38. needreplace = strchr((char *)param->hostname, ':');
  39. if(needreplace) buf[len++] = '[';
  40. len += sprintf((char *)buf + len, "%.256s", (char *)param->hostname);
  41. if(needreplace) buf[len++] = ']';
  42. }
  43. else {
  44. if(*SAFAMILY(addr) == AF_INET6) buf[len++] = '[';
  45. len += myinet_ntop(*SAFAMILY(addr), SAADDR(addr), (char *)buf+len, 256);
  46. if(*SAFAMILY(addr) == AF_INET6) buf[len++] = ']';
  47. }
  48. len += sprintf((char *)buf + len,
  49. ":%hu HTTP/1.0\r\nProxy-Connection: keep-alive\r\n", ntohs(*SAPORT(addr)));
  50. if(user){
  51. len += sprintf((char *)buf + len, "Proxy-authorization: basic ");
  52. sprintf((char *)username, "%.128s:%.128s", user, pass?pass:(unsigned char *)"");
  53. en64(username, buf+len, (int)strlen((char *)username));
  54. len = (int)strlen((char *)buf);
  55. len += sprintf((char *)buf + len, "\r\n");
  56. }
  57. len += sprintf((char *)buf + len, "\r\n");
  58. if(socksend(param->remsock, buf, len, conf.timeouts[CHAIN_TO]) != (int)strlen((char *)buf))
  59. return 31;
  60. param->statssrv64+=len;
  61. param->nwrites++;
  62. if((res = sockgetlinebuf(param, SERVER,buf,13,'\n',conf.timeouts[CHAIN_TO])) < 13)
  63. return 32;
  64. if(buf[9] != '2') return 33;
  65. while((res = sockgetlinebuf(param, SERVER,buf,1023,'\n', conf.timeouts[CHAIN_TO])) > 2);
  66. if(res <= 0) return 34;
  67. return 0;
  68. }
  69. case R_SOCKS4:
  70. case R_SOCKS4P:
  71. case R_SOCKS4B:
  72. {
  73. if(*SAFAMILY(addr) != AF_INET) return 44;
  74. buf[0] = 4;
  75. buf[1] = 1;
  76. memcpy(buf+2, SAPORT(addr), 2);
  77. if(redir->type == R_SOCKS4P && param->hostname) {
  78. buf[4] = buf[5] = buf[6] = 0;
  79. buf[7] = 3;
  80. }
  81. else memcpy(buf+4, SAADDR(addr), 4);
  82. if(!user)user = (unsigned char *)"anonymous";
  83. len = (int)strlen((char *)user) + 1;
  84. memcpy(buf+8, user, len);
  85. len += 8;
  86. if(redir->type == R_SOCKS4P && param->hostname) {
  87. int hostnamelen;
  88. hostnamelen = (int)strlen((char *)param->hostname) + 1;
  89. if(hostnamelen > 255) hostnamelen = 255;
  90. memcpy(buf+len, param->hostname, hostnamelen);
  91. len += hostnamelen;
  92. }
  93. if(socksend(param->remsock, buf, len, conf.timeouts[CHAIN_TO]) < len){
  94. return 41;
  95. }
  96. param->statssrv64+=len;
  97. param->nwrites++;
  98. if((len = sockgetlinebuf(param, SERVER, buf, (redir->type == R_SOCKS4B)? 3:8, EOF, conf.timeouts[CHAIN_TO])) != ((redir->type == R_SOCKS4B)? 3:8)){
  99. return 42;
  100. }
  101. if(buf[1] != 90) {
  102. return 43;
  103. }
  104. }
  105. return 0;
  106. case R_SOCKS5:
  107. case R_SOCKS5P:
  108. case R_SOCKS5B:
  109. {
  110. int inbuf = 0;
  111. buf[0] = 5;
  112. buf[1] = 1;
  113. buf[2] = user? 2 : 0;
  114. if(socksend(param->remsock, buf, 3, conf.timeouts[CHAIN_TO]) != 3){
  115. return 51;
  116. }
  117. param->statssrv64+=len;
  118. param->nwrites++;
  119. if(sockgetlinebuf(param, SERVER, buf, 2, EOF, conf.timeouts[CHAIN_TO]) != 2){
  120. return 52;
  121. }
  122. if(buf[0] != 5) {
  123. return 53;
  124. }
  125. if(buf[1] != 0 && !(buf[1] == 2 && user)){
  126. return 54;
  127. }
  128. if(buf[1] == 2){
  129. buf[inbuf++] = 1;
  130. buf[inbuf] = (unsigned char)strlen((char *)user);
  131. memcpy(buf+inbuf+1, user, buf[inbuf]);
  132. inbuf += buf[inbuf] + 1;
  133. buf[inbuf] = pass?(unsigned char)strlen((char *)pass):0;
  134. if(pass)memcpy(buf+inbuf+1, pass, buf[inbuf]);
  135. inbuf += buf[inbuf] + 1;
  136. if(socksend(param->remsock, buf, inbuf, conf.timeouts[CHAIN_TO]) != inbuf){
  137. return 51;
  138. }
  139. param->statssrv64+=inbuf;
  140. param->nwrites++;
  141. if(sockgetlinebuf(param, SERVER, buf, 2, EOF, 60) != 2){
  142. return 55;
  143. }
  144. if(buf[0] != 1 || buf[1] != 0) {
  145. return 56;
  146. }
  147. }
  148. buf[0] = 5;
  149. buf[1] = 1;
  150. buf[2] = 0;
  151. if(redir->type == R_SOCKS5P && param->hostname) {
  152. buf[3] = 3;
  153. len = (int)strlen((char *)param->hostname);
  154. if(len > 255) len = 255;
  155. buf[4] = len;
  156. memcpy(buf + 5, param->hostname, len);
  157. len += 5;
  158. }
  159. else {
  160. len = 3;
  161. buf[len++] = (*SAFAMILY(addr) == AF_INET)? 1 : 4;
  162. memcpy(buf+len, SAADDR(addr), SAADDRLEN(addr));
  163. len += SAADDRLEN(addr);
  164. }
  165. memcpy(buf+len, SAPORT(addr), 2);
  166. len += 2;
  167. if(socksend(param->remsock, buf, len, conf.timeouts[CHAIN_TO]) != len){
  168. return 51;
  169. }
  170. param->statssrv64+=len;
  171. param->nwrites++;
  172. if(sockgetlinebuf(param, SERVER, buf, 4, EOF, conf.timeouts[CHAIN_TO]) != 4){
  173. return 57;
  174. }
  175. if(buf[0] != 5) {
  176. return 53;
  177. }
  178. if(buf[1] != 0) {
  179. return 60 + (buf[1] % 10);
  180. }
  181. switch (buf[3]) {
  182. case 1:
  183. if (redir->type == R_SOCKS5B || sockgetlinebuf(param, SERVER, buf, 6, EOF, conf.timeouts[CHAIN_TO]) == 6)
  184. break;
  185. return 59;
  186. case 3:
  187. if (sockgetlinebuf(param, SERVER, buf, 256, 0, conf.timeouts[CHAIN_TO]) > 1)
  188. break;
  189. return 59;
  190. case 4:
  191. if (sockgetlinebuf(param, SERVER, buf, 18, EOF, conf.timeouts[CHAIN_TO]) == 18)
  192. break;
  193. return 59;
  194. default:
  195. return 58;
  196. }
  197. return 0;
  198. }
  199. default:
  200. return 30;
  201. }
  202. }
  203. int handleredirect(struct clientparam * param, struct ace * acentry){
  204. int connected = 0;
  205. int weight = 1000;
  206. int res;
  207. int done = 0;
  208. struct chain * cur;
  209. struct chain * redir = NULL;
  210. int r2;
  211. if(param->remsock != INVALID_SOCKET) {
  212. return 0;
  213. }
  214. if(SAISNULL(&param->req) || !*SAPORT(&param->req)) {
  215. return 100;
  216. }
  217. r2 = (myrand(param, sizeof(struct clientparam))%1000);
  218. for(cur = acentry->chains; cur; cur=cur->next){
  219. if(((weight = weight - cur->weight) > r2)|| done) {
  220. if(weight <= 0) {
  221. weight += 1000;
  222. done = 0;
  223. r2 = (myrand(param, sizeof(struct clientparam))%1000);
  224. }
  225. continue;
  226. }
  227. param->redirected++;
  228. done = 1;
  229. if(weight <= 0) {
  230. weight += 1000;
  231. done = 0;
  232. r2 = (myrand(param, sizeof(struct clientparam))%1000);
  233. }
  234. if(!connected){
  235. if(cur->type == R_EXTIP){
  236. param->sinsl = cur->addr;
  237. if(cur->next)continue;
  238. return 0;
  239. }
  240. else if(SAISNULL(&cur->addr) && !*SAPORT(&cur->addr)){
  241. if(cur->extuser){
  242. if(param->extusername)
  243. myfree(param->extusername);
  244. param->extusername = (unsigned char *)mystrdup((char *)((*cur->extuser == '*' && param->username)? param->username : cur->extuser));
  245. if(cur->extpass){
  246. if(param->extpassword)
  247. myfree(param->extpassword);
  248. param->extpassword = (unsigned char *)mystrdup((char *)((*cur->extuser == '*' && param->password)?param->password : cur->extpass));
  249. }
  250. if(*cur->extuser == '*' && !param->username) return 4;
  251. }
  252. switch(cur->type){
  253. case R_POP3:
  254. param->redirectfunc = pop3pchild;
  255. break;
  256. case R_FTP:
  257. param->redirectfunc = ftpprchild;
  258. break;
  259. case R_ADMIN:
  260. param->redirectfunc = adminchild;
  261. break;
  262. case R_ICQ:
  263. param->redirectfunc = icqprchild;
  264. break;
  265. case R_SMTP:
  266. param->redirectfunc = smtppchild;
  267. break;
  268. default:
  269. param->redirectfunc = proxychild;
  270. }
  271. if(cur->next)continue;
  272. return 0;
  273. }
  274. else if(!*SAPORT(&cur->addr) && !SAISNULL(&cur->addr)) {
  275. unsigned short port = *SAPORT(&param->sinsr);
  276. param->sinsr = cur->addr;
  277. *SAPORT(&param->sinsr) = port;
  278. }
  279. else if(SAISNULL(&cur->addr) && *SAPORT(&cur->addr)) *SAPORT(&param->sinsr) = *SAPORT(&cur->addr);
  280. else {
  281. param->sinsr = cur->addr;
  282. }
  283. if((res = alwaysauth(param))){
  284. return (res == 10)? res : 60+res;
  285. }
  286. }
  287. else {
  288. res = (redir)?clientnegotiate(redir, param, (struct sockaddr *)&cur->addr):0;
  289. if(res) return res;
  290. }
  291. redir = cur;
  292. param->redirtype = redir->type;
  293. if(redir->type == R_TCP || redir->type ==R_HTTP) {
  294. if(cur->extuser){
  295. if(*cur -> extuser == '*' && !param->username) return 4;
  296. if(param->extusername)
  297. myfree(param->extusername);
  298. param->extusername = (unsigned char *)mystrdup((char *)((*cur->extuser == '*' && param->username)? param->username : cur->extuser));
  299. if(cur->extpass){
  300. if(param->extpassword)
  301. myfree(param->extpassword);
  302. param->extpassword = (unsigned char *)mystrdup((char *)((*cur->extuser == '*' && param->password)?param->password : cur->extpass));
  303. }
  304. }
  305. return 0;
  306. }
  307. connected = 1;
  308. }
  309. if(!connected || !redir) return 0;
  310. return clientnegotiate(redir, param, (struct sockaddr *)&param->req);
  311. }
  312. int IPInentry(struct sockaddr *sa, struct iplist *ipentry){
  313. int addrlen;
  314. unsigned char *ip, *ipf, *ipt;
  315. if(!sa || ! ipentry || *SAFAMILY(sa) != ipentry->family) return 0;
  316. ip = (unsigned char *)SAADDR(sa);
  317. ipf = (unsigned char *)&ipentry->ip_from;
  318. ipt = (unsigned char *)&ipentry->ip_to;
  319. addrlen = SAADDRLEN(sa);
  320. if(memcmp(ip,ipf,addrlen) < 0 || memcmp(ip,ipt,addrlen) > 0) return 0;
  321. return 1;
  322. }
  323. int ACLmatches(struct ace* acentry, struct clientparam * param){
  324. struct userlist * userentry;
  325. struct iplist *ipentry;
  326. struct portlist *portentry;
  327. struct period *periodentry;
  328. unsigned char * username;
  329. struct hostname * hstentry=NULL;
  330. int i;
  331. int match = 0;
  332. username = param->username?param->username:(unsigned char *)"-";
  333. if(acentry->src) {
  334. for(ipentry = acentry->src; ipentry; ipentry = ipentry->next)
  335. if(IPInentry((struct sockaddr *)&param->sincr, ipentry)) {
  336. break;
  337. }
  338. if(!ipentry) return 0;
  339. }
  340. if((acentry->dst && !SAISNULL(&param->req)) || (acentry->dstnames && param->hostname)) {
  341. for(ipentry = acentry->dst; ipentry; ipentry = ipentry->next)
  342. if(IPInentry((struct sockaddr *)&param->req, ipentry)) {
  343. break;
  344. }
  345. if(!ipentry) {
  346. if(acentry->dstnames && param->hostname){
  347. for(i=0; param->hostname[i]; i++){
  348. param->hostname[i] = tolower(param->hostname[i]);
  349. }
  350. while(i > 5 && param->hostname[i-1] == '.') param->hostname[i-1] = 0;
  351. for(hstentry = acentry->dstnames; hstentry; hstentry = hstentry->next){
  352. switch(hstentry->matchtype){
  353. case 0:
  354. if(strstr((char *)param->hostname, (char *)hstentry->name)) match = 1;
  355. break;
  356. case 1:
  357. if(strstr((char *)param->hostname, (char *)hstentry->name) == (char *)param->hostname) match = 1;
  358. break;
  359. case 2:
  360. if(strstr((char *)param->hostname, (char *)hstentry->name) == (char *)(param->hostname + i - (strlen((char *)hstentry->name)))) match = 1;
  361. break;
  362. default:
  363. if(!strcmp((char *)param->hostname, (char *)hstentry->name)) match = 1;
  364. break;
  365. }
  366. if(match) break;
  367. }
  368. }
  369. }
  370. if(!ipentry && !hstentry) return 0;
  371. }
  372. if(acentry->ports && *SAPORT(&param->req)) {
  373. for (portentry = acentry->ports; portentry; portentry = portentry->next)
  374. if(ntohs(*SAPORT(&param->req)) >= portentry->startport &&
  375. ntohs(*SAPORT(&param->req)) <= portentry->endport) {
  376. break;
  377. }
  378. if(!portentry) return 0;
  379. }
  380. if(acentry->wdays){
  381. if(!(acentry -> wdays & wday)) return 0;
  382. }
  383. if(acentry->periods){
  384. int start_time = (int)(param->time_start - basetime);
  385. for(periodentry = acentry->periods; periodentry; periodentry = periodentry -> next)
  386. if(start_time >= periodentry->fromtime && start_time < periodentry->totime){
  387. break;
  388. }
  389. if(!periodentry) return 0;
  390. }
  391. if(acentry->users){
  392. for(userentry = acentry->users; userentry; userentry = userentry->next)
  393. if(!strcmp((char *)username, (char *)userentry->user)){
  394. break;
  395. }
  396. if(!userentry) return 0;
  397. }
  398. if(acentry->operation) {
  399. if((acentry->operation & param->operation) != param->operation){
  400. return 0;
  401. }
  402. }
  403. if(acentry->weight && (acentry->weight < param->weight)) return 0;
  404. return 1;
  405. }
  406. static void initbandlims (struct clientparam *param){
  407. struct bandlim * be;
  408. int i;
  409. for(i=0, be = conf.bandlimiter; be && i<MAXBANDLIMS; be = be->next) {
  410. if(ACLmatches(be->ace, param)){
  411. if(be->ace->action == NOBANDLIM) {
  412. break;
  413. }
  414. param->bandlims[i++] = be;
  415. param->bandlimfunc = conf.bandlimfunc;
  416. }
  417. }
  418. if(i<MAXBANDLIMS)param->bandlims[i] = NULL;
  419. for(i=0, be = conf.bandlimiterout; be && i<MAXBANDLIMS; be = be->next) {
  420. if(ACLmatches(be->ace, param)){
  421. if(be->ace->action == NOBANDLIM) {
  422. break;
  423. }
  424. param->bandlimsout[i++] = be;
  425. param->bandlimfunc = conf.bandlimfunc;
  426. }
  427. }
  428. if(i<MAXBANDLIMS)param->bandlimsout[i] = NULL;
  429. }
  430. unsigned bandlimitfunc(struct clientparam *param, unsigned nbytesin, unsigned nbytesout){
  431. unsigned sleeptime = 0, nsleeptime;
  432. unsigned long sec;
  433. unsigned msec;
  434. unsigned now;
  435. int i;
  436. #ifdef _WIN32
  437. struct timeb tb;
  438. ftime(&tb);
  439. sec = (unsigned)tb.time;
  440. msec = (unsigned)tb.millitm*1000;
  441. #else
  442. struct timeval tv;
  443. gettimeofday(&tv, NULL);
  444. sec = tv.tv_sec;
  445. msec = tv.tv_usec;
  446. #endif
  447. if(!nbytesin && !nbytesout) return 0;
  448. pthread_mutex_lock(&bandlim_mutex);
  449. if(param->paused != conf.paused && param->bandlimver != conf.paused){
  450. if(!conf.bandlimfunc){
  451. param->bandlimfunc = NULL;
  452. pthread_mutex_unlock(&bandlim_mutex);
  453. return 0;
  454. }
  455. initbandlims(param);
  456. param->bandlimver = conf.paused;
  457. }
  458. for(i=0; nbytesin&& i<MAXBANDLIMS && param->bandlims[i]; i++){
  459. if( !param->bandlims[i]->basetime ||
  460. param->bandlims[i]->basetime > sec ||
  461. param->bandlims[i]->basetime < (sec - 120)
  462. )
  463. {
  464. param->bandlims[i]->basetime = sec;
  465. param->bandlims[i]->nexttime = 0;
  466. continue;
  467. }
  468. now = ((sec - param->bandlims[i]->basetime) * 1000000) + msec;
  469. nsleeptime = (param->bandlims[i]->nexttime > now)?
  470. param->bandlims[i]->nexttime - now : 0;
  471. sleeptime = (nsleeptime > sleeptime)? nsleeptime : sleeptime;
  472. param->bandlims[i]->basetime = sec;
  473. param->bandlims[i]->nexttime = msec + nsleeptime + ((param->bandlims[i]->rate > 1000000)? ((nbytesin/32)*(256000000/param->bandlims[i]->rate)) : (nbytesin * (8000000/param->bandlims[i]->rate)));
  474. }
  475. for(i=0; nbytesout && i<MAXBANDLIMS && param->bandlimsout[i]; i++){
  476. if( !param->bandlimsout[i]->basetime ||
  477. param->bandlimsout[i]->basetime > sec ||
  478. param->bandlimsout[i]->basetime < (sec - 120)
  479. )
  480. {
  481. param->bandlimsout[i]->basetime = sec;
  482. param->bandlimsout[i]->nexttime = 0;
  483. continue;
  484. }
  485. now = ((sec - param->bandlimsout[i]->basetime) * 1000000) + msec;
  486. nsleeptime = (param->bandlimsout[i]->nexttime > now)?
  487. param->bandlimsout[i]->nexttime - now : 0;
  488. sleeptime = (nsleeptime > sleeptime)? nsleeptime : sleeptime;
  489. param->bandlimsout[i]->basetime = sec;
  490. param->bandlimsout[i]->nexttime = msec + nsleeptime + ((param->bandlimsout[i]->rate > 1000000)? ((nbytesout/32)*(256000000/param->bandlimsout[i]->rate)) : (nbytesout * (8000000/param->bandlimsout[i]->rate)));
  491. }
  492. pthread_mutex_unlock(&bandlim_mutex);
  493. return sleeptime/1000;
  494. }
  495. void trafcountfunc(struct clientparam *param){
  496. struct trafcount * tc;
  497. int countout = 0;
  498. pthread_mutex_lock(&tc_mutex);
  499. for(tc = conf.trafcounter; tc; tc = tc->next) {
  500. if(ACLmatches(tc->ace, param)){
  501. time_t t;
  502. if(tc->ace->action == NOCOUNTIN) break;
  503. if(tc->ace->action != COUNTIN) {
  504. countout = 1;
  505. continue;
  506. }
  507. tc->traf64 += param->statssrv64;
  508. time(&t);
  509. tc->updated = t;
  510. }
  511. }
  512. if(countout) for(tc = conf.trafcounter; tc; tc = tc->next) {
  513. if(ACLmatches(tc->ace, param)){
  514. time_t t;
  515. if(tc->ace->action == NOCOUNTOUT) break;
  516. if(tc->ace->action != COUNTOUT) {
  517. continue;
  518. }
  519. tc->traf64 += param->statscli64;
  520. time(&t);
  521. tc->updated = t;
  522. }
  523. }
  524. pthread_mutex_unlock(&tc_mutex);
  525. }
  526. int alwaysauth(struct clientparam * param){
  527. int res;
  528. struct trafcount * tc;
  529. int countout = 0;
  530. res = doconnect(param);
  531. if(!res){
  532. initbandlims(param);
  533. for(tc = conf.trafcounter; tc; tc = tc->next) {
  534. if(tc->disabled) continue;
  535. if(ACLmatches(tc->ace, param)){
  536. if(tc->ace->action == NOCOUNTIN) break;
  537. if(tc->ace->action != COUNTIN) {
  538. countout = 1;
  539. continue;
  540. }
  541. if(tc->traflim64 <= tc->traf64) return 10;
  542. param->trafcountfunc = conf.trafcountfunc;
  543. param->maxtrafin64 = tc->traflim64 - tc->traf64;
  544. }
  545. }
  546. if(countout)for(tc = conf.trafcounter; tc; tc = tc->next) {
  547. if(tc->disabled) continue;
  548. if(ACLmatches(tc->ace, param)){
  549. if(tc->ace->action == NOCOUNTOUT) break;
  550. if(tc->ace->action != COUNTOUT) {
  551. continue;
  552. }
  553. if(tc->traflim64 <= tc->traf64) return 10;
  554. param->trafcountfunc = conf.trafcountfunc;
  555. param->maxtrafout64 = tc->traflim64 - tc->traf64;
  556. }
  557. }
  558. }
  559. return res;
  560. }
  561. int checkACL(struct clientparam * param){
  562. struct ace* acentry;
  563. if(!param->srv->acl) {
  564. return alwaysauth(param);
  565. }
  566. for(acentry = param->srv->acl; acentry; acentry = acentry->next) {
  567. if(ACLmatches(acentry, param)) {
  568. param->nolog = acentry->nolog;
  569. param->weight = acentry->weight;
  570. if(acentry->action == 2) {
  571. struct ace dup;
  572. if(param->operation < 256 && !(param->operation & CONNECT)){
  573. continue;
  574. }
  575. if(param->redirected && acentry->chains && SAISNULL(&acentry->chains->addr) && !*SAPORT(&acentry->chains->addr)) {
  576. continue;
  577. }
  578. dup = *acentry;
  579. return handleredirect(param, &dup);
  580. }
  581. return acentry->action;
  582. }
  583. }
  584. return 3;
  585. }
  586. struct authcache {
  587. char * username;
  588. char * password;
  589. time_t expires;
  590. #ifndef NOIPV6
  591. struct sockaddr_in6 sa;
  592. #else
  593. struct sockaddr_in sa;
  594. #endif
  595. struct authcache *next;
  596. } *authc = NULL;
  597. int cacheauth(struct clientparam * param){
  598. struct authcache *ac, *last=NULL;
  599. pthread_mutex_lock(&hash_mutex);
  600. for(ac = authc; ac; ){
  601. if(ac->expires <= conf.time){
  602. if(ac->username)myfree(ac->username);
  603. if(ac->password)myfree(ac->password);
  604. if(!last){
  605. authc = ac->next;
  606. myfree(ac);
  607. ac = authc;
  608. }
  609. else {
  610. last->next = ac->next;
  611. myfree(ac);
  612. ac = last->next;
  613. }
  614. continue;
  615. }
  616. if(((!(conf.authcachetype&2)) || (param->username && ac->username && !strcmp(ac->username, (char *)param->username))) &&
  617. ((!(conf.authcachetype&1)) || (*SAFAMILY(&ac->sa) == *SAFAMILY(&param->sincr) && !memcmp(SAADDR(&ac->sa), SAADDR(&param->sincr), SAADDRLEN(&ac->sa)))) &&
  618. (!(conf.authcachetype&4) || (ac->password && param->password && !strcmp(ac->password, (char *)param->password)))) {
  619. if(param->username){
  620. myfree(param->username);
  621. }
  622. param->username = (unsigned char *)mystrdup(ac->username);
  623. pthread_mutex_unlock(&hash_mutex);
  624. return 0;
  625. }
  626. last = ac;
  627. ac = ac->next;
  628. }
  629. pthread_mutex_unlock(&hash_mutex);
  630. return 4;
  631. }
  632. int doauth(struct clientparam * param){
  633. int res = 0;
  634. struct auth *authfuncs;
  635. struct authcache *ac;
  636. char * tmp;
  637. int ret = 0;
  638. for(authfuncs=param->srv->authfuncs; authfuncs; authfuncs=authfuncs->next){
  639. res = authfuncs->authenticate?(*authfuncs->authenticate)(param):0;
  640. if(!res) {
  641. if(authfuncs->authorize &&
  642. (res = (*authfuncs->authorize)(param)))
  643. return res;
  644. if(conf.authcachetype && authfuncs->authenticate && authfuncs->authenticate != cacheauth && param->username && (!(conf.authcachetype&4) || (!param->pwtype && param->password))){
  645. pthread_mutex_lock(&hash_mutex);
  646. for(ac = authc; ac; ac = ac->next){
  647. if((!(conf.authcachetype&2) || !strcmp(ac->username, (char *)param->username)) &&
  648. (!(conf.authcachetype&1) || (*SAFAMILY(&ac->sa) == *SAFAMILY(&param->sincr) && !memcmp(SAADDR(&ac->sa), SAADDR(&param->sincr), SAADDRLEN(&ac->sa)))) &&
  649. (!(conf.authcachetype&4) || (ac->password && !strcmp(ac->password, (char *)param->password)))) {
  650. ac->expires = conf.time + conf.authcachetime;
  651. if(strcmp(ac->username, (char *)param->username)){
  652. tmp = ac->username;
  653. ac->username = mystrdup((char *)param->username);
  654. myfree(tmp);
  655. }
  656. if((conf.authcachetype&4)){
  657. tmp = ac->password;
  658. ac->password = mystrdup((char *)param->password);
  659. myfree(tmp);
  660. }
  661. ac->sa = param->sincr;
  662. break;
  663. }
  664. }
  665. if(!ac){
  666. ac = myalloc(sizeof(struct authcache));
  667. if(ac){
  668. ac->expires = conf.time + conf.authcachetime;
  669. ac->username = param->username?mystrdup((char *)param->username):NULL;
  670. ac->sa = param->sincr;
  671. ac->password = NULL;
  672. if((conf.authcachetype&4) && param->password) ac->password = mystrdup((char *)param->password);
  673. }
  674. ac->next = authc;
  675. authc = ac;
  676. }
  677. pthread_mutex_unlock(&hash_mutex);
  678. }
  679. break;
  680. }
  681. if(res > ret) ret = res;
  682. }
  683. if(!res){
  684. return alwaysauth(param);
  685. }
  686. return ret;
  687. }
  688. int ipauth(struct clientparam * param){
  689. int res;
  690. unsigned char *username;
  691. username = param->username;
  692. param->username = NULL;
  693. res = checkACL(param);
  694. param->username = username;
  695. return res;
  696. }
  697. int userauth(struct clientparam * param){
  698. return (param->username)? 0:4;
  699. }
  700. int dnsauth(struct clientparam * param){
  701. char buf[128];
  702. char addr[16];
  703. char dig[]="0123456789abcdef";
  704. unsigned u;
  705. int i;
  706. if(*SAFAMILY(&param->sincr)!=AF_INET){
  707. char *s = buf;
  708. for(i=15; i>=0; i--){
  709. unsigned char c=((unsigned char *)SAADDR(&param->sincr))[i];
  710. *s++ = dig[(c&0xf)];
  711. *s++ = '.';
  712. *s++ = dig[(c>>4)];
  713. *s++ = '.';
  714. }
  715. sprintf(s, "ip6.arpa");
  716. }
  717. else {
  718. u = ntohl(*(unsigned long *)SAADDR(&param->sincr));
  719. sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
  720. ((u&0x000000FF)),
  721. ((u&0x0000FF00)>>8),
  722. ((u&0x00FF0000)>>16),
  723. ((u&0xFF000000)>>24));
  724. }
  725. if(!udpresolve(*SAFAMILY(&param->sincr), (unsigned char *)buf, (unsigned char *)addr, NULL, param, 1)) {
  726. return 3;
  727. }
  728. if(memcmp(SAADDR(&param->sincr), addr, SAADDRLEN(&param->sincr))) {
  729. return 3;
  730. }
  731. return param->username? 0:3;
  732. }
  733. int strongauth(struct clientparam * param){
  734. struct passwords * pwl;
  735. unsigned char buf[256];
  736. if(!param->username) return 4;
  737. pthread_mutex_lock(&pwl_mutex);
  738. for(pwl = conf.pwl; pwl; pwl=pwl->next){
  739. if(!strcmp((char *)pwl->user, (char *)param->username)) switch(pwl->pwtype) {
  740. case CL:
  741. if(!pwl->password || !*pwl->password){
  742. break;
  743. }
  744. else if (!param->pwtype && param->password && !strcmp((char *)param->password, (char *)pwl->password)){
  745. break;
  746. }
  747. #ifndef NOCRYPT
  748. else if (param->pwtype == 2 && param->password) {
  749. ntpwdhash(buf, pwl->password, 0);
  750. mschap(buf, param->password, buf + 16);
  751. if(!memcmp(buf+16, param->password+8, 24)) {
  752. break;
  753. }
  754. }
  755. #endif
  756. pthread_mutex_unlock(&pwl_mutex);
  757. return 6;
  758. #ifndef NOCRYPT
  759. case CR:
  760. if(param->password && !param->pwtype && !strcmp((char *)pwl->password, (char *)mycrypt(param->password, pwl->password,buf))) {
  761. break;
  762. }
  763. pthread_mutex_unlock(&pwl_mutex);
  764. return 7;
  765. case NT:
  766. if(param->password && !param->pwtype && !memcmp(pwl->password, ntpwdhash(buf,param->password, 1), 32)) {
  767. break;
  768. }
  769. else if (param->pwtype == 2){
  770. fromhex(pwl->password, buf, 16);
  771. mschap(buf, param->password, buf + 16);
  772. if(!memcmp(buf + 16, param->password+8, 24)) {
  773. break;
  774. }
  775. }
  776. pthread_mutex_unlock(&pwl_mutex);
  777. return 8;
  778. #endif
  779. default:
  780. pthread_mutex_unlock(&pwl_mutex);
  781. return 999;
  782. }
  783. else continue;
  784. pthread_mutex_unlock(&pwl_mutex);
  785. return 0;
  786. }
  787. pthread_mutex_unlock(&pwl_mutex);
  788. return 5;
  789. }
  790. struct auth authfuncs[] = {
  791. {authfuncs+1, NULL, NULL, ""},
  792. {authfuncs+2, ipauth, NULL, "iponly"},
  793. {authfuncs+3, userauth, checkACL, "useronly"},
  794. {authfuncs+4, dnsauth, checkACL, "dnsname"},
  795. {authfuncs+5, strongauth, checkACL, "strong"},
  796. {authfuncs+6, cacheauth, checkACL, "cache"},
  797. {authfuncs+7, NULL, NULL, "none"},
  798. {NULL, NULL, NULL, ""}
  799. };
  800. struct hashtable dns_table = {0, 4, {0,0,0,0}, NULL, NULL, NULL};
  801. struct hashtable dns6_table = {0, 16, {0,0,0,0}, NULL, NULL, NULL};
  802. void nametohash(const unsigned char * name, unsigned char *hash, unsigned char *rnd){
  803. unsigned i, j, k;
  804. memcpy(hash, rnd, sizeof(unsigned)*4);
  805. for(i=0, j=0, k=0; name[j]; j++){
  806. hash[i] += (toupper(name[j]) - 32)+rnd[((toupper(name[j]))*29277+rnd[(k+j+i)%16]+k+j+i)%16];
  807. if(++i == sizeof(unsigned)*4) {
  808. i = 0;
  809. k++;
  810. }
  811. }
  812. }
  813. unsigned hashindex(struct hashtable *ht, const unsigned char* hash){
  814. unsigned t1, t2, t3, t4;
  815. t1 = *(unsigned *)hash;
  816. t2 = *(unsigned *)(hash + sizeof(unsigned));
  817. t3 = *(unsigned *)(hash + (2*sizeof(unsigned)));
  818. t4 = *(unsigned *)(hash + (3*sizeof(unsigned)));
  819. return (t1 + (t2 * 7) + (t3 * 17) + (t4 * 29) ) % (ht->hashsize >> 2);
  820. }
  821. void destroyhashtable(struct hashtable *ht){
  822. pthread_mutex_lock(&hash_mutex);
  823. if(ht->hashtable){
  824. myfree(ht->hashtable);
  825. ht->hashtable = NULL;
  826. }
  827. if(ht->hashvalues){
  828. myfree(ht->hashvalues);
  829. ht->hashvalues = NULL;
  830. }
  831. ht->hashsize = 0;
  832. pthread_mutex_unlock(&hash_mutex);
  833. }
  834. #define hvalue(I) ((struct hashentry *)((char *)ht->hashvalues + (I)*(sizeof(struct hashentry) + ht->recsize - 4)))
  835. int inithashtable(struct hashtable *ht, unsigned nhashsize){
  836. unsigned i;
  837. clock_t c;
  838. #ifdef _WIN32
  839. struct timeb tb;
  840. ftime(&tb);
  841. #else
  842. struct timeval tb;
  843. struct timezone tz;
  844. gettimeofday(&tb, &tz);
  845. #endif
  846. c = clock();
  847. if(nhashsize<4) return 1;
  848. pthread_mutex_lock(&hash_mutex);
  849. if(ht->hashtable){
  850. myfree(ht->hashtable);
  851. ht->hashtable = NULL;
  852. }
  853. if(ht->hashvalues){
  854. myfree(ht->hashvalues);
  855. ht->hashvalues = NULL;
  856. }
  857. ht->hashsize = 0;
  858. if(!(ht->hashtable = myalloc((nhashsize>>2) * sizeof(struct hashentry *)))){
  859. pthread_mutex_unlock(&hash_mutex);
  860. return 2;
  861. }
  862. if(!(ht->hashvalues = myalloc(nhashsize * (sizeof(struct hashentry) + (ht->recsize-4))))){
  863. myfree(ht->hashtable);
  864. ht->hashtable = NULL;
  865. pthread_mutex_unlock(&hash_mutex);
  866. return 3;
  867. }
  868. ht->hashsize = nhashsize;
  869. ht->rnd[0] = myrand(&tb, sizeof(tb));
  870. ht->rnd[1] = myrand(ht->hashtable, sizeof(ht->hashtable));
  871. ht->rnd[2] = myrand(&c, sizeof(c));
  872. ht->rnd[3] = myrand(ht->hashvalues,sizeof(ht->hashvalues));
  873. memset(ht->hashtable, 0, (ht->hashsize>>2) * sizeof(struct hashentry *));
  874. memset(ht->hashvalues, 0, ht->hashsize * (sizeof(struct hashentry) + ht->recsize -4));
  875. for(i = 0; i< (ht->hashsize - 1); i++) {
  876. hvalue(i)->next = hvalue(i+1);
  877. }
  878. ht->hashempty = ht->hashvalues;
  879. pthread_mutex_unlock(&hash_mutex);
  880. return 0;
  881. }
  882. void hashadd(struct hashtable *ht, const unsigned char* name, unsigned char* value, time_t expires){
  883. struct hashentry * hen, *he;
  884. struct hashentry ** hep;
  885. unsigned index;
  886. pthread_mutex_lock(&hash_mutex);
  887. if(!ht||!value||!name||!ht->hashtable||!ht->hashempty) {
  888. pthread_mutex_unlock(&hash_mutex);
  889. return;
  890. }
  891. hen = ht->hashempty;
  892. ht->hashempty = ht->hashempty->next;
  893. nametohash(name, hen->hash, (unsigned char *)ht->rnd);
  894. memcpy(hen->value, value, ht->recsize);
  895. hen->expires = expires;
  896. hen->next = NULL;
  897. index = hashindex(ht, hen->hash);
  898. for(hep = ht->hashtable + index; (he = *hep)!=NULL; ){
  899. if(he->expires < conf.time || !memcmp(hen->hash, he->hash, sizeof(he->hash))) {
  900. (*hep) = he->next;
  901. he->expires = 0;
  902. he->next = ht->hashempty;
  903. ht->hashempty = he;
  904. }
  905. else hep=&(he->next);
  906. }
  907. hen->next = ht->hashtable[index];
  908. ht->hashtable[index] = hen;
  909. pthread_mutex_unlock(&hash_mutex);
  910. }
  911. unsigned long hashresolv(struct hashtable *ht, const unsigned char* name, unsigned char* value, unsigned *ttl){
  912. unsigned char hash[sizeof(unsigned)*4];
  913. struct hashentry ** hep;
  914. struct hashentry *he;
  915. unsigned index;
  916. pthread_mutex_lock(&hash_mutex);
  917. if(!ht || !ht->hashtable || !name) {
  918. pthread_mutex_unlock(&hash_mutex);
  919. return 0;
  920. }
  921. nametohash(name, hash, (unsigned char *)ht->rnd);
  922. index = hashindex(ht, hash);
  923. for(hep = ht->hashtable + index; (he = *hep)!=NULL; ){
  924. if(he->expires < conf.time) {
  925. (*hep) = he->next;
  926. he->expires = 0;
  927. he->next = ht->hashempty;
  928. ht->hashempty = he;
  929. }
  930. else if(!memcmp(hash, he->hash, sizeof(unsigned)*4)){
  931. if(ttl) *ttl = (unsigned)(he->expires - conf.time);
  932. memcpy(value, he->value, ht->recsize);
  933. pthread_mutex_unlock(&hash_mutex);
  934. return 1;
  935. }
  936. else hep=&(he->next);
  937. }
  938. pthread_mutex_unlock(&hash_mutex);
  939. return 0;
  940. }
  941. struct nserver nservers[MAXNSERVERS] = {{{0},0}, {{0},0}, {{0},0}, {{0},0}, {{0},0}};
  942. struct nserver authnserver;
  943. unsigned long udpresolve(int af, unsigned char * name, unsigned char * value, unsigned *retttl, struct clientparam* param, int makeauth){
  944. int i,n;
  945. unsigned long retval;
  946. if((af == AF_INET) && (retval = hashresolv(&dns_table, name, value, retttl))) {
  947. return retval;
  948. }
  949. if((af == AF_INET6) && (retval = hashresolv(&dns6_table, name, value, retttl))) {
  950. return retval;
  951. }
  952. n = (makeauth && !SAISNULL(&authnserver.addr))? 1 : numservers;
  953. for(i=0; i<n; i++){
  954. unsigned short nq, na;
  955. unsigned char b[4098], *buf, *s1, *s2;
  956. int j, k, len, flen;
  957. SOCKET sock;
  958. unsigned ttl;
  959. #ifndef NOIPV6
  960. struct sockaddr_in6 addr;
  961. struct sockaddr_in6 *sinsr, *sinsl;
  962. #else
  963. struct sockaddr_in addr;
  964. struct sockaddr_in *sinsr, *sinsl;
  965. #endif
  966. int usetcp = 0;
  967. unsigned short serial = 1;
  968. buf = b+2;
  969. sinsl = (param && !makeauth)? &param->sinsl : &addr;
  970. sinsr = (param && !makeauth)? &param->sinsr : &addr;
  971. memset(sinsl, 0, sizeof(addr));
  972. memset(sinsr, 0, sizeof(addr));
  973. if(makeauth && !SAISNULL(&authnserver.addr)){
  974. usetcp = authnserver.usetcp;
  975. *SAFAMILY(sinsl) = *SAFAMILY(&authnserver.addr);
  976. }
  977. else {
  978. usetcp = nservers[i].usetcp;
  979. *SAFAMILY(sinsl) = *SAFAMILY(&nservers[i].addr);
  980. }
  981. if((sock=so._socket(SASOCK(sinsl), usetcp?SOCK_STREAM:SOCK_DGRAM, usetcp?IPPROTO_TCP:IPPROTO_UDP)) == INVALID_SOCKET) break;
  982. if(so._bind(sock,(struct sockaddr *)sinsl,SASIZE(sinsl))){
  983. so._shutdown(sock, SHUT_RDWR);
  984. so._closesocket(sock);
  985. break;
  986. }
  987. if(makeauth && !SAISNULL(&authnserver.addr)){
  988. *sinsr = authnserver.addr;
  989. }
  990. else {
  991. *sinsr = nservers[i].addr;
  992. }
  993. if(usetcp){
  994. if(so._connect(sock,(struct sockaddr *)sinsr,SASIZE(sinsr))) {
  995. so._shutdown(sock, SHUT_RDWR);
  996. so._closesocket(sock);
  997. break;
  998. }
  999. }
  1000. len = (int)strlen((char *)name);
  1001. serial = myrand(name,len);
  1002. *(unsigned short*)buf = serial; /* query id */
  1003. buf[2] = 1; /* recursive */
  1004. buf[3] = 0;
  1005. buf[4] = 0;
  1006. buf[5] = 1; /* 1 request */
  1007. buf[6] = buf[7] = 0; /* no replies */
  1008. buf[8] = buf[9] = 0; /* no ns count */
  1009. buf[10] = buf[11] = 0; /* no additional */
  1010. if(len > 255) {
  1011. len = 255;
  1012. }
  1013. memcpy(buf + 13, name, len);
  1014. len += 13;
  1015. buf[len] = 0;
  1016. for(s2 = buf + 12; (s1 = (unsigned char *)strchr((char *)s2 + 1, '.')); s2 = s1)*s2 = (unsigned char)((s1 - s2) - 1);
  1017. *s2 = (len - (int)(s2 - buf)) - 1;
  1018. len++;
  1019. buf[len++] = 0;
  1020. buf[len++] = (makeauth == 1)? 0x0c : (af==AF_INET6? 0x1c:0x01); /* PTR:host address */
  1021. buf[len++] = 0;
  1022. buf[len++] = 1; /* INET */
  1023. if(usetcp){
  1024. buf-=2;
  1025. *(unsigned short*)buf = htons(len);
  1026. len+=2;
  1027. }
  1028. if(socksendto(sock, (struct sockaddr *)sinsr, buf, len, conf.timeouts[SINGLEBYTE_L]*1000) != len){
  1029. so._shutdown(sock, SHUT_RDWR);
  1030. so._closesocket(sock);
  1031. continue;
  1032. }
  1033. if(param) param->statscli64 += len;
  1034. len = sockrecvfrom(sock, (struct sockaddr *)sinsr, buf, 4096, conf.timeouts[DNS_TO]*1000);
  1035. so._shutdown(sock, SHUT_RDWR);
  1036. so._closesocket(sock);
  1037. if(len <= 13) {
  1038. continue;
  1039. }
  1040. if(param) param->statssrv64 += len;
  1041. if(usetcp){
  1042. unsigned short us;
  1043. us = ntohs(*(unsigned short*)buf);
  1044. len-=2;
  1045. buf+=2;
  1046. if(us > 4096 || us < len || (us > len && sockrecvfrom(sock, (struct sockaddr *)sinsr, buf+len, us-len, conf.timeouts[DNS_TO]*1000) != us-len)) {
  1047. continue;
  1048. }
  1049. }
  1050. if(*(unsigned short *)buf != serial)continue;
  1051. if((na = buf[7] + (((unsigned short)buf[6])<<8)) < 1) {
  1052. return 0;
  1053. }
  1054. nq = buf[5] + (((unsigned short)buf[4])<<8);
  1055. if (nq != 1) {
  1056. continue; /* we did only 1 request */
  1057. }
  1058. for(k = 13; k<len && buf[k]; k++) {
  1059. }
  1060. k++;
  1061. if( (k+4) >= len) {
  1062. continue;
  1063. }
  1064. k += 4;
  1065. if(na > 255) na = 255; /* somebody is very evil */
  1066. for (j = 0; j < na; j++) { /* now there should be answers */
  1067. while(buf[k] < 192 && buf[k] !=0 && (k+buf[k]+14) < len) k+= (buf[k] + 1);
  1068. if(!buf[k]) k--;
  1069. if((k+(af == AF_INET6?28:16)) > len) {
  1070. break;
  1071. }
  1072. flen = buf[k+11] + (((unsigned short)buf[k+10])<<8);
  1073. if((k+12+flen) > len) {
  1074. break;
  1075. }
  1076. if(makeauth != 1){
  1077. if(buf[k+2] != 0 || buf[k+3] != (af == AF_INET6?0x1c:0x1) || flen != (af == AF_INET6?16:4)) {
  1078. k+= (12 + flen);
  1079. continue; /* we need A IPv4 */
  1080. }
  1081. ttl = ntohl(*(unsigned long *)(buf + k + 6));
  1082. memcpy(value, buf + k + 12, af == AF_INET6? 16:4);
  1083. if(ttl < 60 || ttl > (3600*12)) ttl = 300;
  1084. hashadd(af == AF_INET6?&dns6_table:&dns_table, name, value, conf.time+ttl);
  1085. if(retttl) *retttl = ttl;
  1086. return 1;
  1087. }
  1088. else {
  1089. if(buf[k+2] != 0 || buf[k+3] != 0x0c) {
  1090. k+= (12 + flen);
  1091. continue; /* we need A PTR */
  1092. }
  1093. for (s2 = buf + k + 12; s2 < (buf + k + 12 + len) && *s2; ){
  1094. s1 = s2 + ((unsigned)*s2) + 1;
  1095. *s2 = '.';
  1096. s2 = s1;
  1097. }
  1098. *s2 = 0;
  1099. if(param->username)myfree(param->username);
  1100. param->username = (unsigned char *)mystrdup ((char *)buf + k + 13);
  1101. return udpresolve(af,param->username, value, NULL, NULL, 2);
  1102. }
  1103. }
  1104. }
  1105. return 0;
  1106. }
  1107. unsigned long myresolver(int af, unsigned char * name, unsigned char * value){
  1108. return udpresolve(af, name, value, NULL, NULL, 0);
  1109. }
  1110. unsigned long fakeresolver (int af, unsigned char *name, unsigned char * value){
  1111. memset(value, 0, af == AF_INET6? 16 : 4);
  1112. if(af == AF_INET6){
  1113. memset(value, 0, 16);
  1114. value[15] = 2;
  1115. }
  1116. else {
  1117. value[0] = 127;
  1118. value[1] = 0;
  1119. value[2] = 0;
  1120. value[3] = 2;
  1121. }
  1122. return 1;
  1123. }
  1124. #ifndef NOODBC
  1125. SQLHENV henv = NULL;
  1126. SQLHSTMT hstmt = NULL;
  1127. SQLHDBC hdbc = NULL;
  1128. char * sqlstring = NULL;
  1129. void close_sql(){
  1130. if(hstmt) {
  1131. SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
  1132. hstmt = NULL;
  1133. }
  1134. if(hdbc){
  1135. SQLDisconnect(hdbc);
  1136. SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
  1137. hdbc = NULL;
  1138. }
  1139. if(henv) {
  1140. SQLFreeHandle(SQL_HANDLE_ENV, henv);
  1141. henv = NULL;
  1142. }
  1143. }
  1144. int attempt = 0;
  1145. time_t attempt_time = 0;
  1146. int init_sql(char * s){
  1147. SQLRETURN retcode;
  1148. char * datasource;
  1149. char * username;
  1150. char * password;
  1151. char * string;
  1152. if(!s) return 0;
  1153. if(!sqlstring || strcmp(sqlstring, s)){
  1154. string = sqlstring;
  1155. sqlstring=mystrdup(s);
  1156. if(string)myfree(string);
  1157. }
  1158. if(hstmt || hdbc || henv) close_sql();
  1159. attempt++;
  1160. attempt_time = time(0);
  1161. if(!henv){
  1162. retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  1163. if (!henv || (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)){
  1164. henv = NULL;
  1165. return 0;
  1166. }
  1167. retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
  1168. if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
  1169. return 0;
  1170. }
  1171. }
  1172. if(!hdbc){
  1173. retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
  1174. if (!hdbc || (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)) {
  1175. hdbc = NULL;
  1176. SQLFreeHandle(SQL_HANDLE_ENV, henv);
  1177. henv = NULL;
  1178. return 0;
  1179. }
  1180. SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (void*)15, 0);
  1181. }
  1182. string = mystrdup(sqlstring);
  1183. if(!string) return 0;
  1184. datasource = strtok(string, ",");
  1185. username = strtok(NULL, ",");
  1186. password = strtok(NULL, ",");
  1187. /* Connect to data source */
  1188. retcode = SQLConnect(hdbc, (SQLCHAR*) datasource, (SQLSMALLINT)strlen(datasource),
  1189. (SQLCHAR*) username, (SQLSMALLINT)((username)?strlen(username):0),
  1190. (SQLCHAR*) password, (SQLSMALLINT)((password)?strlen(password):0));
  1191. myfree(string);
  1192. if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO){
  1193. SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
  1194. hdbc = NULL;
  1195. SQLFreeHandle(SQL_HANDLE_ENV, henv);
  1196. henv = NULL;
  1197. return 0;
  1198. }
  1199. retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
  1200. if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO){
  1201. close_sql();
  1202. return 0;
  1203. }
  1204. return 1;
  1205. }
  1206. void sqlerr (char *buf){
  1207. if(conf.stdlog){
  1208. fprintf(conf.stdlog, "%s\n", buf);
  1209. fflush(conf.stdlog);
  1210. }
  1211. pthread_mutex_unlock(&log_mutex);
  1212. }
  1213. void logsql(struct clientparam * param, const unsigned char *s) {
  1214. SQLRETURN ret;
  1215. int len;
  1216. if(param->nolog) return;
  1217. pthread_mutex_lock(&log_mutex);
  1218. len = dobuf(param, tmpbuf, s, (unsigned char *)"\'");
  1219. if(attempt > 5){
  1220. time_t t;
  1221. t = time(0);
  1222. if (t - attempt_time < 180){
  1223. sqlerr((char *)tmpbuf);
  1224. return;
  1225. }
  1226. }
  1227. if(!hstmt){
  1228. if(!init_sql(sqlstring)) {
  1229. sqlerr((char *)tmpbuf);
  1230. return;
  1231. }
  1232. }
  1233. if(hstmt){
  1234. ret = SQLExecDirect(hstmt, (SQLCHAR *)tmpbuf, (SQLINTEGER)len);
  1235. if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO){
  1236. close_sql();
  1237. if(!init_sql(sqlstring)){
  1238. sqlerr((char *)tmpbuf);
  1239. return;
  1240. }
  1241. if(hstmt) {
  1242. ret = SQLExecDirect(hstmt, (SQLCHAR *)tmpbuf, (SQLINTEGER)len);
  1243. if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO){
  1244. sqlerr((char *)tmpbuf);
  1245. return;
  1246. }
  1247. attempt = 0;
  1248. }
  1249. }
  1250. attempt = 0;
  1251. }
  1252. pthread_mutex_unlock(&log_mutex);
  1253. }
  1254. #endif