httpap.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /**
  2. * httpap.c
  3. * HTTP Application Proxy for Onion Routing
  4. *
  5. * Matej Pfajfar <mp292@cam.ac.uk>
  6. */
  7. /*
  8. * Changes :
  9. * $Log$
  10. * Revision 1.5 2002/07/20 02:01:18 arma
  11. * bugfixes: don't hang waiting for new children to die; accept HTTP/1.1
  12. *
  13. * Revision 1.4 2002/07/19 18:48:19 arma
  14. * slightly less noisy
  15. *
  16. * Revision 1.3 2002/07/12 18:14:16 montrose
  17. * removed loglevel from global namespace. severity level is set using log() with a NULL format argument now. example: log(LOG_ERR,NULL);
  18. *
  19. * Revision 1.2 2002/07/02 09:16:16 arma
  20. * httpap now prepends dest_addr and dest_port strings with their length.
  21. *
  22. * also, it now sets the listening socket option SO_REUSEADDR
  23. *
  24. * Revision 1.1.1.1 2002/06/26 22:45:50 arma
  25. * initial commit: current code
  26. *
  27. * Revision 1.4 2002/06/14 20:45:26 mp292
  28. * Extra debugging message.
  29. *
  30. * Revision 1.3 2002/04/02 14:27:33 badbytes
  31. * Final finishes.
  32. *
  33. * Revision 1.2 2002/03/12 23:40:58 mp292
  34. * Tested.
  35. *
  36. * Revision 1.1 2002/03/11 00:21:53 mp292
  37. * Coding completed. Pending testing.
  38. *
  39. */
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <sys/time.h>
  43. #include <netinet/in.h>
  44. #include <netdb.h>
  45. #include <arpa/inet.h>
  46. #include <errno.h>
  47. #include <ctype.h>
  48. #include <stdio.h>
  49. #include <unistd.h>
  50. #include <signal.h>
  51. #include <wait.h>
  52. #include <stdarg.h>
  53. #include <ctype.h>
  54. #include <stdint.h>
  55. #include <string.h>
  56. #include <stdlib.h>
  57. #include <time.h>
  58. #include "../common/log.h"
  59. #include "../common/config.h"
  60. #include "../common/ss.h"
  61. #include "../common/utils.h"
  62. #include "../common/version.h"
  63. #include "httpap.h"
  64. #include "http.h"
  65. struct timeval conn_tout;
  66. struct timeval *conn_toutp = &conn_tout;
  67. /* valid command-line options */
  68. static const char *args = "hf:p:l:";
  69. /* valid config file options */
  70. static config_opt_t options[] =
  71. {
  72. {"OnionProxy", CONFIG_TYPE_INT, {0}, 0},
  73. {"MaxConn", CONFIG_TYPE_INT, {0}, 0},
  74. {"Anonimize", CONFIG_TYPE_INT, {0}, 0},
  75. {"ConnTimeout", CONFIG_TYPE_INT, {0}, 0},
  76. {0}
  77. };
  78. enum opts {
  79. OnionProxy=0,MaxConn, Anonimize, ConnTimeout
  80. };
  81. /* number of open connections */
  82. int connections=0;
  83. /* prints help on using httpap */
  84. void print_usage()
  85. {
  86. char *program = "httpap";
  87. printf("\n%s - HTTP application proxy for Onion Routing.\nUsage : %s -f config [-p port -l loglevel -h]\n-h : display this help\n-f config : config file\n-p port : port number which %s should bind to\n-l loglevel : logging threshold; one of alert|crit|err|warning|notice|info|debug\n\n", program,program,program);
  88. }
  89. /* used for reaping zombie processes */
  90. void sigchld_handler(int s)
  91. {
  92. while((waitpid (-1, NULL, WNOHANG)) > 0) {
  93. // while (wait(NULL) > 0);
  94. connections--;
  95. }
  96. }
  97. int handle_connection(int new_sock, struct hostent *local, struct sockaddr_in remote, uint16_t op_port)
  98. {
  99. int retval = 0;
  100. int i;
  101. char islocal = 0; /* is the accepted connection local? */
  102. char *cp; /* character pointer used for checking whether the connection is local */
  103. unsigned char *line; /* one line of input */
  104. int len; /* length of the line */
  105. uint16_t stringlen; /* used for sending how long a string is before the actual string */
  106. unsigned char *http_ver; /* HTTP version of the incoming request */
  107. unsigned char *addr; /* destination address */
  108. unsigned char *port; /* destination port */
  109. unsigned char *header_name; /* name of a request header */
  110. uint16_t portn; /* destination port converted into an integer */
  111. char *errtest; /* error check when converting the port into an integer */
  112. ss_t ss; /* standard structure */
  113. unsigned char errcode; /* error code returned by the onion proxy */
  114. int sop; /* socket for connecting to the onion proxy */
  115. struct sockaddr_in op_addr; /* onion proxy address */
  116. /* for use with select() */
  117. fd_set mask,rmask;
  118. int maxfd;
  119. unsigned char buf[1024]; /* data buffer */
  120. log(LOG_DEBUG, "handle_connection() : Local address = %s.", inet_ntoa(*(struct in_addr *)local->h_addr));
  121. log(LOG_DEBUG, "handle_connection() : Remote address = %s.", inet_ntoa(remote.sin_addr));
  122. /* first check that the connection is from the local host, otherwise it will be rejected */
  123. if (*(uint32_t *)&remote.sin_addr == inet_addr("127.0.0.1"))
  124. islocal = 1;
  125. for (i=0; (local->h_addr_list[i] != NULL) && (!islocal); i++)
  126. {
  127. cp = local->h_addr_list[i];
  128. log(LOG_DEBUG,"handle_connection() : Checking if connection is from address %s.",inet_ntoa(*(struct in_addr *)cp));
  129. if (!memcmp(&remote.sin_addr, cp, sizeof(struct in_addr)))
  130. islocal = 1;
  131. }
  132. /* bypass this check for testing purposes */
  133. islocal = 1;
  134. /* reject a non-local connection */
  135. if (!islocal)
  136. {
  137. close(new_sock);
  138. return 0;
  139. }
  140. /* get the request-line */
  141. retval = http_get_line(new_sock, &line, &len, conn_toutp);
  142. if (retval == -1)
  143. {
  144. log(LOG_DEBUG,"handle_connection : Malformed input or connection lost.");
  145. write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
  146. close(new_sock);
  147. return -1;
  148. }
  149. log(LOG_DEBUG,"handle_connection : Received this from client : %s.", line);
  150. /* check the HTTP version */
  151. retval = http_get_version(line, &http_ver);
  152. if (retval == -1)
  153. {
  154. log(LOG_DEBUG,"handle_connection : Unable to extract the HTTP version of the incoming request.");
  155. write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
  156. return -1;
  157. }
  158. log(LOG_DEBUG,"handle_connection : Client's version is : %s.",http_ver);
  159. // if (strcmp(http_ver, HTTPAP_VERSION)) /* not supported */
  160. // {
  161. // log(LOG_DEBUG,"handle_connection : Client's version is %s, I only support HTTP/1.0.",http_ver);
  162. // write_tout(new_sock, HTTPAP_STATUS_LINE_VERSION_NOT_SUPPORTED, strlen(HTTPAP_STATUS_LINE_VERSION_NOT_SUPPORTED), conn_toutp);
  163. // return -1;
  164. // }
  165. free((void *)http_ver);
  166. /* extract the destination address and port */
  167. retval = http_get_dest(line, &addr, &port);
  168. if (retval == -1)
  169. {
  170. log(LOG_DEBUG,"handle_connection : Unable to extract destination address and port number.");
  171. write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
  172. return -1;
  173. }
  174. if (!port) /* no destination port specified, assume the default */
  175. {
  176. port = (unsigned char *)malloc(6);
  177. if (!port)
  178. {
  179. log(LOG_ERR,"Insufficient memory.");
  180. write_tout(new_sock, HTTPAP_STATUS_LINE_UNEXPECTED, strlen(HTTPAP_STATUS_LINE_UNEXPECTED), conn_toutp);
  181. return -1;
  182. }
  183. snprintf(port,6,"%u",htons(HTTPAP_DEFAULT_HTTP_PORT));
  184. }
  185. else
  186. {
  187. log(LOG_DEBUG,"handle_connection() : Destination address is %s.",addr);
  188. log(LOG_DEBUG,"handle_connection() : Destination port is %s.",port);
  189. /* conver the port to an integer */
  190. portn = (uint16_t)strtoul(port,&errtest,0);
  191. if ((*port == '\0') || (*errtest != '\0')) /* port conversion was unsuccessful */
  192. {
  193. log(LOG_DEBUG,"handle_connection : Unable to convert destination port.");
  194. write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
  195. return -1;
  196. }
  197. /* convert to network order and write back to a string */
  198. free((void *)port);
  199. port = (unsigned char *)malloc(6);
  200. if (!port)
  201. {
  202. log(LOG_ERR,"Insufficient memory.");
  203. write_tout(new_sock, HTTPAP_STATUS_LINE_UNEXPECTED, strlen(HTTPAP_STATUS_LINE_UNEXPECTED), conn_toutp);
  204. return -1;
  205. }
  206. snprintf(port,6,"%u",htons(portn));
  207. }
  208. /* create a standard structure */
  209. ss.version = VERSION;
  210. ss.protocol = SS_PROTOCOL_HTTP;
  211. ss.retry_count = 0;
  212. ss.addr_fmt = SS_ADDR_FMT_ASCII_HOST_PORT;
  213. /* open a socket for connecting to the proxy */
  214. sop = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  215. if (sop < 0)
  216. {
  217. log(LOG_DEBUG,"handle_connection() : Error opening socket.");
  218. write_tout(new_sock, HTTPAP_STATUS_LINE_UNEXPECTED, strlen(HTTPAP_STATUS_LINE_UNEXPECTED), conn_toutp);
  219. return -1;
  220. }
  221. log(LOG_DEBUG,"handle_connection() : Socket opened.");
  222. memset((void *)&op_addr,0,sizeof(op_addr)); /* clear the structure first */
  223. /* set up the sockaddr_in structure */
  224. op_addr.sin_family=AF_INET;
  225. op_addr.sin_port=htons(op_port);
  226. memcpy((void *)&op_addr.sin_addr,local->h_addr,local->h_length);
  227. log(LOG_DEBUG,"handle_connection() : Trying to connect to %s at port %u.",inet_ntoa(*((struct in_addr *)local->h_addr)),op_port);
  228. /* try to connect */
  229. retval = connect(sop,(struct sockaddr *)&op_addr,sizeof(op_addr));
  230. if (retval == -1)
  231. {
  232. log(LOG_DEBUG,"handle_connection() : Connection to the onion proxy failed.");
  233. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  234. close(sop);
  235. return -1;
  236. }
  237. /* send the standard structure and the destination address+port */
  238. retval = write_tout(sop,(unsigned char *)&ss, sizeof(ss), conn_toutp);
  239. if (retval < sizeof(ss))
  240. {
  241. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  242. close(sop);
  243. return -1;
  244. }
  245. /* patch so the OP doesn't have to guess how long the string is. Note
  246. * we're *no longer* sending the NULL character. */
  247. stringlen = htons(strlen(addr));
  248. write_tout(sop,(char *)&stringlen,sizeof(uint16_t), conn_toutp);
  249. retval = write_tout(sop,addr,strlen(addr), conn_toutp);
  250. if (retval < strlen(addr))
  251. {
  252. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  253. close(sop);
  254. return -1;
  255. }
  256. /* patch so the OP doesn't have to guess how long the string is. Note
  257. * we're *no longer* sending the NULL character. */
  258. stringlen = htons(strlen(port));
  259. write_tout(sop,(char *)&stringlen,sizeof(short int), conn_toutp);
  260. retval = write_tout(sop,port,strlen(port), conn_toutp);
  261. if (retval < strlen(port))
  262. {
  263. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  264. close(sop);
  265. return -1;
  266. }
  267. /* wait for a return code */
  268. retval = read_tout(sop, &errcode, 1, MSG_WAITALL, conn_toutp);
  269. if (retval < 1)
  270. {
  271. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  272. close(sop);
  273. return -1;
  274. }
  275. if (!errcode) /* onion proxy says OK */
  276. {
  277. /* send the request-line */
  278. retval = write_tout(sop, line, strlen(line), conn_toutp);
  279. if (retval < strlen(line))
  280. {
  281. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  282. close(new_sock);
  283. return -1;
  284. }
  285. free((void *)line);
  286. /* read the request headers (if any) and sanitize if necessary */
  287. while(1)
  288. {
  289. retval = http_get_line(new_sock, &line, &len, conn_toutp);
  290. if (retval == -1)
  291. {
  292. log(LOG_DEBUG,"handle_connection() : Malformed input or connection lost.");
  293. write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
  294. close(new_sock);
  295. return -1;
  296. }
  297. log(LOG_DEBUG,"handle_connection() : Received this from client : %s.", line);
  298. if (len == 2) /* empty line (CRLF only) signifying the end of headers */
  299. {
  300. log(LOG_DEBUG,"handle_connection() : Empty line received.");
  301. retval = write_tout(sop,line,strlen(line),conn_toutp);
  302. if (retval < strlen(line))
  303. {
  304. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  305. close(new_sock);
  306. return -1;
  307. }
  308. free((void *)line);
  309. break;
  310. }
  311. else /* process the header */
  312. {
  313. retval = http_get_header_name(line, &header_name);
  314. if (retval == -1)
  315. {
  316. log(LOG_DEBUG,"handle_connection : Unable to extract header name.");
  317. write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
  318. return -1;
  319. }
  320. log(LOG_DEBUG,"handle_connection : Identified the header as %s.", header_name);
  321. /* discard the Proxy-Connection header */
  322. if (!strcmp(header_name,HTTPAP_HEADER_PROXY_CONNECTION))
  323. free((void *)line);
  324. else if (options[Anonimize].r.i) /* did the user request anonimization? */
  325. {
  326. if (!strcmp(header_name,HTTPAP_HEADER_USER_AGENT))
  327. free((void *)line);
  328. else if (!strcmp(header_name, HTTPAP_HEADER_REFERER))
  329. free((void *)line);
  330. else
  331. {
  332. retval = write_tout(sop, line, strlen(line), conn_toutp);
  333. if (retval < strlen(line))
  334. {
  335. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  336. close(new_sock);
  337. return -1;
  338. }
  339. }
  340. }
  341. else
  342. {
  343. retval = write_tout(sop, line, strlen(line), conn_toutp);
  344. if (retval < strlen(line))
  345. {
  346. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  347. close(new_sock);
  348. return -1;
  349. }
  350. }
  351. free((void *)header_name);
  352. }
  353. }
  354. /* forward data in both directions until one of the principals closes it */
  355. /* set up for select() */
  356. log(LOG_DEBUG,"Header processed, forwarding data in both directions.");
  357. FD_ZERO(&mask);
  358. FD_ZERO(&rmask);
  359. FD_SET(new_sock, &mask);
  360. FD_SET(sop, &mask);
  361. if (sop > new_sock)
  362. maxfd = sop;
  363. else
  364. maxfd = new_sock;
  365. while(1)
  366. {
  367. rmask = mask;
  368. retval = select(maxfd+1,&rmask,NULL,NULL,NULL);
  369. if (retval < 0)
  370. {
  371. log(LOG_DEBUG,"handle_connection() : select() returned a negative integer");
  372. break;
  373. }
  374. if (FD_ISSET(sop,&rmask)) /* data from the onion proxy */
  375. {
  376. retval = read_tout(sop,buf,1024,0,conn_toutp);
  377. if (retval <= 0)
  378. {
  379. log(LOG_DEBUG,"handle_connection : Conection to the onion proxy lost.");
  380. close(sop);
  381. close(new_sock);
  382. break;
  383. }
  384. // log(LOG_DEBUG,"handle_connection() : Received %u bytes from the onion proxy.",retval);
  385. retval = write_tout(new_sock, buf, retval, conn_toutp);
  386. if (retval <= 0)
  387. {
  388. log(LOG_DEBUG, "handle_connection : Connection to the client lost.");
  389. close(sop);
  390. close(new_sock);
  391. break;
  392. }
  393. }
  394. if (FD_ISSET(new_sock, &rmask))
  395. {
  396. retval = read_tout(new_sock,buf,1024,0,conn_toutp);
  397. if (retval <= 0)
  398. {
  399. log(LOG_DEBUG,"handle_connection : Conection to the client lost.");
  400. close(sop);
  401. close(new_sock);
  402. break;
  403. }
  404. log(LOG_DEBUG,"handle_connection() : Received %u bytes from the client.",retval);
  405. retval = write_tout(sop, buf, retval, conn_toutp);
  406. if (retval <= 0)
  407. {
  408. log(LOG_DEBUG, "handle_connection : Connection to the onion proxy lost.");
  409. close(sop);
  410. close(new_sock);
  411. break;
  412. }
  413. }
  414. }
  415. }
  416. else
  417. {
  418. log(LOG_DEBUG,"handle_connection() : Onion proxy returned a non-zero error code (%d)!", errcode);
  419. write_tout(new_sock, HTTPAP_STATUS_LINE_UNEXPECTED, strlen(HTTPAP_STATUS_LINE_UNEXPECTED), conn_toutp);
  420. close(sop);
  421. return -1;
  422. }
  423. return 0;
  424. }
  425. int main(int argc, char *argv[])
  426. {
  427. int loglevel = LOG_DEBUG;
  428. int retval = 0;
  429. char c; /* command-line option */
  430. int one=1;
  431. /* configuration file */
  432. char *conf_filename = NULL;
  433. FILE *cf = NULL;
  434. struct hostent *local_host;
  435. char local_hostname[512];
  436. struct sockaddr_in local, remote; /* local and remote address info */
  437. int request_sock; /* where we listen for connections */
  438. int new_sock; /* for accepted connections */
  439. size_t sin_size; /* for accept() calls */
  440. u_short p; /* http proxy port */
  441. u_short op_port; /* onion proxy port */
  442. /* used for reaping zombie processes */
  443. struct sigaction sa;
  444. char *errtest = NULL; /* for detecting strtoul() errors */
  445. /* set default listening port */
  446. p = htons(HTTPAP_LISTEN_PORT);
  447. /* deal with program arguments */
  448. if ((argc < 2) && (argc > 5)) /* to few or too many arguments*/
  449. {
  450. print_usage();
  451. return -1;
  452. }
  453. opterr = 0;
  454. while ((c = getopt(argc,argv,args)) != -1)
  455. {
  456. switch(c)
  457. {
  458. case 'f': /* config file */
  459. conf_filename = optarg;
  460. break;
  461. case 'p':
  462. p = htons((u_short)strtoul(optarg,&errtest,0));
  463. if (errtest == optarg) /* error */
  464. {
  465. log(LOG_ERR,"Error : -p must be followed by an unsigned positive integer value.");
  466. print_usage();
  467. return -1;
  468. }
  469. break;
  470. case 'h':
  471. print_usage();
  472. return 0;
  473. break;
  474. case 'l':
  475. if (!strcmp(optarg,"emerg"))
  476. loglevel = LOG_EMERG;
  477. else if (!strcmp(optarg,"alert"))
  478. loglevel = LOG_ALERT;
  479. else if (!strcmp(optarg,"crit"))
  480. loglevel = LOG_CRIT;
  481. else if (!strcmp(optarg,"err"))
  482. loglevel = LOG_ERR;
  483. else if (!strcmp(optarg,"warning"))
  484. loglevel = LOG_WARNING;
  485. else if (!strcmp(optarg,"notice"))
  486. loglevel = LOG_NOTICE;
  487. else if (!strcmp(optarg,"info"))
  488. loglevel = LOG_INFO;
  489. else if (!strcmp(optarg,"debug"))
  490. loglevel = LOG_DEBUG;
  491. else
  492. {
  493. log(LOG_ERR,"Error : argument to -l must be one of alert|crit|err|warning|notice|info|debug.");
  494. print_usage();
  495. return -1;
  496. }
  497. break;
  498. case '?':
  499. if (isprint(c))
  500. log(LOG_ERR,"Missing argument or unknown option '-%c'.",optopt);
  501. else
  502. log(LOG_ERR,"Unknown option character 'x%x'.",optopt);
  503. print_usage();
  504. return -1;
  505. break;
  506. default:
  507. abort();
  508. }
  509. }
  510. log(loglevel,NULL); /* assign severity level for logger */
  511. /* the -f option is mandatory */
  512. if (conf_filename == NULL)
  513. {
  514. log(LOG_ERR,"You must specify a config file with the -f option. See help (-h).");
  515. return -1;
  516. }
  517. /* load config file */
  518. cf = open_config(conf_filename);
  519. if (!cf)
  520. {
  521. log(LOG_ERR,"Could not open configuration file %s.",conf_filename);
  522. return -1;
  523. }
  524. retval = parse_config(cf,options);
  525. if (retval)
  526. return -1;
  527. if (options[OnionProxy].err != 1)
  528. {
  529. log(LOG_ERR,"The OnionProxy option is mandatory.");
  530. return -1;
  531. }
  532. if (options[MaxConn].err != 1)
  533. {
  534. log(LOG_ERR,"The MaxConn option is mandatory.");
  535. return -1;
  536. }
  537. if (options[Anonimize].err != 1)
  538. {
  539. log(LOG_ERR,"The Anonimize option is mandatory.");
  540. return -1;
  541. }
  542. else if ((options[Anonimize].r.i != 0) && (options[Anonimize].r.i != 1))
  543. {
  544. log(LOG_ERR,"The Anonimize option takes the values 1 or 0.");
  545. return -1;
  546. }
  547. if (options[ConnTimeout].err != 1)
  548. {
  549. conn_tout.tv_sec = HTTPAP_DEFAULT_CONN_TIMEOUT;
  550. }
  551. else
  552. {
  553. if (!options[ConnTimeout].r.i)
  554. conn_toutp = NULL;
  555. else
  556. conn_tout.tv_sec = options[ConnTimeout].r.i;
  557. }
  558. conn_tout.tv_usec = 0;
  559. op_port = (u_short)options[OnionProxy].r.i;
  560. /* get local address so that we know where to get the onion proxy when we need it */
  561. retval = gethostname(local_hostname, (size_t)512);
  562. if (retval < 0)
  563. {
  564. log(LOG_ERR,"Error getting local hostname");
  565. return -1;
  566. }
  567. local_host = gethostbyname(local_hostname);
  568. if (!local_host)
  569. {
  570. log(LOG_ERR,"Error getting local address.");
  571. return -1;
  572. }
  573. log(LOG_DEBUG,"main() : Got local address : %s.",local_hostname);
  574. /* get the server up and running */
  575. request_sock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  576. if (request_sock < 0)
  577. {
  578. log(LOG_ERR,"Error opening socket.");
  579. return -1;
  580. }
  581. log(LOG_DEBUG,"Socket opened.");
  582. memset((void *)&local,0,sizeof(local)); /* clear the structure first */
  583. /* set up the sockaddr_in structure */
  584. local.sin_family=AF_INET;
  585. local.sin_addr.s_addr = INADDR_ANY;
  586. local.sin_port=p;
  587. setsockopt(request_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  588. /* bind it to the socket */
  589. retval = bind(request_sock,(struct sockaddr *)&local, sizeof(local));
  590. if (retval < 0)
  591. {
  592. log(LOG_ERR,"Error binding socket to local port %d.",ntohs(p));
  593. return retval;
  594. }
  595. log(LOG_DEBUG,"Socket bound to port %d.",ntohs(p));
  596. /* listen for connections */
  597. retval = listen(request_sock,SOMAXCONN);
  598. if (retval < 0)
  599. {
  600. log(LOG_ERR,"Could not listen for connections.");
  601. return retval;
  602. }
  603. log(LOG_DEBUG,"Listening for connections.");
  604. /* server should now be up and running */
  605. /* install the signal handler for making sure zombie processes are killed */
  606. sa.sa_handler = sigchld_handler;
  607. sigemptyset(&sa.sa_mask);
  608. sa.sa_flags = SA_RESTART;
  609. retval = sigaction(SIGCHLD,&sa,NULL);
  610. if (retval < 0)
  611. {
  612. log(LOG_ERR,"Could not install a signal handler.");
  613. return -1;
  614. }
  615. /* main server loop */
  616. /* I use a forking server technique - this isn't the most efficient way to do it,
  617. * but it is simpler. */
  618. while(1)
  619. {
  620. sin_size = sizeof(struct sockaddr_in);
  621. new_sock = accept(request_sock,(struct sockaddr *)&remote,&sin_size);
  622. if (new_sock == -1)
  623. {
  624. if (errno != EINTR)
  625. log(LOG_ERR,"Could not accept socket connection.");
  626. else
  627. log(LOG_DEBUG,"Interrupt received.");
  628. continue;
  629. }
  630. if (connections >= options[MaxConn].r.i)
  631. {
  632. log(LOG_NOTICE,"Number of maximum connections reached. Rejecting incoming request.");
  633. close(new_sock);
  634. continue;
  635. }
  636. log(LOG_DEBUG,"Accepted a connection from %s.",inet_ntoa(remote.sin_addr));
  637. connections++;
  638. if (!fork()) /* this is the child process */
  639. {
  640. close(request_sock); /* the child doesn't need the request socket anymore */
  641. /* Main logic of httpap. */
  642. retval = handle_connection(new_sock, local_host, remote, op_port);
  643. /* End main logic */
  644. exit(retval); /* done, exit */
  645. }
  646. close(new_sock); /* don't need this anymore */
  647. }
  648. return retval;
  649. }