auth.c 35 KB

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