auth.c 38 KB

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