axhttpd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Copyright (c) 2007, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <sys/types.h>
  33. #include <signal.h>
  34. #include <stdlib.h>
  35. #include <sys/stat.h>
  36. #include <pwd.h>
  37. #include "axhttp.h"
  38. struct serverstruct *servers;
  39. struct connstruct *usedconns;
  40. struct connstruct *freeconns;
  41. const char * const server_version = "axhttpd/"AXTLS_VERSION;
  42. static void addtoservers(int sd);
  43. static int openlistener(int port);
  44. static void handlenewconnection(int listenfd, int is_ssl);
  45. static void addconnection(int sd, char *ip, int is_ssl);
  46. static void ax_chdir(void);
  47. #if defined(CONFIG_HTTP_HAS_CGI)
  48. struct cgiextstruct *cgiexts;
  49. static void addcgiext(const char *tp);
  50. #if !defined(WIN32)
  51. static void reaper(int sigtype)
  52. {
  53. wait3(NULL, WNOHANG, NULL);
  54. }
  55. #endif
  56. #endif
  57. #ifdef CONFIG_HTTP_VERBOSE /* should really be in debug mode or something */
  58. /* clean up memory for valgrind */
  59. static void sigint_cleanup(int sig)
  60. {
  61. struct serverstruct *sp;
  62. struct connstruct *tp;
  63. while (servers != NULL)
  64. {
  65. if (servers->is_ssl)
  66. ssl_ctx_free(servers->ssl_ctx);
  67. sp = servers->next;
  68. free(servers);
  69. servers = sp;
  70. }
  71. while (freeconns != NULL)
  72. {
  73. tp = freeconns->next;
  74. free(freeconns);
  75. freeconns = tp;
  76. }
  77. while (usedconns != NULL)
  78. {
  79. tp = usedconns->next;
  80. free(usedconns);
  81. usedconns = tp;
  82. }
  83. #if defined(CONFIG_HTTP_HAS_CGI)
  84. while (cgiexts)
  85. {
  86. struct cgiextstruct *cp = cgiexts->next;
  87. if (cp == NULL) /* last entry */
  88. free(cgiexts->ext);
  89. free(cgiexts);
  90. cgiexts = cp;
  91. }
  92. #endif
  93. exit(0);
  94. }
  95. static void die(int sigtype)
  96. {
  97. exit(0);
  98. }
  99. #endif
  100. int main(int argc, char *argv[])
  101. {
  102. fd_set rfds, wfds;
  103. struct connstruct *tp, *to;
  104. struct serverstruct *sp;
  105. int rnum, wnum, active;
  106. int i;
  107. time_t currtime;
  108. #ifdef WIN32
  109. WORD wVersionRequested = MAKEWORD(2, 2);
  110. WSADATA wsaData;
  111. WSAStartup(wVersionRequested,&wsaData);
  112. #else
  113. signal(SIGPIPE, SIG_IGN);
  114. #if defined(CONFIG_HTTP_HAS_CGI)
  115. signal(SIGCHLD, reaper);
  116. #endif
  117. #ifdef CONFIG_HTTP_VERBOSE
  118. signal(SIGQUIT, die);
  119. #endif
  120. #endif
  121. #ifdef CONFIG_HTTP_VERBOSE
  122. signal(SIGTERM, die);
  123. signal(SIGINT, sigint_cleanup);
  124. #endif
  125. tdate_init();
  126. for (i = 0; i < INITIAL_CONNECTION_SLOTS; i++)
  127. {
  128. tp = freeconns;
  129. freeconns = (struct connstruct *)calloc(1, sizeof(struct connstruct));
  130. freeconns->next = tp;
  131. }
  132. if ((active = openlistener(CONFIG_HTTP_PORT)) == -1)
  133. {
  134. #ifdef CONFIG_HTTP_VERBOSE
  135. fprintf(stderr, "ERR: Couldn't bind to port %d\n",
  136. CONFIG_HTTP_PORT);
  137. #endif
  138. exit(1);
  139. }
  140. addtoservers(active);
  141. if ((active = openlistener(CONFIG_HTTP_HTTPS_PORT)) == -1)
  142. {
  143. #ifdef CONFIG_HTTP_VERBOSE
  144. fprintf(stderr, "ERR: Couldn't bind to port %d\n",
  145. CONFIG_HTTP_HTTPS_PORT);
  146. #endif
  147. exit(1);
  148. }
  149. addtoservers(active);
  150. servers->ssl_ctx = ssl_ctx_new(CONFIG_HTTP_DEFAULT_SSL_OPTIONS,
  151. CONFIG_HTTP_SESSION_CACHE_SIZE);
  152. servers->is_ssl = 1;
  153. #if defined(CONFIG_HTTP_HAS_CGI)
  154. addcgiext(CONFIG_HTTP_CGI_EXTENSIONS);
  155. #endif
  156. #if defined(CONFIG_HTTP_VERBOSE)
  157. #if defined(CONFIG_HTTP_HAS_CGI)
  158. printf("addcgiext %s\n", CONFIG_HTTP_CGI_EXTENSIONS);
  159. #endif
  160. printf("%s: listening on ports %d (http) and %d (https)\n",
  161. server_version, CONFIG_HTTP_PORT, CONFIG_HTTP_HTTPS_PORT);
  162. TTY_FLUSH();
  163. #endif
  164. ax_chdir();
  165. #ifdef CONFIG_HTTP_ENABLE_DIFFERENT_USER
  166. {
  167. struct passwd *pd = getpwnam(CONFIG_HTTP_USER);
  168. if (pd != NULL)
  169. {
  170. int res = setuid(pd->pw_uid);
  171. res |= setgid(pd->pw_gid);
  172. #if defined(CONFIG_HTTP_VERBOSE)
  173. if (res == 0)
  174. {
  175. printf("change to '%s' successful\n", CONFIG_HTTP_USER);
  176. TTY_FLUSH();
  177. }
  178. #endif
  179. }
  180. }
  181. #endif
  182. #ifndef WIN32
  183. #ifdef CONFIG_HTTP_IS_DAEMON
  184. if (fork() > 0) /* parent will die */
  185. exit(0);
  186. setsid();
  187. #endif
  188. #endif
  189. /* main loop */
  190. while (1)
  191. {
  192. FD_ZERO(&rfds);
  193. FD_ZERO(&wfds);
  194. rnum = wnum = -1;
  195. sp = servers;
  196. while (sp != NULL) /* read each server port */
  197. {
  198. FD_SET(sp->sd, &rfds);
  199. if (sp->sd > rnum)
  200. rnum = sp->sd;
  201. sp = sp->next;
  202. }
  203. /* Add the established sockets */
  204. tp = usedconns;
  205. currtime = time(NULL);
  206. while (tp != NULL)
  207. {
  208. if (currtime > tp->timeout) /* timed out? Kill it. */
  209. {
  210. to = tp;
  211. tp = tp->next;
  212. removeconnection(to);
  213. continue;
  214. }
  215. if (tp->state == STATE_WANT_TO_READ_HEAD)
  216. {
  217. FD_SET(tp->networkdesc, &rfds);
  218. if (tp->networkdesc > rnum)
  219. rnum = tp->networkdesc;
  220. }
  221. if (tp->state == STATE_WANT_TO_SEND_HEAD)
  222. {
  223. FD_SET(tp->networkdesc, &wfds);
  224. if (tp->networkdesc > wnum)
  225. wnum = tp->networkdesc;
  226. }
  227. if (tp->state == STATE_WANT_TO_READ_FILE)
  228. {
  229. FD_SET(tp->filedesc, &rfds);
  230. if (tp->filedesc > rnum)
  231. rnum = tp->filedesc;
  232. }
  233. if (tp->state == STATE_WANT_TO_SEND_FILE)
  234. {
  235. FD_SET(tp->networkdesc, &wfds);
  236. if (tp->networkdesc > wnum)
  237. wnum = tp->networkdesc;
  238. }
  239. #if defined(CONFIG_HTTP_DIRECTORIES)
  240. if (tp->state == STATE_DOING_DIR)
  241. {
  242. FD_SET(tp->networkdesc, &wfds);
  243. if (tp->networkdesc > wnum)
  244. wnum = tp->networkdesc;
  245. }
  246. #endif
  247. tp = tp->next;
  248. }
  249. active = select(wnum > rnum ? wnum+1 : rnum+1,
  250. rnum != -1 ? &rfds : NULL,
  251. wnum != -1 ? &wfds : NULL,
  252. NULL, NULL);
  253. /* New connection? */
  254. sp = servers;
  255. while (active > 0 && sp != NULL)
  256. {
  257. if (FD_ISSET(sp->sd, &rfds))
  258. {
  259. handlenewconnection(sp->sd, sp->is_ssl);
  260. active--;
  261. }
  262. sp = sp->next;
  263. }
  264. /* Handle the established sockets */
  265. tp = usedconns;
  266. while (active > 0 && tp != NULL)
  267. {
  268. to = tp;
  269. tp = tp->next;
  270. if (to->state == STATE_WANT_TO_READ_HEAD &&
  271. FD_ISSET(to->networkdesc, &rfds))
  272. {
  273. active--;
  274. #if defined(CONFIG_HTTP_HAS_CGI)
  275. if (to->post_state)
  276. read_post_data(to);
  277. else
  278. #endif
  279. procreadhead(to);
  280. }
  281. if (to->state == STATE_WANT_TO_SEND_HEAD &&
  282. FD_ISSET(to->networkdesc, &wfds))
  283. {
  284. active--;
  285. procsendhead(to);
  286. }
  287. if (to->state == STATE_WANT_TO_READ_FILE &&
  288. FD_ISSET(to->filedesc, &rfds))
  289. {
  290. active--;
  291. procreadfile(to);
  292. }
  293. if (to->state == STATE_WANT_TO_SEND_FILE &&
  294. FD_ISSET(to->networkdesc, &wfds))
  295. {
  296. active--;
  297. procsendfile(to);
  298. }
  299. #if defined(CONFIG_HTTP_DIRECTORIES)
  300. if (to->state == STATE_DOING_DIR &&
  301. FD_ISSET(to->networkdesc, &wfds))
  302. {
  303. active--;
  304. procdodir(to);
  305. }
  306. #endif
  307. }
  308. }
  309. return 0;
  310. }
  311. #if defined(CONFIG_HTTP_HAS_CGI)
  312. static void addcgiext(const char *cgi_exts)
  313. {
  314. char *cp = strdup(cgi_exts);
  315. /* extenstions are comma separated */
  316. do
  317. {
  318. struct cgiextstruct *ex = (struct cgiextstruct *)
  319. malloc(sizeof(struct cgiextstruct));
  320. ex->ext = cp;
  321. ex->next = cgiexts;
  322. cgiexts = ex;
  323. if ((cp = strchr(cp, ',')) != NULL)
  324. *cp++ = 0;
  325. } while (cp != NULL);
  326. }
  327. #endif
  328. static void addtoservers(int sd)
  329. {
  330. struct serverstruct *tp = (struct serverstruct *)
  331. calloc(1, sizeof(struct serverstruct));
  332. tp->next = servers;
  333. tp->sd = sd;
  334. servers = tp;
  335. }
  336. #ifdef HAVE_IPV6
  337. static void handlenewconnection(int listenfd, int is_ssl)
  338. {
  339. struct sockaddr_in6 their_addr;
  340. int tp = sizeof(their_addr);
  341. char ipbuf[100];
  342. int connfd = accept(listenfd, (struct sockaddr *)&their_addr, &tp);
  343. if (tp == sizeof(struct sockaddr_in6))
  344. inet_ntop(AF_INET6, &their_addr.sin6_addr, ipbuf, sizeof(ipbuf));
  345. else if (tp == sizeof(struct sockaddr_in))
  346. inet_ntop(AF_INET, &(((struct sockaddr_in *)&their_addr)->sin_addr),
  347. ipbuf, sizeof(ipbuf));
  348. else
  349. *ipbuf = '\0';
  350. addconnection(connfd, ipbuf, is_ssl);
  351. }
  352. #else
  353. static void handlenewconnection(int listenfd, int is_ssl)
  354. {
  355. struct sockaddr_in their_addr;
  356. socklen_t tp = sizeof(struct sockaddr_in);
  357. int connfd = accept(listenfd, (struct sockaddr *)&their_addr, &tp);
  358. addconnection(connfd, inet_ntoa(their_addr.sin_addr), is_ssl);
  359. }
  360. #endif
  361. static int openlistener(int port)
  362. {
  363. int sd;
  364. #ifdef WIN32
  365. char tp = 1;
  366. #else
  367. int tp = 1;
  368. #endif
  369. #ifndef HAVE_IPV6
  370. struct sockaddr_in my_addr;
  371. if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  372. return -1;
  373. memset(&my_addr, 0, sizeof(my_addr));
  374. my_addr.sin_family = AF_INET;
  375. my_addr.sin_port = htons((short)port);
  376. my_addr.sin_addr.s_addr = INADDR_ANY;
  377. #else
  378. struct sockaddr_in6 my_addr;
  379. if ((sd = socket(AF_INET6, SOCK_STREAM, 0)) == -1)
  380. return -1;
  381. memset(&my_addr, 0, sizeof(my_addr));
  382. my_addr.sin6_family = AF_INET6;
  383. my_addr.sin6_port = htons(port);
  384. my_addr.sin6_addr.s_addr = INADDR_ANY;
  385. #endif
  386. setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &tp, sizeof(tp));
  387. if (bind(sd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
  388. {
  389. close(sd);
  390. return -1;
  391. }
  392. listen(sd, BACKLOG);
  393. return sd;
  394. }
  395. /* Wrapper function for strncpy() that guarantees
  396. a null-terminated string. This is to avoid any possible
  397. issues due to strncpy()'s behaviour.
  398. */
  399. char *my_strncpy(char *dest, const char *src, size_t n)
  400. {
  401. strncpy(dest, src, n);
  402. dest[n-1] = '\0';
  403. return dest;
  404. }
  405. int isdir(const char *tpbuf)
  406. {
  407. struct stat st;
  408. char path[MAXREQUESTLENGTH];
  409. strcpy(path, tpbuf);
  410. #ifdef WIN32 /* win32 stat() can't handle trailing '\' */
  411. if (path[strlen(path)-1] == '\\')
  412. path[strlen(path)-1] = 0;
  413. #endif
  414. if (stat(path, &st) == -1)
  415. return 0;
  416. if ((st.st_mode & S_IFMT) == S_IFDIR)
  417. return 1;
  418. return 0;
  419. }
  420. static void addconnection(int sd, char *ip, int is_ssl)
  421. {
  422. struct connstruct *tp;
  423. /* Get ourselves a connstruct */
  424. if (freeconns == NULL)
  425. tp = (struct connstruct *)calloc(1, sizeof(struct connstruct));
  426. else
  427. {
  428. tp = freeconns;
  429. freeconns = tp->next;
  430. }
  431. /* Attach it to the used list */
  432. tp->next = usedconns;
  433. usedconns = tp;
  434. tp->networkdesc = sd;
  435. if (is_ssl)
  436. tp->ssl = ssl_server_new(servers->ssl_ctx, sd);
  437. tp->is_ssl = is_ssl;
  438. tp->filedesc = -1;
  439. #if defined(CONFIG_HTTP_HAS_DIRECTORIES)
  440. tp->dirp = NULL;
  441. #endif
  442. *tp->actualfile = '\0';
  443. *tp->filereq = '\0';
  444. tp->state = STATE_WANT_TO_READ_HEAD;
  445. tp->reqtype = TYPE_GET;
  446. tp->close_when_done = 0;
  447. tp->timeout = time(NULL) + CONFIG_HTTP_TIMEOUT;
  448. #if defined(CONFIG_HTTP_HAS_CGI)
  449. strcpy(tp->remote_addr, ip);
  450. #endif
  451. }
  452. void removeconnection(struct connstruct *cn)
  453. {
  454. struct connstruct *tp;
  455. int shouldret = 0;
  456. tp = usedconns;
  457. if (tp == NULL || cn == NULL)
  458. shouldret = 1;
  459. else if (tp == cn)
  460. usedconns = tp->next;
  461. else
  462. {
  463. while (tp != NULL)
  464. {
  465. if (tp->next == cn)
  466. {
  467. tp->next = (tp->next)->next;
  468. shouldret = 0;
  469. break;
  470. }
  471. tp = tp->next;
  472. shouldret = 1;
  473. }
  474. }
  475. if (shouldret)
  476. return;
  477. /* If we did, add it to the free list */
  478. cn->next = freeconns;
  479. freeconns = cn;
  480. /* Close it all down */
  481. if (cn->networkdesc != -1)
  482. {
  483. if (cn->is_ssl)
  484. {
  485. ssl_free(cn->ssl);
  486. cn->ssl = NULL;
  487. }
  488. SOCKET_CLOSE(cn->networkdesc);
  489. }
  490. if (cn->filedesc != -1)
  491. close(cn->filedesc);
  492. #if defined(CONFIG_HTTP_HAS_DIRECTORIES)
  493. if (cn->dirp != NULL)
  494. #ifdef WIN32
  495. FindClose(cn->dirp);
  496. #else
  497. closedir(cn->dirp);
  498. #endif
  499. #endif
  500. }
  501. /*
  502. * Change directories one way or the other.
  503. */
  504. static void ax_chdir(void)
  505. {
  506. static char *webroot = CONFIG_HTTP_WEBROOT;
  507. if (chdir(webroot))
  508. {
  509. #ifdef CONFIG_HTTP_VERBOSE
  510. fprintf(stderr, "'%s' is not a directory\n", webroot);
  511. #endif
  512. exit(1);
  513. }
  514. }