httpap.c 22 KB

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