proxymain.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2008 by ZARAZA <3APA3A@security.nnov.ru>
  4. please read License Agreement
  5. $Id: proxymain.c,v 1.85 2012-04-15 22:46:09 vlad Exp $
  6. */
  7. #include "proxy.h"
  8. #ifndef MODULEMAINFUNC
  9. #define MODULEMAINFUNC main
  10. #define STDMAIN
  11. #ifndef _WINCE
  12. int main (int argc, char** argv){
  13. #else
  14. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow){
  15. int argc;
  16. char ** argv;
  17. WNDCLASS wc;
  18. HWND hwnd = 0;
  19. #endif
  20. #else
  21. extern int linenum;
  22. extern int haveerror;
  23. int MODULEMAINFUNC (int argc, char** argv){
  24. #endif
  25. SOCKET sock = INVALID_SOCKET;
  26. int i=0;
  27. SASIZETYPE size;
  28. pthread_t thread;
  29. struct clientparam defparam;
  30. struct srvparam srv;
  31. struct clientparam * newparam;
  32. int error = 0;
  33. unsigned sleeptime;
  34. unsigned char buf[256];
  35. char *hostname=NULL;
  36. int opt = 1, isudp;
  37. FILE *fp = NULL;
  38. int nlog = 5000;
  39. char loghelp[] =
  40. #ifdef STDMAIN
  41. #ifndef _WIN32
  42. " -I inetd mode (requires real socket, doesn't work with TTY)\n"
  43. " -l@IDENT log to syslog IDENT\n"
  44. #endif
  45. " -d go to background (daemon)\n"
  46. #else
  47. " -u never ask for username\n"
  48. #endif
  49. " -fFORMAT logging format (see documentation)\n"
  50. " -l log to stderr\n"
  51. " -lFILENAME log to FILENAME\n"
  52. " -bBUFSIZE size of network buffer (default 4096 for TCP, 16384 for UDP)\n"
  53. " -t be silent (do not log service start/stop)\n"
  54. " -iIP ip address or internal interface (clients are expected to connect)\n"
  55. " -eIP ip address or external interface (outgoing connection will have this)\n";
  56. #ifdef _WIN32
  57. unsigned long ul = 1;
  58. #else
  59. #ifdef STDMAIN
  60. int inetd = 0;
  61. #endif
  62. #endif
  63. SOCKET new_sock = INVALID_SOCKET;
  64. struct linger lg;
  65. #ifdef _WIN32
  66. HANDLE h;
  67. #endif
  68. #ifdef STDMAIN
  69. #ifdef _WINCE
  70. argc = ceparseargs((char *)lpCmdLine);
  71. argv = ceargv;
  72. if(FindWindow(lpCmdLine, lpCmdLine)) return 0;
  73. ZeroMemory(&wc,sizeof(wc));
  74. wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  75. wc.hInstance=hInstance;
  76. wc.hCursor=LoadCursor(NULL,IDC_ARROW);
  77. wc.lpfnWndProc=DefWindowProc;
  78. wc.style=CS_HREDRAW|CS_VREDRAW;
  79. wc.lpszClassName=lpCmdLine;
  80. RegisterClass(&wc);
  81. hwnd = CreateWindowEx(WS_EX_TOOLWINDOW,lpCmdLine,lpCmdLine,WS_VISIBLE|WS_POPUP,0,0,0,0,0,0,hInstance,0);
  82. #endif
  83. #ifdef _WIN32
  84. WSADATA wd;
  85. WSAStartup(MAKEWORD( 1, 1 ), &wd);
  86. #else
  87. signal(SIGPIPE, SIG_IGN);
  88. pthread_attr_init(&pa);
  89. pthread_attr_setstacksize(&pa,PTHREAD_STACK_MIN + 16384);
  90. pthread_attr_setdetachstate(&pa,PTHREAD_CREATE_DETACHED);
  91. #endif
  92. #endif
  93. srvinit(&srv, &defparam);
  94. srv.pf = childdef.pf;
  95. isudp = childdef.isudp;
  96. srv.service = defparam.service = childdef.service;
  97. #ifndef STDMAIN
  98. srv.acl = copyacl(conf.acl);
  99. srv.authfuncs = copyauth(conf.authfuncs);
  100. if(!conf.services){
  101. conf.services = &srv;
  102. }
  103. else {
  104. srv.next = conf.services;
  105. conf.services = conf.services->prev = &srv;
  106. }
  107. #else
  108. srv.nouser = 1;
  109. #endif
  110. for (i=1; i<argc; i++) {
  111. if(*argv[i]=='-') {
  112. switch(argv[i][1]) {
  113. case 'd':
  114. if(!conf.demon)daemonize();
  115. conf.demon = 1;
  116. break;
  117. case 'l':
  118. srv.logfunc = logstdout;
  119. srv.logtarget = (unsigned char*)argv[i] + 2;
  120. if(argv[i][2]) {
  121. if(argv[i][2]=='@'){
  122. #ifdef STDMAIN
  123. #ifndef _WIN32
  124. openlog(argv[i]+3, LOG_PID, LOG_DAEMON);
  125. srv.logfunc = logsyslog;
  126. #endif
  127. #endif
  128. }
  129. else {
  130. fp = fopen(argv[i] + 2, "a");
  131. if (fp) {
  132. srv.stdlog = fp;
  133. fseek(fp, 0L, SEEK_END);
  134. }
  135. }
  136. }
  137. break;
  138. case 'i':
  139. srv.intip = getip((unsigned char *)argv[i]+2);
  140. break;
  141. case 'e':
  142. srv.extip = getip((unsigned char *)argv[i]+2);
  143. break;
  144. case 'p':
  145. srv.intport = htons(atoi(argv[i]+2));
  146. break;
  147. case 'b':
  148. srv.bufsize = atoi(argv[i]+2);
  149. break;
  150. case 'n':
  151. srv.usentlm = atoi(argv[i]+2);
  152. break;
  153. #ifdef STDMAIN
  154. #ifndef _WIN32
  155. case 'I':
  156. size = sizeof(defparam.sinc);
  157. if(so._getsockname(0, (struct sockaddr*)&defparam.sinc, &size) ||
  158. defparam.sinc.sin_family != AF_INET) error = 1;
  159. else inetd = 1;
  160. break;
  161. #endif
  162. #endif
  163. case 'f':
  164. srv.logformat = (unsigned char *)argv[i] + 2;
  165. break;
  166. case 't':
  167. srv.silent = 1;
  168. break;
  169. case 'h':
  170. hostname = argv[i] + 2;
  171. break;
  172. case 'u':
  173. srv.nouser = 1;
  174. break;
  175. case 'T':
  176. srv.transparent = 1;
  177. break;
  178. case 's':
  179. case 'a':
  180. srv.singlepacket = 1 + atoi(argv[i]+2);
  181. break;
  182. default:
  183. error = 1;
  184. break;
  185. }
  186. }
  187. else break;
  188. }
  189. #ifndef STDMAIN
  190. if(childdef.port) {
  191. #endif
  192. #ifndef PORTMAP
  193. if (error || i!=argc) {
  194. #ifndef STDMAIN
  195. haveerror = 1;
  196. conf.threadinit = 0;
  197. #endif
  198. fprintf(stderr, "%s of " VERSION " (" BUILDDATE ")\n"
  199. "Usage: %s options\n"
  200. "Available options are:\n"
  201. "%s"
  202. " -pPORT - service port to accept connections\n"
  203. "%s"
  204. "\tExample: %s -i127.0.0.1\n\n"
  205. "%s",
  206. argv[0], argv[0], loghelp, childdef.helpmessage, argv[0],
  207. #ifdef STDMAIN
  208. copyright
  209. #else
  210. ""
  211. #endif
  212. );
  213. return (1);
  214. }
  215. #endif
  216. #ifndef STDMAIN
  217. }
  218. else {
  219. #endif
  220. #ifndef NOPORTMAP
  221. if (error || argc != i+3 || *argv[i]=='-'|| (srv.intport = htons((unsigned short)atoi(argv[i])))==0 || (srv.targetport = htons((unsigned short)atoi(argv[i+2])))==0) {
  222. #ifndef STDMAIN
  223. haveerror = 1;
  224. conf.threadinit = 0;
  225. #endif
  226. fprintf(stderr, "%s of " VERSION " (" BUILDDATE ")\n"
  227. "Usage: %s options"
  228. " [-e<external_ip>] <port_to_bind>"
  229. " <target_hostname> <target_port>\n"
  230. "Available options are:\n"
  231. "%s"
  232. "%s"
  233. "\tExample: %s -d -i127.0.0.1 6666 serv.somehost.ru 6666\n\n"
  234. "%s",
  235. argv[0], argv[0], loghelp, childdef.helpmessage, argv[0],
  236. #ifdef STDMAIN
  237. copyright
  238. #else
  239. ""
  240. #endif
  241. );
  242. return (1);
  243. }
  244. srv.target = (unsigned char *)mystrdup(argv[i+1]);
  245. #endif
  246. #ifndef STDMAIN
  247. }
  248. #else
  249. #ifndef _WIN32
  250. if(inetd) {
  251. fcntl(0,F_SETFL,O_NONBLOCK);
  252. if(!isudp){
  253. so._setsockopt(0, SOL_SOCKET, SO_LINGER, (unsigned char *)&lg, sizeof(lg));
  254. so._setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (unsigned char *)&opt, sizeof(int));
  255. }
  256. defparam.clisock = 0;
  257. if(! (newparam = myalloc (sizeof(defparam)))){
  258. return 2;
  259. };
  260. memcpy(newparam, &defparam, sizeof(defparam));
  261. return((*srv.pf)((void *)newparam)? 1:0);
  262. }
  263. #endif
  264. #endif
  265. srvinit2(&srv, &defparam);
  266. if(!srv.intport) srv.intport = htons(childdef.port);
  267. if(!defparam.sinc.sin_port) defparam.sinc.sin_port = htons(childdef.port);
  268. if(hostname)parsehostname(hostname, &defparam, childdef.port);
  269. #ifndef STDMAIN
  270. copyfilter(conf.filters, &srv);
  271. conf.threadinit = 0;
  272. #endif
  273. if(srv.srvsock == INVALID_SOCKET){
  274. if(!isudp){
  275. lg.l_onoff = 1;
  276. lg.l_linger = conf.timeouts[STRING_L];
  277. sock=so._socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  278. }
  279. else {
  280. sock=so._socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  281. }
  282. if( sock == INVALID_SOCKET) {
  283. perror("socket()");
  284. return -2;
  285. }
  286. #ifdef _WIN32
  287. ioctlsocket(sock, FIONBIO, &ul);
  288. #else
  289. fcntl(sock,F_SETFL,O_NONBLOCK);
  290. #endif
  291. srv.srvsock = sock;
  292. if(so._setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (unsigned char *)&opt, sizeof(int)))perror("setsockopt()");
  293. #ifdef SO_REUSEPORT
  294. so._setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (unsigned char *)&opt, sizeof(int));
  295. #endif
  296. }
  297. size = sizeof(defparam.sinc);
  298. for(sleeptime = SLEEPTIME * 100; so._bind(sock, (struct sockaddr*)&defparam.sinc, size)==-1; usleep(sleeptime)) {
  299. sprintf((char *)buf, "bind(): %s", strerror(errno));
  300. if(!srv.silent)(*srv.logfunc)(&defparam, buf);
  301. sleeptime = (sleeptime<<1);
  302. if(!sleeptime) {
  303. so._closesocket(sock);
  304. return -3;
  305. }
  306. }
  307. if(!isudp){
  308. if(so._listen (sock, 1 + (srv.maxchild>>4))==-1) {
  309. sprintf((char *)buf, "listen(): %s", strerror(errno));
  310. if(!srv.silent)(*srv.logfunc)(&defparam, buf);
  311. return -4;
  312. }
  313. }
  314. else
  315. defparam.clisock = sock;
  316. if(!srv.silent){
  317. sprintf((char *)buf, "Accepting connections [%u/%u]", (unsigned)getpid(), (unsigned)pthread_self());
  318. (*srv.logfunc)(&defparam, buf);
  319. }
  320. defparam.sinc.sin_addr.s_addr = defparam.sins.sin_addr.s_addr = 0;
  321. defparam.sinc.sin_port = defparam.sins.sin_port = 0;
  322. srv.fds.fd = sock;
  323. srv.fds.events = POLLIN;
  324. for (;;) {
  325. for(;;){
  326. while((conf.paused == srv.version && srv.childcount >= srv.maxchild)){
  327. nlog++;
  328. if(nlog > 5000) {
  329. sprintf((char *)buf, "Warning: too many connected clients (%d/%d)", srv.childcount, srv.maxchild);
  330. if(!srv.silent)(*srv.logfunc)(&defparam, buf);
  331. nlog = 0;
  332. }
  333. usleep(SLEEPTIME);
  334. }
  335. if (conf.paused != srv.version) break;
  336. if (srv.fds.events & POLLIN) {
  337. error = so._poll(&srv.fds, 1, 1000);
  338. }
  339. else {
  340. usleep(SLEEPTIME);
  341. continue;
  342. }
  343. if (error >= 1) break;
  344. if (error == 0) continue;
  345. if (errno != EAGAIN && errno != EINTR) {
  346. sprintf((char *)buf, "poll(): %s/%d", strerror(errno), errno);
  347. if(!srv.silent)(*srv.logfunc)(&defparam, buf);
  348. break;
  349. }
  350. continue;
  351. }
  352. if((conf.paused != srv.version) || (error < 0)) break;
  353. if(!isudp){
  354. size = sizeof(defparam.sinc);
  355. new_sock = so._accept(sock, (struct sockaddr*)&defparam.sinc, &size);
  356. if(new_sock == INVALID_SOCKET){
  357. sprintf((char *)buf, "accept(): %s", strerror(errno));
  358. if(!srv.silent)(*srv.logfunc)(&defparam, buf);
  359. continue;
  360. }
  361. #ifdef _WIN32
  362. ioctlsocket(new_sock, FIONBIO, &ul);
  363. #else
  364. fcntl(new_sock,F_SETFL,O_NONBLOCK);
  365. #endif
  366. so._setsockopt(new_sock, SOL_SOCKET, SO_LINGER, (unsigned char *)&lg, sizeof(lg));
  367. so._setsockopt(new_sock, SOL_SOCKET, SO_OOBINLINE, (unsigned char *)&opt, sizeof(int));
  368. }
  369. else
  370. srv.fds.events = 0;
  371. if(! (newparam = myalloc (sizeof(defparam)))){
  372. if(!isudp) so._closesocket(new_sock);
  373. defparam.res = 21;
  374. if(!srv.silent)(*srv.logfunc)(&defparam, (unsigned char *)"Memory Allocation Failed");
  375. usleep(SLEEPTIME);
  376. continue;
  377. };
  378. memcpy(newparam, &defparam, sizeof(defparam));
  379. if(defparam.hostname)newparam->hostname=strdup(defparam.hostname);
  380. clearstat(newparam);
  381. if(!isudp) newparam->clisock = new_sock;
  382. #ifndef STDMAIN
  383. if(makefilters(&srv, newparam) > CONTINUE){
  384. freeparam(newparam);
  385. continue;
  386. }
  387. #endif
  388. newparam->prev = newparam->next = NULL;
  389. pthread_mutex_lock(&srv.counter_mutex);
  390. if(!srv.child){
  391. srv.child = newparam;
  392. }
  393. else {
  394. newparam->next = srv.child;
  395. srv.child = srv.child->prev = newparam;
  396. }
  397. #ifdef _WIN32
  398. #ifndef _WINCE
  399. h = (HANDLE)_beginthreadex((LPSECURITY_ATTRIBUTES )NULL, (unsigned)16384, (BEGINTHREADFUNC)srv.pf, (void *) newparam, 0, &thread);
  400. #else
  401. h = (HANDLE)CreateThread((LPSECURITY_ATTRIBUTES )NULL, (unsigned)32768, (BEGINTHREADFUNC)srv.pf, (void *) newparam, 0, &thread);
  402. #endif
  403. newparam->threadid = (unsigned)thread;
  404. if (h) {
  405. srv.childcount++;
  406. CloseHandle(h);
  407. }
  408. else {
  409. myfree(newparam);
  410. }
  411. #else
  412. if((error = pthread_create(&thread, &pa, (PTHREADFUNC)srv.pf, (void *)newparam))){
  413. sprintf((char *)buf, "pthread_create(): %s", strerror(error));
  414. if(!srv.silent)(*srv.logfunc)(&defparam, buf);
  415. freeparam(newparam);
  416. }
  417. else {
  418. srv.childcount++;
  419. newparam->threadid = (unsigned)thread;
  420. }
  421. #endif
  422. pthread_mutex_unlock(&srv.counter_mutex);
  423. memset(&defparam.sinc, 0, sizeof(defparam.sinc));
  424. if(isudp) while(!srv.fds.events)usleep(SLEEPTIME);
  425. }
  426. if(!srv.silent) srv.logfunc(&defparam, (unsigned char *)"Exiting thread");
  427. if(fp) fclose(fp);
  428. srvfree(&srv);
  429. if(defparam.hostname)myfree(defparam.hostname);
  430. #ifndef STDMAIN
  431. if(srv.next)srv.next->prev = srv.prev;
  432. if(srv.prev)srv.prev->next = srv.next;
  433. else conf.services = srv.next;
  434. freeacl(srv.acl);
  435. freeauth(srv.authfuncs);
  436. #endif
  437. return 0;
  438. }
  439. void srvinit(struct srvparam * srv, struct clientparam *param){
  440. memset(srv, 0, sizeof(struct srvparam));
  441. srv->version = conf.paused;
  442. srv->logfunc = conf.logfunc;
  443. srv->logformat = conf.logformat;
  444. srv->authfunc = conf.authfunc;
  445. srv->usentlm = 0;
  446. srv->maxchild = conf.maxchild;
  447. srv->time_start = time(NULL);
  448. srv->logtarget = conf.logtarget;
  449. srv->srvsock = INVALID_SOCKET;
  450. srv->logdumpsrv = conf.logdumpsrv;
  451. srv->logdumpcli = conf.logdumpcli;
  452. memset(param, 0, sizeof(struct clientparam));
  453. param->srv = srv;
  454. param->remsock = param->clisock = param->ctrlsock = param->ctrlsocksrv = INVALID_SOCKET;
  455. param->req.sin_family = param->sins.sin_family = param->sinc.sin_family = AF_INET;
  456. pthread_mutex_init(&srv->counter_mutex, NULL);
  457. }
  458. void srvinit2(struct srvparam * srv, struct clientparam *param){
  459. if(srv->logformat){
  460. char *s;
  461. if(*srv->logformat == '-' && (s = strchr((char *)srv->logformat + 1, '+')) && s[1]){
  462. *s = 0;
  463. srv->nonprintable = (unsigned char *)mystrdup((char *)srv->logformat + 1);
  464. srv->replace = s[1];
  465. srv->logformat = (unsigned char *)mystrdup(s + 2);
  466. *s = '+';
  467. }
  468. else srv->logformat = (unsigned char *)mystrdup((char *)srv->logformat);
  469. }
  470. if(srv->logtarget) srv->logtarget = (unsigned char *)mystrdup((char *)srv->logtarget);
  471. if(!srv->intip) srv->intip = conf.intip;
  472. param->sinc.sin_addr.s_addr = srv->intip;
  473. param->sinc.sin_port = srv->intport;
  474. if(!srv->extip) srv->extip = conf.extip;
  475. param->sins.sin_addr.s_addr = param->extip = srv->extip;
  476. if(!srv->extport) srv->extport = htons(conf.extport);
  477. param->sins.sin_port = param->extport = srv->extport;
  478. }
  479. void srvfree(struct srvparam * srv){
  480. if(srv->srvsock != INVALID_SOCKET) so._closesocket(srv->srvsock);
  481. srv->srvsock = INVALID_SOCKET;
  482. srv->service = S_ZOMBIE;
  483. while(srv->child) usleep(SLEEPTIME * 100);
  484. #ifndef STDMAIN
  485. if(srv->filter){
  486. while(srv->nfilters){
  487. srv->nfilters--;
  488. if(srv->filter[srv->nfilters].filter_close){
  489. (*srv->filter[srv->nfilters].filter_close)(srv->filter[srv->nfilters].data);
  490. }
  491. }
  492. myfree(srv->filter);
  493. }
  494. #endif
  495. pthread_mutex_destroy(&srv->counter_mutex);
  496. if(srv->target) myfree(srv->target);
  497. if(srv->logtarget) myfree(srv->logtarget);
  498. if(srv->logformat) myfree(srv->logformat);
  499. if(srv->nonprintable) myfree(srv->nonprintable);
  500. }
  501. void freeparam(struct clientparam * param) {
  502. if(param->res == 2) return;
  503. if(param->datfilterssrv) myfree(param->datfilterssrv);
  504. #ifndef STDMAIN
  505. if(param->reqfilters) myfree(param->reqfilters);
  506. if(param->hdrfilterscli) myfree(param->hdrfilterscli);
  507. if(param->hdrfilterssrv) myfree(param->hdrfilterssrv);
  508. if(param->predatfilters) myfree(param->predatfilters);
  509. if(param->datfilterscli) myfree(param->datfilterscli);
  510. if(param->filters){
  511. if(param->nfilters)while(param->nfilters--){
  512. if(param->filters[param->nfilters].filter->filter_clear)
  513. (*param->filters[param->nfilters].filter->filter_clear)(param->filters[param->nfilters].data);
  514. }
  515. myfree(param->filters);
  516. }
  517. #endif
  518. if(param->clibuf) myfree(param->clibuf);
  519. if(param->srvbuf) myfree(param->srvbuf);
  520. if(param->srv){
  521. pthread_mutex_lock(&param->srv->counter_mutex);
  522. if(param->prev){
  523. param->prev->next = param->next;
  524. }
  525. else
  526. param->srv->child = param->next;
  527. if(param->next){
  528. param->next->prev = param->prev;
  529. }
  530. (param->srv->childcount)--;
  531. pthread_mutex_unlock(&param->srv->counter_mutex);
  532. }
  533. if(param->hostname) myfree(param->hostname);
  534. if(param->username) myfree(param->username);
  535. if(param->password) myfree(param->password);
  536. if(param->extusername) myfree(param->extusername);
  537. if(param->extpassword) myfree(param->extpassword);
  538. if(param->ctrlsocksrv != INVALID_SOCKET && param->ctrlsocksrv != param->remsock) {
  539. so._shutdown(param->ctrlsocksrv, SHUT_RDWR);
  540. so._closesocket(param->ctrlsocksrv);
  541. }
  542. if(param->ctrlsock != INVALID_SOCKET && param->ctrlsock != param->clisock) {
  543. so._shutdown(param->ctrlsock, SHUT_RDWR);
  544. so._closesocket(param->ctrlsock);
  545. }
  546. if(param->remsock != INVALID_SOCKET) {
  547. so._shutdown(param->remsock, SHUT_RDWR);
  548. so._closesocket(param->remsock);
  549. }
  550. if(param->clisock != INVALID_SOCKET) {
  551. so._shutdown(param->clisock, SHUT_RDWR);
  552. so._closesocket(param->clisock);
  553. }
  554. myfree(param);
  555. }
  556. #ifndef STDMAIN
  557. static void * itcopy (void * from, size_t size){
  558. void * ret;
  559. if(!from) return NULL;
  560. ret = myalloc(size);
  561. if(ret) memcpy(ret, from, size);
  562. return ret;
  563. }
  564. struct auth * copyauth (struct auth * authfuncs){
  565. struct auth * newauth = NULL;
  566. newauth = authfuncs = itcopy(authfuncs, sizeof(struct auth));
  567. for( ; authfuncs; authfuncs = authfuncs->next = itcopy(authfuncs->next, sizeof(struct auth)));
  568. return newauth;
  569. }
  570. struct ace * copyacl (struct ace *ac){
  571. struct ace * ret = NULL;
  572. struct iplist *ipl;
  573. struct portlist *pl;
  574. struct userlist *ul;
  575. struct chain *ch;
  576. struct period *pel;
  577. struct hostname *hst;
  578. ret = ac = itcopy(ac, sizeof(struct ace));
  579. for( ; ac; ac = ac->next = itcopy(ac->next, sizeof(struct ace))){
  580. ac->src = itcopy(ac->src, sizeof(struct iplist));
  581. for(ipl = ac->src; ipl; ipl = ipl->next = itcopy(ipl->next, sizeof(struct iplist)));
  582. ac->dst = itcopy(ac->dst, sizeof(struct iplist));
  583. for(ipl = ac->dst; ipl; ipl = ipl->next = itcopy(ipl->next, sizeof(struct iplist)));
  584. ac->ports = itcopy(ac->ports, sizeof(struct portlist));
  585. for(pl = ac->ports; pl; pl = pl->next = itcopy(pl->next, sizeof(struct portlist)));
  586. ac->periods = itcopy(ac->periods, sizeof(struct period));
  587. for(pel = ac->periods; pel; pel = pel->next = itcopy(pel->next, sizeof(struct period)));
  588. ac->users = itcopy(ac->users, sizeof(struct userlist));
  589. for(ul = ac->users; ul; ul = ul->next = itcopy(ul->next, sizeof(struct userlist))){
  590. if(ul->user) ul->user = (unsigned char*)mystrdup((char *)ul->user);
  591. }
  592. ac->dstnames = itcopy(ac->dstnames, sizeof(struct hostname));
  593. for(hst = ac->dstnames; hst; hst = hst->next = itcopy(hst->next, sizeof(struct hostname))){
  594. if(hst->name) hst->name = (unsigned char*)mystrdup((char *)hst->name);
  595. }
  596. ac->chains = itcopy(ac->chains, sizeof(struct chain));
  597. for(ch = ac->chains; ch; ch = ch->next = itcopy(ch->next, sizeof(struct chain))){
  598. if(ch->extuser)ch->extuser = (unsigned char*)mystrdup((char *)ch->extuser);
  599. if(ch->extpass)ch->extpass = (unsigned char*)mystrdup((char *)ch->extpass);
  600. }
  601. }
  602. return ret;
  603. }
  604. void copyfilter (struct filter *filter, struct srvparam *srv){
  605. int nfilters = 0;
  606. if(!filter) return;
  607. for(srv->filter = filter; srv->filter; srv->filter = srv->filter->next) nfilters++;
  608. srv->filter = myalloc(sizeof(struct filter) * nfilters);
  609. if(!srv->filter) return;
  610. for(; filter; filter = filter->next){
  611. void *data = NULL;
  612. if(!filter->filter_open || !(data = (*filter->filter_open)(filter->data, srv))) continue;
  613. memcpy(srv->filter + srv->nfilters, filter, sizeof(struct filter));
  614. srv->filter[srv->nfilters].data = data;
  615. if(srv->nfilters>0)srv->filter[srv->nfilters - 1].next = srv->filter + srv->nfilters;
  616. srv->nfilters++;
  617. if(filter->filter_request)srv->nreqfilters++;
  618. if(filter->filter_header_srv)srv->nhdrfilterssrv++;
  619. if(filter->filter_header_cli)srv->nhdrfilterscli++;
  620. if(filter->filter_predata)srv->npredatfilters++;
  621. if(filter->filter_data_srv)srv->ndatfilterssrv++;
  622. if(filter->filter_data_cli)srv->ndatfilterscli++;
  623. }
  624. }
  625. FILTER_ACTION makefilters (struct srvparam *srv, struct clientparam *param){
  626. FILTER_ACTION res=PASS;
  627. FILTER_ACTION action;
  628. int i;
  629. if(!srv->nfilters) return PASS;
  630. if(!(param->filters = myalloc(sizeof(struct filterp) * srv->nfilters)) ||
  631. (srv->nreqfilters && !(param->reqfilters = myalloc(sizeof(struct filterp *) * srv->nreqfilters))) ||
  632. (srv->nhdrfilterssrv && !(param->hdrfilterssrv = myalloc(sizeof(struct filterp *) * srv->nhdrfilterssrv))) ||
  633. (srv->nhdrfilterscli && !(param->hdrfilterscli = myalloc(sizeof(struct filterp *) * srv->nhdrfilterscli))) ||
  634. (srv->npredatfilters && !(param->predatfilters = myalloc(sizeof(struct filterp *) * srv->npredatfilters))) ||
  635. (srv->ndatfilterssrv && !(param->datfilterssrv = myalloc(sizeof(struct filterp *) * srv->ndatfilterssrv))) ||
  636. (srv->ndatfilterscli && !(param->datfilterscli = myalloc(sizeof(struct filterp *) * srv->ndatfilterscli)))
  637. ){
  638. param->res = 21;
  639. return REJECT;
  640. }
  641. for(i=0; i<srv->nfilters; i++){
  642. if(!srv->filter[i].filter_client)continue;
  643. action = (*srv->filter[i].filter_client)(srv->filter[i].data, param, &param->filters[param->nfilters].data);
  644. if(action == PASS) continue;
  645. if(action > CONTINUE) return action;
  646. param->filters[param->nfilters].filter = srv->filter + i;
  647. if(srv->filter[i].filter_request)param->reqfilters[param->nreqfilters++] = param->filters + param->nfilters;
  648. if(srv->filter[i].filter_header_cli)param->hdrfilterscli[param->nhdrfilterscli++] = param->filters + param->nfilters;
  649. if(srv->filter[i].filter_header_srv)param->hdrfilterssrv[param->nhdrfilterssrv++] = param->filters + param->nfilters;
  650. if(srv->filter[i].filter_predata)param->predatfilters[param->npredatfilters++] = param->filters + param->nfilters;
  651. if(srv->filter[i].filter_data_cli)param->datfilterscli[param->ndatfilterscli++] = param->filters + param->nfilters;
  652. if(srv->filter[i].filter_data_srv)param->datfilterssrv[param->ndatfilterssrv++] = param->filters + param->nfilters;
  653. param->nfilters++;
  654. }
  655. return res;
  656. }
  657. static void * itfree(void *data, void * retval){
  658. myfree(data);
  659. return retval;
  660. }
  661. void freepwl(struct passwords *pwl){
  662. for(; pwl; pwl = (struct passwords *)itfree(pwl, pwl->next)){
  663. if(pwl->user)myfree(pwl->user);
  664. if(pwl->password)myfree(pwl->password);
  665. }
  666. }
  667. void freeauth(struct auth * authfuncs){
  668. for(; authfuncs; authfuncs = (struct auth *)itfree(authfuncs, authfuncs->next));
  669. }
  670. void freeacl(struct ace *ac){
  671. struct iplist *ipl;
  672. struct portlist *pl;
  673. struct userlist *ul;
  674. struct chain *ch;
  675. struct period *pel;
  676. struct hostname *hst;
  677. for(; ac; ac = (struct ace *) itfree(ac, ac->next)){
  678. for(ipl = ac->src; ipl; ipl = (struct iplist *)itfree(ipl, ipl->next));
  679. for(ipl = ac->dst; ipl; ipl = (struct iplist *)itfree(ipl,ipl->next));
  680. for(pl = ac->ports; pl; pl = (struct portlist *)itfree(pl, pl->next));
  681. for(pel = ac->periods; pel; pel = (struct period *)itfree(pel, pel->next));
  682. for(ul = ac->users; ul; ul = (struct userlist *)itfree(ul, ul->next)){
  683. if(ul->user)myfree(ul->user);
  684. }
  685. for(hst = ac->dstnames; hst; hst = (struct hostname *)itfree(hst, hst->next)){
  686. if(hst->name)myfree(hst->name);
  687. }
  688. for(ch = ac->chains; ch; ch = (struct chain *) itfree(ch, ch->next)){
  689. if(ch->extuser) myfree(ch->extuser);
  690. if(ch->extpass) myfree(ch->extpass);
  691. }
  692. }
  693. }
  694. void freeconf(struct extparam *confp){
  695. struct bandlim * bl;
  696. struct bandlim * blout;
  697. struct trafcount * tc;
  698. struct passwords *pw;
  699. struct ace *acl;
  700. struct filemon *fm;
  701. int counterd, archiverc;
  702. unsigned char *logname, *logtarget;
  703. unsigned char **archiver;
  704. unsigned char * logformat;
  705. int i;
  706. pthread_mutex_lock(&tc_mutex);
  707. confp->trafcountfunc = NULL;
  708. tc = confp->trafcounter;
  709. confp->trafcounter = NULL;
  710. counterd = confp->counterd;
  711. confp->counterd = -1;
  712. pthread_mutex_unlock(&tc_mutex);
  713. if(tc)dumpcounters(tc,counterd);
  714. for(; tc; tc = (struct trafcount *) itfree(tc, tc->next)){
  715. if(tc->comment)myfree(tc->comment);
  716. freeacl(tc->ace);
  717. }
  718. confp->countertype = NONE;
  719. logtarget = confp->logtarget;
  720. confp->logtarget = NULL;
  721. logformat = confp->logformat;
  722. confp->logformat = NULL;
  723. pthread_mutex_lock(&bandlim_mutex);
  724. bl = confp->bandlimiter;
  725. blout = confp->bandlimiterout;
  726. confp->bandlimiter = NULL;
  727. confp->bandlimiterout = NULL;
  728. confp->bandlimfunc = NULL;
  729. pthread_mutex_unlock(&bandlim_mutex);
  730. logname = confp->logname;
  731. confp->logname = NULL;
  732. archiverc = confp->archiverc;
  733. confp->archiverc = 0;
  734. archiver = confp->archiver;
  735. confp->archiver = NULL;
  736. fm = confp->fmon;
  737. confp->fmon = NULL;
  738. pw = confp->pwl;
  739. confp->pwl = NULL;
  740. confp->rotate = 0;
  741. confp->logtype = NONE;
  742. confp->authfunc = ipauth;
  743. confp->bandlimfunc = NULL;
  744. confp->intip = confp->extip = 0;
  745. confp->intport = confp->extport = 0;
  746. confp->singlepacket = 0;
  747. confp->maxchild = 100;
  748. resolvfunc = NULL;
  749. acl = confp->acl;
  750. confp->acl = NULL;
  751. confp->logtime = confp->time = 0;
  752. usleep(SLEEPTIME);
  753. freeacl(acl);
  754. freepwl(pw);
  755. for(; bl; bl = (struct bandlim *) itfree(bl, bl->next)) freeacl(bl->ace);
  756. for(; blout; blout = (struct bandlim *) itfree(blout, blout->next))freeacl(blout->ace);
  757. if(counterd != -1) {
  758. close(counterd);
  759. }
  760. for(; fm; fm = (struct filemon *)itfree(fm, fm->next)){
  761. if(fm->path) myfree(fm->path);
  762. }
  763. if(logtarget) {
  764. myfree(logtarget);
  765. }
  766. if(logname) {
  767. myfree(logname);
  768. }
  769. if(logformat) {
  770. myfree(logformat);
  771. }
  772. if(archiver) {
  773. for(i = 0; i < archiverc; i++) myfree(archiver[i]);
  774. myfree(archiver);
  775. }
  776. }
  777. FILTER_ACTION handlereqfilters(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p){
  778. FILTER_ACTION action;
  779. int i;
  780. for(i=0; i<param->nreqfilters; i++){
  781. action = (*param->reqfilters[i]->filter->filter_request)(param->reqfilters[i]->data, param, buf_p, bufsize_p, offset, length_p);
  782. if(action!=CONTINUE) return action;
  783. }
  784. return PASS;
  785. }
  786. FILTER_ACTION handlehdrfilterssrv(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p){
  787. FILTER_ACTION action;
  788. int i;
  789. for(i=0; i<param->nhdrfilterssrv; i++){
  790. action = (*param->hdrfilterssrv[i]->filter->filter_header_srv)(param->hdrfilterssrv[i]->data, param, buf_p, bufsize_p, offset, length_p);
  791. if(action!=CONTINUE) return action;
  792. }
  793. return PASS;
  794. }
  795. FILTER_ACTION handlehdrfilterscli(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p){
  796. FILTER_ACTION action;
  797. int i;
  798. for(i = 0; i < param->nhdrfilterscli; i++){
  799. action = (*param->hdrfilterscli[i]->filter->filter_header_cli)(param->hdrfilterscli[i]->data, param, buf_p, bufsize_p, offset, length_p);
  800. if(action!=CONTINUE) return action;
  801. }
  802. return PASS;
  803. }
  804. #endif
  805. FILTER_ACTION handlepredatflt(struct clientparam *cparam){
  806. #ifndef STDMAIN
  807. FILTER_ACTION action;
  808. int i;
  809. for(i=0; i<cparam->npredatfilters ;i++){
  810. action = (*cparam->predatfilters[i]->filter->filter_predata)(cparam->predatfilters[i]->data, cparam);
  811. if(action!=CONTINUE) return action;
  812. }
  813. #endif
  814. return PASS;
  815. }
  816. FILTER_ACTION handledatfltcli(struct clientparam *cparam, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p){
  817. #ifndef STDMAIN
  818. FILTER_ACTION action;
  819. int i;
  820. for(i=0; i<cparam->ndatfilterscli ;i++){
  821. action = (*cparam->datfilterscli[i]->filter->filter_data_cli)(cparam->datfilterscli[i]->data, cparam, buf_p, bufsize_p, offset, length_p);
  822. if(action!=CONTINUE) return action;
  823. }
  824. #endif
  825. return PASS;
  826. }
  827. FILTER_ACTION handledatfltsrv(struct clientparam *cparam, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p){
  828. FILTER_ACTION action;
  829. int i;
  830. for(i=0; i<cparam->ndatfilterssrv; i++){
  831. action = (*cparam->datfilterssrv[i]->filter->filter_data_srv)(cparam->datfilterssrv[i]->data, cparam, buf_p, bufsize_p, offset, length_p);
  832. if(action!=CONTINUE) return action;
  833. }
  834. return PASS;
  835. }