auth.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2016 by Vladimir Dubrovin <3proxy@3proxy.ru>
  4. please read License Agreement
  5. */
  6. #include "proxy.h"
  7. int clientnegotiate(struct chain * redir, struct clientparam * param, struct sockaddr * addr, unsigned char * hostname){
  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 && hostname) {
  37. char * needreplace;
  38. needreplace = strchr((char *)hostname, ':');
  39. if(needreplace) buf[len++] = '[';
  40. len += sprintf((char *)buf + len, "%.256s", (char *)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\nConnection: 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 && 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 && hostname) {
  87. int hostnamelen;
  88. hostnamelen = (int)strlen((char *)hostname) + 1;
  89. if(hostnamelen > 255) hostnamelen = 255;
  90. memcpy(buf+len, 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 && hostname) {
  152. buf[3] = 3;
  153. len = (int)strlen((char *)hostname);
  154. if(len > 255) len = 255;
  155. buf[4] = len;
  156. memcpy(buf + 5, 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(SAISNULL(&param->sinsl))param->sinsl = param->sincr;
  238. if(cur->next)continue;
  239. return 0;
  240. }
  241. else if(SAISNULL(&cur->addr) && !*SAPORT(&cur->addr)){
  242. if(cur->extuser){
  243. if(param->extusername)
  244. myfree(param->extusername);
  245. param->extusername = (unsigned char *)mystrdup((char *)((*cur->extuser == '*' && param->username)? param->username : cur->extuser));
  246. if(cur->extpass){
  247. if(param->extpassword)
  248. myfree(param->extpassword);
  249. param->extpassword = (unsigned char *)mystrdup((char *)((*cur->extuser == '*' && param->password)?param->password : cur->extpass));
  250. }
  251. if(*cur->extuser == '*' && !param->username) return 4;
  252. }
  253. switch(cur->type){
  254. case R_POP3:
  255. param->redirectfunc = pop3pchild;
  256. break;
  257. case R_FTP:
  258. param->redirectfunc = ftpprchild;
  259. break;
  260. case R_ADMIN:
  261. param->redirectfunc = adminchild;
  262. break;
  263. case R_SMTP:
  264. param->redirectfunc = smtppchild;
  265. break;
  266. default:
  267. param->redirectfunc = proxychild;
  268. }
  269. if(cur->next)continue;
  270. return 0;
  271. }
  272. else if(!*SAPORT(&cur->addr) && !SAISNULL(&cur->addr)) {
  273. unsigned short port = *SAPORT(&param->sinsr);
  274. param->sinsr = cur->addr;
  275. *SAPORT(&param->sinsr) = port;
  276. }
  277. else if(SAISNULL(&cur->addr) && *SAPORT(&cur->addr)) *SAPORT(&param->sinsr) = *SAPORT(&cur->addr);
  278. else {
  279. param->sinsr = cur->addr;
  280. }
  281. if((res = alwaysauth(param))){
  282. return (res == 10)? res : 60+res;
  283. }
  284. }
  285. else {
  286. res = (redir)?clientnegotiate(redir, param, (struct sockaddr *)&cur->addr, cur->exthost):0;
  287. if(res) return res;
  288. }
  289. redir = cur;
  290. param->redirtype = redir->type;
  291. if(redir->type == R_TCP || redir->type ==R_HTTP) {
  292. if(cur->extuser){
  293. if(*cur -> extuser == '*' && !param->username) return 4;
  294. if(param->extusername)
  295. myfree(param->extusername);
  296. param->extusername = (unsigned char *)mystrdup((char *)((*cur->extuser == '*' && param->username)? param->username : cur->extuser));
  297. if(cur->extpass){
  298. if(param->extpassword)
  299. myfree(param->extpassword);
  300. param->extpassword = (unsigned char *)mystrdup((char *)((*cur->extuser == '*' && param->password)?param->password : cur->extpass));
  301. }
  302. }
  303. return 0;
  304. }
  305. connected = 1;
  306. }
  307. if(!connected || !redir) return 0;
  308. return clientnegotiate(redir, param, (struct sockaddr *)&param->req, param->hostname);
  309. }
  310. int IPInentry(struct sockaddr *sa, struct iplist *ipentry){
  311. int addrlen;
  312. unsigned char *ip, *ipf, *ipt;
  313. if(!sa || ! ipentry || *SAFAMILY(sa) != ipentry->family) return 0;
  314. ip = (unsigned char *)SAADDR(sa);
  315. ipf = (unsigned char *)&ipentry->ip_from;
  316. ipt = (unsigned char *)&ipentry->ip_to;
  317. addrlen = SAADDRLEN(sa);
  318. if(memcmp(ip,ipf,addrlen) < 0 || memcmp(ip,ipt,addrlen) > 0) return 0;
  319. return 1;
  320. }
  321. int ACLmatches(struct ace* acentry, struct clientparam * param){
  322. struct userlist * userentry;
  323. struct iplist *ipentry;
  324. struct portlist *portentry;
  325. struct period *periodentry;
  326. unsigned char * username;
  327. struct hostname * hstentry=NULL;
  328. int i;
  329. int match = 0;
  330. username = param->username?param->username:(unsigned char *)"-";
  331. if(acentry->src) {
  332. for(ipentry = acentry->src; ipentry; ipentry = ipentry->next)
  333. if(IPInentry((struct sockaddr *)&param->sincr, ipentry)) {
  334. break;
  335. }
  336. if(!ipentry) return 0;
  337. }
  338. if((acentry->dst && !SAISNULL(&param->req)) || (acentry->dstnames && param->hostname)) {
  339. for(ipentry = acentry->dst; ipentry; ipentry = ipentry->next)
  340. if(IPInentry((struct sockaddr *)&param->req, ipentry)) {
  341. break;
  342. }
  343. if(!ipentry) {
  344. if(acentry->dstnames && param->hostname){
  345. for(i=0; param->hostname[i]; i++){
  346. param->hostname[i] = tolower(param->hostname[i]);
  347. }
  348. while(i > 5 && param->hostname[i-1] == '.') param->hostname[i-1] = 0;
  349. for(hstentry = acentry->dstnames; hstentry; hstentry = hstentry->next){
  350. int lname, lhost;
  351. switch(hstentry->matchtype){
  352. case 0:
  353. #ifndef _WIN32
  354. if(strcasestr((char *)param->hostname, (char *)hstentry->name)) match = 1;
  355. #else
  356. if(strstr((char *)param->hostname, (char *)hstentry->name)) match = 1;
  357. #endif
  358. break;
  359. case 1:
  360. if(!strncasecmp((char *)param->hostname, (char *)hstentry->name, strlen((char *)hstentry->name)))
  361. match = 1;
  362. break;
  363. case 2:
  364. lname = strlen((char *)hstentry->name);
  365. lhost = strlen((char *)param->hostname);
  366. if(lhost > lname){
  367. if(!strncasecmp((char *)param->hostname + (lhost - lname),
  368. (char *)hstentry->name,
  369. lname))
  370. match = 1;
  371. }
  372. break;
  373. default:
  374. if(!strcasecmp((char *)param->hostname, (char *)hstentry->name)) match = 1;
  375. break;
  376. }
  377. if(match) break;
  378. }
  379. }
  380. }
  381. if(!ipentry && !hstentry) return 0;
  382. }
  383. if(acentry->ports && *SAPORT(&param->req)) {
  384. for (portentry = acentry->ports; portentry; portentry = portentry->next)
  385. if(ntohs(*SAPORT(&param->req)) >= portentry->startport &&
  386. ntohs(*SAPORT(&param->req)) <= portentry->endport) {
  387. break;
  388. }
  389. if(!portentry) return 0;
  390. }
  391. if(acentry->wdays){
  392. if(!(acentry -> wdays & wday)) return 0;
  393. }
  394. if(acentry->periods){
  395. int start_time = (int)(param->time_start - basetime);
  396. for(periodentry = acentry->periods; periodentry; periodentry = periodentry -> next)
  397. if(start_time >= periodentry->fromtime && start_time < periodentry->totime){
  398. break;
  399. }
  400. if(!periodentry) return 0;
  401. }
  402. if(acentry->users){
  403. for(userentry = acentry->users; userentry; userentry = userentry->next)
  404. if(!strcmp((char *)username, (char *)userentry->user)){
  405. break;
  406. }
  407. if(!userentry) return 0;
  408. }
  409. if(acentry->operation) {
  410. if((acentry->operation & param->operation) != param->operation){
  411. return 0;
  412. }
  413. }
  414. if(acentry->weight && (acentry->weight < param->weight)) return 0;
  415. return 1;
  416. }
  417. int startconnlims (struct clientparam *param){
  418. struct connlim * ce;
  419. time_t delta;
  420. uint64_t rating;
  421. int ret = 0;
  422. pthread_mutex_lock(&connlim_mutex);
  423. for(ce = conf.connlimiter; ce; ce = ce->next) {
  424. if(ACLmatches(ce->ace, param)){
  425. if(ce->ace->action == NOCONNLIM)break;
  426. if(!ce->period){
  427. if(ce->rate <= ce->rating) {
  428. ret = 1;
  429. break;
  430. }
  431. ce->rating++;
  432. continue;
  433. }
  434. delta = conf.time - ce->basetime;
  435. if(ce->period <= delta || ce->basetime > conf.time){
  436. ce->basetime = conf.time;
  437. ce->rating = 0x100000;
  438. continue;
  439. }
  440. rating = delta? ((ce->rating * (ce->period - delta)) / ce->period) + 0x100000 : ce->rating + 0x100000;
  441. if (rating > (ce->rate<<20)) {
  442. ret = 2;
  443. break;
  444. }
  445. ce->rating = rating;
  446. ce->basetime = conf.time;
  447. }
  448. }
  449. pthread_mutex_unlock(&connlim_mutex);
  450. return ret;
  451. }
  452. void stopconnlims (struct clientparam *param){
  453. struct connlim * ce;
  454. pthread_mutex_lock(&connlim_mutex);
  455. for(ce = conf.connlimiter; ce; ce = ce->next) {
  456. if(ACLmatches(ce->ace, param)){
  457. if(ce->ace->action == NOCONNLIM)break;
  458. if(!ce->period && ce->rating){
  459. ce->rating--;
  460. continue;
  461. }
  462. }
  463. }
  464. pthread_mutex_unlock(&connlim_mutex);
  465. }
  466. static void initbandlims (struct clientparam *param){
  467. struct bandlim * be;
  468. int i;
  469. param->bandlimfunc = NULL;
  470. param->bandlims[0] = NULL;
  471. param->bandlimsout[0] = NULL;
  472. if(!conf.bandlimfunc || (!conf.bandlimiter && !conf.bandlimiterout)) return;
  473. for(i=0, be = conf.bandlimiter; be && i<MAXBANDLIMS; be = be->next) {
  474. if(ACLmatches(be->ace, param)){
  475. if(be->ace->action == NOBANDLIM) {
  476. break;
  477. }
  478. param->bandlims[i++] = be;
  479. param->bandlimfunc = conf.bandlimfunc;
  480. }
  481. }
  482. if(i<MAXBANDLIMS)param->bandlims[i] = NULL;
  483. for(i=0, be = conf.bandlimiterout; be && i<MAXBANDLIMS; be = be->next) {
  484. if(ACLmatches(be->ace, param)){
  485. if(be->ace->action == NOBANDLIM) {
  486. break;
  487. }
  488. param->bandlimsout[i++] = be;
  489. param->bandlimfunc = conf.bandlimfunc;
  490. }
  491. }
  492. if(i<MAXBANDLIMS)param->bandlimsout[i] = NULL;
  493. param->bandlimver = conf.bandlimver;
  494. }
  495. unsigned bandlimitfunc(struct clientparam *param, unsigned nbytesin, unsigned nbytesout){
  496. unsigned sleeptime = 0, nsleeptime;
  497. time_t sec;
  498. unsigned msec;
  499. unsigned now;
  500. int i;
  501. #ifdef _WIN32
  502. struct timeb tb;
  503. ftime(&tb);
  504. sec = (unsigned)tb.time;
  505. msec = (unsigned)tb.millitm*1000;
  506. #else
  507. struct timeval tv;
  508. gettimeofday(&tv, NULL);
  509. sec = tv.tv_sec;
  510. msec = tv.tv_usec;
  511. #endif
  512. if(!nbytesin && !nbytesout) return 0;
  513. pthread_mutex_lock(&bandlim_mutex);
  514. if(param->bandlimver != conf.bandlimver){
  515. initbandlims(param);
  516. param->bandlimver = conf.bandlimver;
  517. }
  518. for(i=0; nbytesin&& i<MAXBANDLIMS && param->bandlims[i]; i++){
  519. if( !param->bandlims[i]->basetime ||
  520. param->bandlims[i]->basetime > sec ||
  521. param->bandlims[i]->basetime < (sec - 120)
  522. )
  523. {
  524. param->bandlims[i]->basetime = sec;
  525. param->bandlims[i]->nexttime = 0;
  526. continue;
  527. }
  528. now = (unsigned)((sec - param->bandlims[i]->basetime) * 1000000) + msec;
  529. nsleeptime = (param->bandlims[i]->nexttime > now)?
  530. param->bandlims[i]->nexttime - now : 0;
  531. sleeptime = (nsleeptime > sleeptime)? nsleeptime : sleeptime;
  532. param->bandlims[i]->basetime = sec;
  533. param->bandlims[i]->nexttime = msec + nsleeptime + ((nbytesin > 512)? ((nbytesin+32)/64)*(((64*8*1000000)/param->bandlims[i]->rate)) : ((nbytesin+1) * (8*1000000))/param->bandlims[i]->rate);
  534. }
  535. for(i=0; nbytesout && i<MAXBANDLIMS && param->bandlimsout[i]; i++){
  536. if( !param->bandlimsout[i]->basetime ||
  537. param->bandlimsout[i]->basetime > sec ||
  538. param->bandlimsout[i]->basetime < (sec - 120)
  539. )
  540. {
  541. param->bandlimsout[i]->basetime = sec;
  542. param->bandlimsout[i]->nexttime = 0;
  543. continue;
  544. }
  545. now = (unsigned)((sec - param->bandlimsout[i]->basetime) * 1000000) + msec;
  546. nsleeptime = (param->bandlimsout[i]->nexttime > now)?
  547. param->bandlimsout[i]->nexttime - now : 0;
  548. sleeptime = (nsleeptime > sleeptime)? nsleeptime : sleeptime;
  549. param->bandlimsout[i]->basetime = sec;
  550. param->bandlimsout[i]->nexttime = msec + nsleeptime + ((nbytesout > 512)? ((nbytesout+32)/64)*((64*8*1000000)/param->bandlimsout[i]->rate) : ((nbytesout+1)* (8*1000000))/param->bandlimsout[i]->rate);
  551. }
  552. pthread_mutex_unlock(&bandlim_mutex);
  553. return sleeptime/1000;
  554. }
  555. void trafcountfunc(struct clientparam *param){
  556. struct trafcount * tc;
  557. int countout = 0;
  558. pthread_mutex_lock(&tc_mutex);
  559. for(tc = conf.trafcounter; tc; tc = tc->next) {
  560. if(ACLmatches(tc->ace, param)){
  561. time_t t;
  562. if(tc->ace->action == NOCOUNTIN || tc->ace->action == NOCOUNTALL) {
  563. countout = 1;
  564. break;
  565. }
  566. if(tc->ace->action != COUNTIN) {
  567. countout = 1;
  568. if(tc->ace->action != COUNTALL)continue;
  569. }
  570. tc->traf64 += param->statssrv64;
  571. time(&t);
  572. tc->updated = t;
  573. }
  574. }
  575. if(countout) for(tc = conf.trafcounter; tc; tc = tc->next) {
  576. if(ACLmatches(tc->ace, param)){
  577. time_t t;
  578. if(tc->ace->action == NOCOUNTOUT || tc->ace->action == NOCOUNTALL) break;
  579. if(tc->ace->action != COUNTOUT && tc->ace->action != COUNTALL ) {
  580. continue;
  581. }
  582. tc->traf64 += param->statscli64;
  583. time(&t);
  584. tc->updated = t;
  585. }
  586. }
  587. pthread_mutex_unlock(&tc_mutex);
  588. }
  589. int alwaysauth(struct clientparam * param){
  590. int res;
  591. struct trafcount * tc;
  592. int countout = 0;
  593. if(conf.connlimiter && param->remsock == INVALID_SOCKET && startconnlims(param)) return 95;
  594. res = doconnect(param);
  595. if(!res){
  596. if(conf.bandlimfunc && (conf.bandlimiter||conf.bandlimiterout)){
  597. pthread_mutex_lock(&bandlim_mutex);
  598. initbandlims(param);
  599. pthread_mutex_unlock(&bandlim_mutex);
  600. }
  601. if(conf.trafcountfunc && conf.trafcounter) {
  602. pthread_mutex_lock(&tc_mutex);
  603. for(tc = conf.trafcounter; tc; tc = tc->next) {
  604. if(tc->disabled) continue;
  605. if(ACLmatches(tc->ace, param)){
  606. if(tc->ace->action == NOCOUNTIN) break;
  607. if(tc->ace->action != COUNTIN) {
  608. countout = 1;
  609. continue;
  610. }
  611. if(tc->traflim64 <= tc->traf64) return 10;
  612. param->trafcountfunc = conf.trafcountfunc;
  613. param->maxtrafin64 = tc->traflim64 - tc->traf64;
  614. }
  615. }
  616. if(countout)for(tc = conf.trafcounter; tc; tc = tc->next) {
  617. if(tc->disabled) continue;
  618. if(ACLmatches(tc->ace, param)){
  619. if(tc->ace->action == NOCOUNTOUT) break;
  620. if(tc->ace->action != COUNTOUT) {
  621. continue;
  622. }
  623. if(tc->traflim64 <= tc->traf64) return 10;
  624. param->trafcountfunc = conf.trafcountfunc;
  625. param->maxtrafout64 = tc->traflim64 - tc->traf64;
  626. }
  627. }
  628. pthread_mutex_unlock(&tc_mutex);
  629. }
  630. }
  631. return res;
  632. }
  633. int checkACL(struct clientparam * param){
  634. struct ace* acentry;
  635. if(!param->srv->acl) {
  636. return 0;
  637. }
  638. for(acentry = param->srv->acl; acentry; acentry = acentry->next) {
  639. if(ACLmatches(acentry, param)) {
  640. param->nolog = acentry->nolog;
  641. param->weight = acentry->weight;
  642. if(acentry->action == 2) {
  643. struct ace dup;
  644. if(param->operation < 256 && !(param->operation & CONNECT)){
  645. continue;
  646. }
  647. if(param->redirected && acentry->chains && SAISNULL(&acentry->chains->addr) && !*SAPORT(&acentry->chains->addr)) {
  648. continue;
  649. }
  650. dup = *acentry;
  651. return handleredirect(param, &dup);
  652. }
  653. return acentry->action;
  654. }
  655. }
  656. return 3;
  657. }
  658. struct authcache {
  659. char * username;
  660. char * password;
  661. time_t expires;
  662. #ifndef NOIPV6
  663. struct sockaddr_in6 sa, sinsl;
  664. #else
  665. struct sockaddr_in sa, sinsl;
  666. #endif
  667. struct ace *acl;
  668. struct authcache *next;
  669. } *authc = NULL;
  670. int cacheauth(struct clientparam * param){
  671. struct authcache *ac, *last=NULL;
  672. pthread_mutex_lock(&hash_mutex);
  673. for(ac = authc; ac; ){
  674. if(ac->expires <= conf.time){
  675. if(ac->username)myfree(ac->username);
  676. if(ac->password)myfree(ac->password);
  677. if(!last){
  678. authc = ac->next;
  679. myfree(ac);
  680. ac = authc;
  681. }
  682. else {
  683. last->next = ac->next;
  684. myfree(ac);
  685. ac = last->next;
  686. }
  687. continue;
  688. }
  689. if(
  690. (!(conf.authcachetype&2) || (param->username && ac->username && !strcmp(ac->username, (char *)param->username))) &&
  691. (!(conf.authcachetype&4) || (ac->password && param->password && !strcmp(ac->password, (char *)param->password))) &&
  692. (!(conf.authcachetype&16) || (ac->acl == param->srv->acl))
  693. ) {
  694. if(!(conf.authcachetype&1)
  695. || ((*SAFAMILY(&ac->sa) == *SAFAMILY(&param->sincr)
  696. && !memcmp(SAADDR(&ac->sa), SAADDR(&param->sincr), SAADDRLEN(&ac->sa))))){
  697. if(conf.authcachetype&32) {
  698. param->sinsl = ac->sinsl;
  699. }
  700. if(param->username){
  701. myfree(param->username);
  702. }
  703. param->username = (unsigned char *)mystrdup(ac->username);
  704. pthread_mutex_unlock(&hash_mutex);
  705. return 0;
  706. }
  707. else if ((conf.authcachetype&1) && (conf.authcachetype&8)) {
  708. pthread_mutex_unlock(&hash_mutex);
  709. return 10;
  710. }
  711. }
  712. last = ac;
  713. ac = ac->next;
  714. }
  715. pthread_mutex_unlock(&hash_mutex);
  716. return 4;
  717. }
  718. int doauth(struct clientparam * param){
  719. int res = 0;
  720. struct auth *authfuncs;
  721. struct authcache *ac;
  722. char * tmp;
  723. int ret = 0;
  724. for(authfuncs=param->srv->authfuncs; authfuncs; authfuncs=authfuncs->next){
  725. res = authfuncs->authenticate?(*authfuncs->authenticate)(param):0;
  726. if(!res) {
  727. if(authfuncs->authorize &&
  728. (res = (*authfuncs->authorize)(param)))
  729. return res;
  730. if(conf.authcachetype && authfuncs->authenticate && authfuncs->authenticate != cacheauth && param->username && (!(conf.authcachetype&4) || (!param->pwtype && param->password))){
  731. pthread_mutex_lock(&hash_mutex);
  732. for(ac = authc; ac; ac = ac->next){
  733. if(
  734. (!(conf.authcachetype&2) || !strcmp(ac->username, (char *)param->username)) &&
  735. (!(conf.authcachetype&1) || (*SAFAMILY(&ac->sa) == *SAFAMILY(&param->sincr) && !memcmp(SAADDR(&ac->sa), SAADDR(&param->sincr), SAADDRLEN(&ac->sa)))) &&
  736. (!(conf.authcachetype&4) || (ac->password && !strcmp(ac->password, (char *)param->password))) &&
  737. (!(conf.authcachetype&16) || (ac->acl == param->srv->acl))
  738. ) {
  739. ac->expires = conf.time + conf.authcachetime;
  740. if(strcmp(ac->username, (char *)param->username)){
  741. tmp = ac->username;
  742. ac->username = mystrdup((char *)param->username);
  743. myfree(tmp);
  744. }
  745. if((conf.authcachetype&4)){
  746. tmp = ac->password;
  747. ac->password = mystrdup((char *)param->password);
  748. myfree(tmp);
  749. }
  750. ac->sa = param->sincr;
  751. if(conf.authcachetype&32) {
  752. ac->sinsl = param-> sinsl;
  753. *SAPORT(&ac->sinsl) = 0;
  754. }
  755. break;
  756. }
  757. }
  758. if(!ac){
  759. ac = myalloc(sizeof(struct authcache));
  760. if(ac){
  761. ac->expires = conf.time + conf.authcachetime;
  762. ac->username = param->username?mystrdup((char *)param->username):NULL;
  763. ac->sa = param->sincr;
  764. ac->password = NULL;
  765. if((conf.authcachetype&4) && param->password) ac->password = mystrdup((char *)param->password);
  766. if(conf.authcachetype&32) {
  767. ac->sinsl = param->sinsl;
  768. *SAPORT(&ac->sinsl) = 0;
  769. }
  770. }
  771. ac->next = authc;
  772. authc = ac;
  773. }
  774. pthread_mutex_unlock(&hash_mutex);
  775. }
  776. break;
  777. }
  778. if(res > ret) ret = res;
  779. if(ret > 9) return ret;
  780. }
  781. if(!res){
  782. return alwaysauth(param);
  783. }
  784. return ret;
  785. }
  786. int ipauth(struct clientparam * param){
  787. int res;
  788. unsigned char *username;
  789. username = param->username;
  790. param->username = NULL;
  791. res = checkACL(param);
  792. param->username = username;
  793. return res;
  794. }
  795. int userauth(struct clientparam * param){
  796. return (param->username)? 0:4;
  797. }
  798. int dnsauth(struct clientparam * param){
  799. char buf[128];
  800. char addr[16];
  801. char dig[]="0123456789abcdef";
  802. unsigned u;
  803. int i;
  804. if(*SAFAMILY(&param->sincr)!=AF_INET){
  805. char *s = buf;
  806. for(i=15; i>=0; i--){
  807. unsigned char c=((unsigned char *)SAADDR(&param->sincr))[i];
  808. *s++ = dig[(c&0xf)];
  809. *s++ = '.';
  810. *s++ = dig[(c>>4)];
  811. *s++ = '.';
  812. }
  813. sprintf(s, "ip6.arpa");
  814. }
  815. else {
  816. u = ntohl(*(unsigned long *)SAADDR(&param->sincr));
  817. sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
  818. ((u&0x000000FF)),
  819. ((u&0x0000FF00)>>8),
  820. ((u&0x00FF0000)>>16),
  821. ((u&0xFF000000)>>24));
  822. }
  823. if(!udpresolve(*SAFAMILY(&param->sincr), (unsigned char *)buf, (unsigned char *)addr, NULL, param, 1)) {
  824. return 3;
  825. }
  826. if(memcmp(SAADDR(&param->sincr), addr, SAADDRLEN(&param->sincr))) {
  827. return 3;
  828. }
  829. return param->username? 0:3;
  830. }
  831. int strongauth(struct clientparam * param){
  832. struct passwords * pwl;
  833. unsigned char buf[256];
  834. if(!param->username) return 4;
  835. pthread_mutex_lock(&pwl_mutex);
  836. for(pwl = conf.pwl; pwl; pwl=pwl->next){
  837. if(!strcmp((char *)pwl->user, (char *)param->username)) switch(pwl->pwtype) {
  838. case CL:
  839. if(!pwl->password || !*pwl->password){
  840. break;
  841. }
  842. else if (!param->pwtype && param->password && !strcmp((char *)param->password, (char *)pwl->password)){
  843. break;
  844. }
  845. #ifndef NOCRYPT
  846. else if (param->pwtype == 2 && param->password) {
  847. ntpwdhash(buf, pwl->password, 0);
  848. mschap(buf, param->password, buf + 16);
  849. if(!memcmp(buf+16, param->password+8, 24)) {
  850. break;
  851. }
  852. }
  853. #endif
  854. pthread_mutex_unlock(&pwl_mutex);
  855. return 6;
  856. #ifndef NOCRYPT
  857. case CR:
  858. if(param->password && !param->pwtype && !strcmp((char *)pwl->password, (char *)mycrypt(param->password, pwl->password,buf))) {
  859. break;
  860. }
  861. pthread_mutex_unlock(&pwl_mutex);
  862. return 7;
  863. case NT:
  864. if(param->password && !param->pwtype && !memcmp(pwl->password, ntpwdhash(buf,param->password, 1), 32)) {
  865. break;
  866. }
  867. else if (param->pwtype == 2){
  868. fromhex(pwl->password, buf, 16);
  869. mschap(buf, param->password, buf + 16);
  870. if(!memcmp(buf + 16, param->password+8, 24)) {
  871. break;
  872. }
  873. }
  874. pthread_mutex_unlock(&pwl_mutex);
  875. return 8;
  876. #endif
  877. default:
  878. pthread_mutex_unlock(&pwl_mutex);
  879. return 999;
  880. }
  881. else continue;
  882. pthread_mutex_unlock(&pwl_mutex);
  883. return 0;
  884. }
  885. pthread_mutex_unlock(&pwl_mutex);
  886. return 5;
  887. }
  888. int radauth(struct clientparam * param);
  889. struct auth authfuncs[] = {
  890. {authfuncs+1, NULL, NULL, ""},
  891. {authfuncs+2, ipauth, NULL, "iponly"},
  892. {authfuncs+3, userauth, checkACL, "useronly"},
  893. {authfuncs+4, dnsauth, checkACL, "dnsname"},
  894. {authfuncs+5, strongauth, checkACL, "strong"},
  895. {authfuncs+6, cacheauth, checkACL, "cache"},
  896. #ifndef NORADIUS
  897. #define AUTHOFFSET 1
  898. {authfuncs+7, radauth, checkACL, "radius"},
  899. #else
  900. #define AUTHOFFSET 0
  901. #endif
  902. {authfuncs+7+AUTHOFFSET, NULL, NULL, "none"},
  903. {NULL, NULL, NULL, ""}
  904. };
  905. struct hashtable dns_table = {0, 4, {0,0,0,0}, NULL, NULL, NULL};
  906. struct hashtable dns6_table = {0, 16, {0,0,0,0}, NULL, NULL, NULL};
  907. void nametohash(const unsigned char * name, unsigned char *hash, unsigned char *rnd){
  908. unsigned i, j, k;
  909. memcpy(hash, rnd, sizeof(unsigned)*4);
  910. for(i=0, j=0, k=0; name[j]; j++){
  911. hash[i] += (toupper(name[j]) - 32)+rnd[((toupper(name[j]))*29277+rnd[(k+j+i)%16]+k+j+i)%16];
  912. if(++i == sizeof(unsigned)*4) {
  913. i = 0;
  914. k++;
  915. }
  916. }
  917. }
  918. unsigned hashindex(struct hashtable *ht, const unsigned char* hash){
  919. unsigned t1, t2, t3, t4;
  920. t1 = *(unsigned *)hash;
  921. t2 = *(unsigned *)(hash + sizeof(unsigned));
  922. t3 = *(unsigned *)(hash + (2*sizeof(unsigned)));
  923. t4 = *(unsigned *)(hash + (3*sizeof(unsigned)));
  924. return (t1 + (t2 * 7) + (t3 * 17) + (t4 * 29) ) % (ht->hashsize >> 2);
  925. }
  926. void destroyhashtable(struct hashtable *ht){
  927. pthread_mutex_lock(&hash_mutex);
  928. if(ht->hashtable){
  929. myfree(ht->hashtable);
  930. ht->hashtable = NULL;
  931. }
  932. if(ht->hashvalues){
  933. myfree(ht->hashvalues);
  934. ht->hashvalues = NULL;
  935. }
  936. ht->hashsize = 0;
  937. pthread_mutex_unlock(&hash_mutex);
  938. }
  939. #define hvalue(I) ((struct hashentry *)((char *)ht->hashvalues + (I)*(sizeof(struct hashentry) + ht->recsize - 4)))
  940. int inithashtable(struct hashtable *ht, unsigned nhashsize){
  941. unsigned i;
  942. clock_t c;
  943. #ifdef _WIN32
  944. struct timeb tb;
  945. ftime(&tb);
  946. #else
  947. struct timeval tb;
  948. struct timezone tz;
  949. gettimeofday(&tb, &tz);
  950. #endif
  951. c = clock();
  952. if(nhashsize<4) return 1;
  953. pthread_mutex_lock(&hash_mutex);
  954. if(ht->hashtable){
  955. myfree(ht->hashtable);
  956. ht->hashtable = NULL;
  957. }
  958. if(ht->hashvalues){
  959. myfree(ht->hashvalues);
  960. ht->hashvalues = NULL;
  961. }
  962. ht->hashsize = 0;
  963. if(!(ht->hashtable = myalloc((nhashsize>>2) * sizeof(struct hashentry *)))){
  964. pthread_mutex_unlock(&hash_mutex);
  965. return 2;
  966. }
  967. if(!(ht->hashvalues = myalloc(nhashsize * (sizeof(struct hashentry) + (ht->recsize-4))))){
  968. myfree(ht->hashtable);
  969. ht->hashtable = NULL;
  970. pthread_mutex_unlock(&hash_mutex);
  971. return 3;
  972. }
  973. ht->hashsize = nhashsize;
  974. ht->rnd[0] = myrand(&tb, sizeof(tb));
  975. ht->rnd[1] = myrand(ht->hashtable, sizeof(ht->hashtable));
  976. ht->rnd[2] = myrand(&c, sizeof(c));
  977. ht->rnd[3] = myrand(ht->hashvalues,sizeof(ht->hashvalues));
  978. memset(ht->hashtable, 0, (ht->hashsize>>2) * sizeof(struct hashentry *));
  979. memset(ht->hashvalues, 0, ht->hashsize * (sizeof(struct hashentry) + ht->recsize -4));
  980. for(i = 0; i< (ht->hashsize - 1); i++) {
  981. hvalue(i)->next = hvalue(i+1);
  982. }
  983. ht->hashempty = ht->hashvalues;
  984. pthread_mutex_unlock(&hash_mutex);
  985. return 0;
  986. }
  987. void hashadd(struct hashtable *ht, const unsigned char* name, unsigned char* value, time_t expires){
  988. struct hashentry * hen, *he;
  989. struct hashentry ** hep;
  990. unsigned index;
  991. pthread_mutex_lock(&hash_mutex);
  992. if(!ht||!value||!name||!ht->hashtable||!ht->hashempty) {
  993. pthread_mutex_unlock(&hash_mutex);
  994. return;
  995. }
  996. hen = ht->hashempty;
  997. ht->hashempty = ht->hashempty->next;
  998. nametohash(name, hen->hash, (unsigned char *)ht->rnd);
  999. memcpy(hen->value, value, ht->recsize);
  1000. hen->expires = expires;
  1001. hen->next = NULL;
  1002. index = hashindex(ht, hen->hash);
  1003. for(hep = ht->hashtable + index; (he = *hep)!=NULL; ){
  1004. if(he->expires < conf.time || !memcmp(hen->hash, he->hash, sizeof(he->hash))) {
  1005. (*hep) = he->next;
  1006. he->expires = 0;
  1007. he->next = ht->hashempty;
  1008. ht->hashempty = he;
  1009. }
  1010. else hep=&(he->next);
  1011. }
  1012. hen->next = ht->hashtable[index];
  1013. ht->hashtable[index] = hen;
  1014. pthread_mutex_unlock(&hash_mutex);
  1015. }
  1016. unsigned long hashresolv(struct hashtable *ht, const unsigned char* name, unsigned char* value, unsigned *ttl){
  1017. unsigned char hash[sizeof(unsigned)*4];
  1018. struct hashentry ** hep;
  1019. struct hashentry *he;
  1020. unsigned index;
  1021. pthread_mutex_lock(&hash_mutex);
  1022. if(!ht || !ht->hashtable || !name) {
  1023. pthread_mutex_unlock(&hash_mutex);
  1024. return 0;
  1025. }
  1026. nametohash(name, hash, (unsigned char *)ht->rnd);
  1027. index = hashindex(ht, hash);
  1028. for(hep = ht->hashtable + index; (he = *hep)!=NULL; ){
  1029. if(he->expires < conf.time) {
  1030. (*hep) = he->next;
  1031. he->expires = 0;
  1032. he->next = ht->hashempty;
  1033. ht->hashempty = he;
  1034. }
  1035. else if(!memcmp(hash, he->hash, sizeof(unsigned)*4)){
  1036. if(ttl) *ttl = (unsigned)(he->expires - conf.time);
  1037. memcpy(value, he->value, ht->recsize);
  1038. pthread_mutex_unlock(&hash_mutex);
  1039. return 1;
  1040. }
  1041. else hep=&(he->next);
  1042. }
  1043. pthread_mutex_unlock(&hash_mutex);
  1044. return 0;
  1045. }
  1046. struct nserver nservers[MAXNSERVERS] = {{{0},0}, {{0},0}, {{0},0}, {{0},0}, {{0},0}};
  1047. struct nserver authnserver;
  1048. unsigned long udpresolve(int af, unsigned char * name, unsigned char * value, unsigned *retttl, struct clientparam* param, int makeauth){
  1049. int i,n;
  1050. unsigned long retval;
  1051. if((af == AF_INET) && (retval = hashresolv(&dns_table, name, value, retttl))) {
  1052. return retval;
  1053. }
  1054. if((af == AF_INET6) && (retval = hashresolv(&dns6_table, name, value, retttl))) {
  1055. return retval;
  1056. }
  1057. n = (makeauth && !SAISNULL(&authnserver.addr))? 1 : numservers;
  1058. for(i=0; i<n; i++){
  1059. unsigned short nq, na;
  1060. unsigned char b[4098], *buf, *s1, *s2;
  1061. int j, k, len, flen;
  1062. SOCKET sock;
  1063. unsigned ttl;
  1064. #ifndef NOIPV6
  1065. struct sockaddr_in6 addr;
  1066. struct sockaddr_in6 *sinsr, *sinsl;
  1067. #else
  1068. struct sockaddr_in addr;
  1069. struct sockaddr_in *sinsr, *sinsl;
  1070. #endif
  1071. int usetcp = 0;
  1072. unsigned short serial = 1;
  1073. buf = b+2;
  1074. sinsl = (param && !makeauth)? &param->sinsl : &addr;
  1075. sinsr = (param && !makeauth)? &param->sinsr : &addr;
  1076. memset(sinsl, 0, sizeof(addr));
  1077. memset(sinsr, 0, sizeof(addr));
  1078. if(makeauth && !SAISNULL(&authnserver.addr)){
  1079. usetcp = authnserver.usetcp;
  1080. *SAFAMILY(sinsl) = *SAFAMILY(&authnserver.addr);
  1081. }
  1082. else {
  1083. usetcp = nservers[i].usetcp;
  1084. *SAFAMILY(sinsl) = *SAFAMILY(&nservers[i].addr);
  1085. }
  1086. if((sock=so._socket(SASOCK(sinsl), usetcp?SOCK_STREAM:SOCK_DGRAM, usetcp?IPPROTO_TCP:IPPROTO_UDP)) == INVALID_SOCKET) break;
  1087. if(so._bind(sock,(struct sockaddr *)sinsl,SASIZE(sinsl))){
  1088. so._shutdown(sock, SHUT_RDWR);
  1089. so._closesocket(sock);
  1090. break;
  1091. }
  1092. if(makeauth && !SAISNULL(&authnserver.addr)){
  1093. *sinsr = authnserver.addr;
  1094. }
  1095. else {
  1096. *sinsr = nservers[i].addr;
  1097. }
  1098. if(usetcp){
  1099. if(connectwithpoll(sock,(struct sockaddr *)sinsr,SASIZE(sinsr),CONNECT_TO)) {
  1100. so._shutdown(sock, SHUT_RDWR);
  1101. so._closesocket(sock);
  1102. break;
  1103. }
  1104. #ifdef TCP_NODELAY
  1105. {
  1106. int opt = 1;
  1107. setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&opt, sizeof(opt));
  1108. }
  1109. #endif
  1110. }
  1111. len = (int)strlen((char *)name);
  1112. serial = myrand(name,len);
  1113. *(unsigned short*)buf = serial; /* query id */
  1114. buf[2] = 1; /* recursive */
  1115. buf[3] = 0;
  1116. buf[4] = 0;
  1117. buf[5] = 1; /* 1 request */
  1118. buf[6] = buf[7] = 0; /* no replies */
  1119. buf[8] = buf[9] = 0; /* no ns count */
  1120. buf[10] = buf[11] = 0; /* no additional */
  1121. if(len > 255) {
  1122. len = 255;
  1123. }
  1124. memcpy(buf + 13, name, len);
  1125. len += 13;
  1126. buf[len] = 0;
  1127. for(s2 = buf + 12; (s1 = (unsigned char *)strchr((char *)s2 + 1, '.')); s2 = s1)*s2 = (unsigned char)((s1 - s2) - 1);
  1128. *s2 = (len - (int)(s2 - buf)) - 1;
  1129. len++;
  1130. buf[len++] = 0;
  1131. buf[len++] = (makeauth == 1)? 0x0c : (af==AF_INET6? 0x1c:0x01); /* PTR:host address */
  1132. buf[len++] = 0;
  1133. buf[len++] = 1; /* INET */
  1134. if(usetcp){
  1135. buf-=2;
  1136. *(unsigned short*)buf = htons(len);
  1137. len+=2;
  1138. }
  1139. if(socksendto(sock, (struct sockaddr *)sinsr, buf, len, conf.timeouts[SINGLEBYTE_L]*1000) != len){
  1140. so._shutdown(sock, SHUT_RDWR);
  1141. so._closesocket(sock);
  1142. continue;
  1143. }
  1144. if(param) param->statscli64 += len;
  1145. len = sockrecvfrom(sock, (struct sockaddr *)sinsr, buf, 4096, conf.timeouts[DNS_TO]*1000);
  1146. so._shutdown(sock, SHUT_RDWR);
  1147. so._closesocket(sock);
  1148. if(len <= 13) {
  1149. continue;
  1150. }
  1151. if(param) param->statssrv64 += len;
  1152. if(usetcp){
  1153. unsigned short us;
  1154. us = ntohs(*(unsigned short*)buf);
  1155. len-=2;
  1156. buf+=2;
  1157. if(us > 4096 || us < len || (us > len && sockrecvfrom(sock, (struct sockaddr *)sinsr, buf+len, us-len, conf.timeouts[DNS_TO]*1000) != us-len)) {
  1158. continue;
  1159. }
  1160. }
  1161. if(*(unsigned short *)buf != serial)continue;
  1162. if((na = buf[7] + (((unsigned short)buf[6])<<8)) < 1) {
  1163. return 0;
  1164. }
  1165. nq = buf[5] + (((unsigned short)buf[4])<<8);
  1166. if (nq != 1) {
  1167. continue; /* we did only 1 request */
  1168. }
  1169. for(k = 13; k<len && buf[k]; k++) {
  1170. }
  1171. k++;
  1172. if( (k+4) >= len) {
  1173. continue;
  1174. }
  1175. k += 4;
  1176. if(na > 255) na = 255; /* somebody is very evil */
  1177. for (j = 0; j < na; j++) { /* now there should be answers */
  1178. while(buf[k] < 192 && buf[k] !=0 && (k+buf[k]+14) < len) k+= (buf[k] + 1);
  1179. if(!buf[k]) k--;
  1180. if((k+(af == AF_INET6?28:16)) > len) {
  1181. break;
  1182. }
  1183. flen = buf[k+11] + (((unsigned short)buf[k+10])<<8);
  1184. if((k+12+flen) > len) {
  1185. break;
  1186. }
  1187. if(makeauth != 1){
  1188. if(buf[k+2] != 0 || buf[k+3] != (af == AF_INET6?0x1c:0x1) || flen != (af == AF_INET6?16:4)) {
  1189. k+= (12 + flen);
  1190. continue; /* we need A IPv4 */
  1191. }
  1192. ttl = ntohl(*(unsigned long *)(buf + k + 6));
  1193. memcpy(value, buf + k + 12, af == AF_INET6? 16:4);
  1194. if(ttl < 60 || ttl > (3600*12)) ttl = 300;
  1195. hashadd(af == AF_INET6?&dns6_table:&dns_table, name, value, conf.time+ttl);
  1196. if(retttl) *retttl = ttl;
  1197. return 1;
  1198. }
  1199. else {
  1200. if(buf[k+2] != 0 || buf[k+3] != 0x0c) {
  1201. k+= (12 + flen);
  1202. continue; /* we need A PTR */
  1203. }
  1204. for (s2 = buf + k + 12; s2 < (buf + k + 12 + len) && *s2; ){
  1205. s1 = s2 + ((unsigned)*s2) + 1;
  1206. *s2 = '.';
  1207. s2 = s1;
  1208. }
  1209. *s2 = 0;
  1210. if(param->username)myfree(param->username);
  1211. param->username = (unsigned char *)mystrdup ((char *)buf + k + 13);
  1212. return udpresolve(af,param->username, value, NULL, NULL, 2);
  1213. }
  1214. }
  1215. }
  1216. return 0;
  1217. }
  1218. unsigned long myresolver(int af, unsigned char * name, unsigned char * value){
  1219. return udpresolve(af, name, value, NULL, NULL, 0);
  1220. }
  1221. unsigned long fakeresolver (int af, unsigned char *name, unsigned char * value){
  1222. memset(value, 0, af == AF_INET6? 16 : 4);
  1223. if(af == AF_INET6){
  1224. memset(value, 0, 16);
  1225. value[15] = 2;
  1226. }
  1227. else {
  1228. value[0] = 127;
  1229. value[1] = 0;
  1230. value[2] = 0;
  1231. value[3] = 2;
  1232. }
  1233. return 1;
  1234. }
  1235. #ifndef NOODBC
  1236. SQLHENV henv = NULL;
  1237. SQLHSTMT hstmt = NULL;
  1238. SQLHDBC hdbc = NULL;
  1239. char * sqlstring = NULL;
  1240. void close_sql(){
  1241. if(hstmt) {
  1242. SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
  1243. hstmt = NULL;
  1244. }
  1245. if(hdbc){
  1246. SQLDisconnect(hdbc);
  1247. SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
  1248. hdbc = NULL;
  1249. }
  1250. if(henv) {
  1251. SQLFreeHandle(SQL_HANDLE_ENV, henv);
  1252. henv = NULL;
  1253. }
  1254. }
  1255. int attempt = 0;
  1256. time_t attempt_time = 0;
  1257. int init_sql(char * s){
  1258. SQLRETURN retcode;
  1259. char * datasource;
  1260. char * username;
  1261. char * password;
  1262. char * string;
  1263. if(!s) return 0;
  1264. if(!sqlstring || strcmp(sqlstring, s)){
  1265. string = sqlstring;
  1266. sqlstring=mystrdup(s);
  1267. if(string)myfree(string);
  1268. }
  1269. if(hstmt || hdbc || henv) close_sql();
  1270. attempt++;
  1271. attempt_time = time(0);
  1272. if(!henv){
  1273. retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  1274. if (!henv || (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)){
  1275. henv = NULL;
  1276. return 0;
  1277. }
  1278. retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
  1279. if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
  1280. return 0;
  1281. }
  1282. }
  1283. if(!hdbc){
  1284. retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
  1285. if (!hdbc || (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)) {
  1286. hdbc = NULL;
  1287. SQLFreeHandle(SQL_HANDLE_ENV, henv);
  1288. henv = NULL;
  1289. return 0;
  1290. }
  1291. SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (void*)15, 0);
  1292. }
  1293. string = mystrdup(sqlstring);
  1294. if(!string) return 0;
  1295. datasource = strtok(string, ",");
  1296. username = strtok(NULL, ",");
  1297. password = strtok(NULL, ",");
  1298. /* Connect to data source */
  1299. retcode = SQLConnect(hdbc, (SQLCHAR*) datasource, (SQLSMALLINT)strlen(datasource),
  1300. (SQLCHAR*) username, (SQLSMALLINT)((username)?strlen(username):0),
  1301. (SQLCHAR*) password, (SQLSMALLINT)((password)?strlen(password):0));
  1302. myfree(string);
  1303. if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO){
  1304. SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
  1305. hdbc = NULL;
  1306. SQLFreeHandle(SQL_HANDLE_ENV, henv);
  1307. henv = NULL;
  1308. return 0;
  1309. }
  1310. retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
  1311. if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO){
  1312. close_sql();
  1313. return 0;
  1314. }
  1315. return 1;
  1316. }
  1317. void sqlerr (char *buf){
  1318. if(conf.stdlog){
  1319. fprintf(conf.stdlog, "%s\n", buf);
  1320. fflush(conf.stdlog);
  1321. }
  1322. pthread_mutex_unlock(&log_mutex);
  1323. }
  1324. unsigned char statbuf[8192];
  1325. void logsql(struct clientparam * param, const unsigned char *s) {
  1326. SQLRETURN ret;
  1327. int len;
  1328. if(param->nolog) return;
  1329. pthread_mutex_lock(&log_mutex);
  1330. len = dobuf(param, statbuf, s, (unsigned char *)"\'");
  1331. if(attempt > 5){
  1332. time_t t;
  1333. t = time(0);
  1334. if (t - attempt_time < 180){
  1335. sqlerr((char *)statbuf);
  1336. return;
  1337. }
  1338. }
  1339. if(!hstmt){
  1340. if(!init_sql(sqlstring)) {
  1341. sqlerr((char *)statbuf);
  1342. return;
  1343. }
  1344. }
  1345. if(hstmt){
  1346. ret = SQLExecDirect(hstmt, (SQLCHAR *)statbuf, (SQLINTEGER)len);
  1347. if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO){
  1348. close_sql();
  1349. if(!init_sql(sqlstring)){
  1350. sqlerr((char *)statbuf);
  1351. return;
  1352. }
  1353. if(hstmt) {
  1354. ret = SQLExecDirect(hstmt, (SQLCHAR *)statbuf, (SQLINTEGER)len);
  1355. if(ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO){
  1356. sqlerr((char *)statbuf);
  1357. return;
  1358. }
  1359. attempt = 0;
  1360. }
  1361. }
  1362. attempt = 0;
  1363. }
  1364. pthread_mutex_unlock(&log_mutex);
  1365. }
  1366. #endif