auth.c 35 KB

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