httpap.c 21 KB

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