httpap.c 21 KB

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