Kagera uHTTP Daemon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kuhttpd.h 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * @PROJECT Kagera uHTTP Daemon
  3. * @COPYRIGHT See COPYING in the top level directory
  4. * @FILE kuhttpd.h
  5. * @PURPOSE HTTP Server
  6. * @DEVELOPERS Rafal Kupiec <belliash@asiotec.eu.org>
  7. * Jef Poskanzer <jef@acme.com>
  8. */
  9. #ifndef __KUHTTPD_H
  10. #define __KUHTTPD_H
  11. #define STDIN_FILENO 0
  12. #define STDOUT_FILENO 1
  13. #define STDERR_FILENO 2
  14. #define SIZE_T_MAX 2147483647L
  15. #define ERR_DIR "errors"
  16. #define DEFAULT_HTTP_PORT 80
  17. #define DEFAULT_HTTPS_PORT 443
  18. #define DEFAULT_CERTFILE "kuhttpd.pem"
  19. #define DEFAULT_CHARSET "UTF-8"
  20. #define DEFAULT_LANGUAGE "english"
  21. #define DEFAULT_CGI_PATTERN "cgi-bin/**|**.cgi|**.sh"
  22. #define DEFAULT_USER "nobody"
  23. #define CGI_NICE 10
  24. #define CGI_PATH "/usr/local/bin:/usr/ucb:/bin:/usr/bin:/usr/local/sbin:/sbin:/usr/sbin"
  25. #define CGI_LD_LIBRARY_PATH "/usr/local/lib:/usr/lib:/lib"
  26. #define AUTH_FILE ".htpasswd"
  27. #define READ_TIMEOUT 60
  28. #define WRITE_TIMEOUT 300
  29. #define METHOD_UNKNOWN 0
  30. #define METHOD_GET 1
  31. #define METHOD_HEAD 2
  32. #define METHOD_POST 3
  33. #define SERVER_SOFTWARE "Kagera uHTTP Daemon 1.4"
  34. struct mime_entry {
  35. char* ext;
  36. size_t ext_len;
  37. char* val;
  38. size_t val_len;
  39. };
  40. typedef union {
  41. struct sockaddr sa;
  42. struct sockaddr_in sa_in;
  43. struct sockaddr_in6 sa_in6;
  44. struct sockaddr_storage sa_stor;
  45. } usockaddr;
  46. static int b64_decode_table[256] = {
  47. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  48. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  49. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
  50. 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,
  51. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
  52. 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
  53. -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
  54. 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,
  55. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  56. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  57. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  58. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  59. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  60. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  61. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  62. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
  63. };
  64. static unsigned char allowDirectoryListing;
  65. static char* argv0;
  66. static char* data_dir;
  67. static char* certfile;
  68. static char* cgi_pattern;
  69. static char* charset;
  70. static char* language;
  71. static char* cipher;
  72. static char cwd[MAXPATHLEN];
  73. static int debug;
  74. static char* defaultPageFile;
  75. static char* defaultRealmName;
  76. static char* defaultRealmPasswordFile;
  77. static char* dir;
  78. static int do_ssl;
  79. static int got_hup;
  80. static char* hostname;
  81. static char hostname_buf[500];
  82. static int listen4_fd, listen6_fd;
  83. static int listen4s_fd, listen6s_fd;
  84. static char* local_pattern;
  85. static char* logfile;
  86. static FILE* logfp;
  87. static int max_age;
  88. static int no_empty_referers;
  89. static char* p3p;
  90. static char* pageNotFoundFile;
  91. static char* pidfile;
  92. static unsigned short port;
  93. static SSL_CTX* ssl_ctx;
  94. static unsigned short sslPort;
  95. static char* url_pattern;
  96. static char* user;
  97. static off_t bytes;
  98. static usockaddr client_addr;
  99. static int conn_fd;
  100. static char* file;
  101. static int method;
  102. static char* path;
  103. static char* pathinfo;
  104. static char* protocol;
  105. static char* query;
  106. static char* request;
  107. static size_t request_size, request_len, request_idx;
  108. struct stat sb;
  109. static SSL* ssl;
  110. static int status;
  111. static char* authorization;
  112. static size_t content_length;
  113. static char* content_type;
  114. static char* cookie;
  115. static char* host;
  116. static time_t if_modified_since;
  117. static char* referer;
  118. static char* remoteuser;
  119. static char* response;
  120. static size_t response_size, response_len;
  121. static char* useragent;
  122. static void add_headers(int s, char* title, char* extra_header, char* me, char* mt, off_t b, time_t mod);
  123. static void add_to_buf(char** bufP, size_t* bufsizeP, size_t* buflenP, char* str, size_t len);
  124. static void add_to_request(char* str, size_t len);
  125. static void add_to_response(char* str, size_t len);
  126. static void auth_check(char* dirname, int is_ssl);
  127. static int b64_decode(const char* str, unsigned char* space, int size);
  128. static char* build_env(char* fmt, char* arg);
  129. static void cgi_interpose_input(int wfd, int is_ssl);
  130. static void cgi_interpose_output(int rfd, int parse_headers, int is_ssl);
  131. static void check_referer(int is_ssl);
  132. static void clear_ndelay(int fd);
  133. extern char* crypt(const char* key, const char* setting);
  134. static void de_dotdot(char* file);
  135. static void do_cgi(int is_ssl, unsigned short port);
  136. static void do_dir(int is_ssl);
  137. static void do_file(int is_ssl, unsigned short conn_port);
  138. static void* e_malloc(size_t size);
  139. static void* e_realloc(void* optr, size_t size);
  140. static char* e_strdup(char* ostr);
  141. static const char* figure_mime(char* name, char* me, size_t me_size);
  142. static char* file_details(const char* dir, const char* name);
  143. static char* get_method_str(int m);
  144. static int get_pathinfo(void);
  145. static char* get_request_line(void);
  146. static void handle_read_timeout(int sig, int is_ssl);
  147. static void handle_read_timeout_sig(int sig);
  148. static void handle_request(int is_ssl, unsigned short conn_port);
  149. static void handle_sigchld(int sig);
  150. static void handle_sighup(int sig);
  151. static void handle_sigterm(int sig);
  152. static void handle_write_timeout(int sig);
  153. static int hexit(char c);
  154. static void init_mime(void);
  155. static int initialize_listen_socket(usockaddr* usaP);
  156. static void lookup_hostname(usockaddr* usa4P, usockaddr* usa4sP, size_t sa4_len, int* gotv4P, int* gotv4sP, usockaddr* usa6P, usockaddr* usa6sP, size_t sa6_len, int* gotv6P, int* gotv6sP);
  157. static char** make_argp(void);
  158. static char** make_envp(int is_sl, unsigned short port);
  159. static void make_log_entry(void);
  160. static char* ntoa(usockaddr* usaP);
  161. static ssize_t my_read(char* buf, size_t size, int is_ssl);
  162. static ssize_t my_write(char* buf, size_t size, int is_ssl);
  163. static void no_value_required(char* name, char* value);
  164. static void post_post_garbage_hack(int is_ssl);
  165. static void read_config(char* filename);
  166. static int really_check_referer(void);
  167. static void reopen_logfile(void);
  168. static void send_authenticate(char* realm, int is_ssl);
  169. static void send_error(int s, char* title, char* extra_header, char* text, int is_ssl);
  170. static void send_error_body(int s, char* title, char* text);
  171. static int send_error_file(char* filename);
  172. static void send_error_tail(void);
  173. static void send_redirect(char* extra_header, char* hostname, char* new_location, int is_ssl);
  174. static void send_response(int is_ssl);
  175. static void send_via_write(int fd, off_t size, int is_ssl);
  176. static void set_ndelay(int fd);
  177. static int sockaddr_check(usockaddr* usaP);
  178. static size_t sockaddr_len(usockaddr* usaP);
  179. static void start_request(void);
  180. static void start_response(void);
  181. static void strdecode(char* to, char* from);
  182. static void strencode(char* to, size_t tosize, const char* from);
  183. static void usage(void);
  184. static void value_required(char* name, char* value);
  185. #endif