auth.c 36 KB

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