auth.c 36 KB

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