main.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) 2004-2009 Sergey Lyubka
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. *
  22. * $Id: main.c 518 2010-05-03 12:55:35Z valenok $
  23. */
  24. #if defined(_WIN32)
  25. #define _CRT_SECURE_NO_WARNINGS /* Disable deprecation warning in VS2005 */
  26. #endif /* _WIN32 */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <signal.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <limits.h>
  33. #include <stddef.h>
  34. #include "mongoose.h"
  35. #ifdef _WIN32
  36. #include <windows.h>
  37. #include <winsvc.h>
  38. #define DIRSEP '\\'
  39. #define snprintf _snprintf
  40. #if !defined(__LCC__)
  41. #define strdup(x) _strdup(x)
  42. #endif /* !MINGW */
  43. #define sleep(x) Sleep((x) * 1000)
  44. #else
  45. #include <sys/wait.h>
  46. #include <unistd.h> /* For pause() */
  47. #define DIRSEP '/'
  48. #endif /* _WIN32 */
  49. static int exit_flag; /* Program termination flag */
  50. #if !defined(CONFIG_FILE)
  51. #define CONFIG_FILE "mongoose.conf"
  52. #endif /* !CONFIG_FILE */
  53. static void
  54. signal_handler(int sig_num)
  55. {
  56. #if !defined(_WIN32)
  57. if (sig_num == SIGCHLD) {
  58. do {
  59. } while (waitpid(-1, &sig_num, WNOHANG) > 0);
  60. } else
  61. #endif /* !_WIN32 */
  62. {
  63. exit_flag = sig_num;
  64. }
  65. }
  66. /*
  67. * Edit the passwords file.
  68. */
  69. static int
  70. mg_edit_passwords(const char *fname, const char *domain,
  71. const char *user, const char *pass)
  72. {
  73. struct mg_context *ctx;
  74. struct mg_config config;
  75. int retval;
  76. memset(&config, 0, sizeof(config));
  77. config.auth_domain = (char *) domain;
  78. config.num_threads = "0";
  79. config.listening_ports = "";
  80. ctx = mg_start(&config);
  81. retval = mg_modify_passwords_file(ctx, fname, user, pass);
  82. mg_stop(ctx);
  83. return (retval);
  84. }
  85. #define OFFSET(x) offsetof(struct mg_config, x)
  86. static struct option_descriptor {
  87. const char *name;
  88. const char *description;
  89. size_t offset;
  90. } known_options[] = {
  91. {"root", "\tWeb root directory", OFFSET(document_root)},
  92. {"index_files", "Index files", OFFSET(index_files)},
  93. {"ssl_cert", "SSL certificate file", OFFSET(ssl_certificate)},
  94. {"ports", "Listening ports", OFFSET(listening_ports)},
  95. {"dir_list", "Directory listing", OFFSET(enable_directory_listing)},
  96. {"protect", "URI to htpasswd mapping", OFFSET(protect)},
  97. {"cgi_ext", "CGI extensions", OFFSET(cgi_extensions)},
  98. {"cgi_interp", "CGI interpreter to use", OFFSET(cgi_interpreter)},
  99. {"cgi_env", "Custom CGI enviroment variables", OFFSET(cgi_environment)},
  100. {"ssi_ext", "SSI extensions", OFFSET(ssi_extensions)},
  101. {"auth_realm", "Authentication domain name", OFFSET(auth_domain)},
  102. {"auth_gpass", "Global passwords file", OFFSET(global_passwords_file)},
  103. {"auth_PUT", "PUT,DELETE auth file", OFFSET(put_delete_passwords_file)},
  104. {"uid", "\tRun as user", OFFSET(uid)},
  105. {"access_log", "Access log file", OFFSET(access_log_file)},
  106. {"error_log", "Error log file", OFFSET(error_log_file)},
  107. {"acl", "\tAllow/deny IP addresses/subnets", OFFSET(acl)},
  108. {"num_threads", "Threads to spawn", OFFSET(num_threads)},
  109. {"mime_types", "Extra mime types to use", OFFSET(mime_types)},
  110. {NULL, NULL, 0}
  111. };
  112. static void
  113. show_usage_and_exit(const struct mg_config *config)
  114. {
  115. const struct option_descriptor *o;
  116. const char *value;
  117. (void) fprintf(stderr,
  118. "Mongoose version %s (c) Sergey Lyubka\n"
  119. "usage: mongoose [options] [config_file]\n", mg_version());
  120. fprintf(stderr, " -A <htpasswd_file> <realm> <user> <passwd>\n");
  121. for (o = known_options; o->name != NULL; o++) {
  122. (void) fprintf(stderr, " -%s\t%s", o->name, o->description);
  123. value = * (char **) ((char *) config + o->offset);
  124. if (value != NULL)
  125. fprintf(stderr, " (default: \"%s\")", value);
  126. fputc('\n', stderr);
  127. }
  128. exit(EXIT_FAILURE);
  129. }
  130. static void
  131. set_option(struct mg_config *config, const char *name, char *value)
  132. {
  133. const struct option_descriptor *o;
  134. for (o = known_options; o->name != NULL; o++)
  135. if (strcmp(name, o->name) == 0) {
  136. * (char **) ((char *) config + o->offset) = value;
  137. break;
  138. }
  139. if (o->name == NULL)
  140. show_usage_and_exit(config);
  141. }
  142. static void
  143. process_command_line_arguments(struct mg_config *config, char *argv[])
  144. {
  145. const char *config_file = CONFIG_FILE;
  146. char line[512], opt[512], *vals[100],
  147. val[512], path[FILENAME_MAX], *p;
  148. FILE *fp;
  149. size_t i, line_no = 0;
  150. /* First find out, which config file to open */
  151. for (i = 1; argv[i] != NULL && argv[i][0] == '-'; i += 2)
  152. if (argv[i + 1] == NULL)
  153. show_usage_and_exit(config);
  154. if (argv[i] != NULL && argv[i + 1] != NULL) {
  155. /* More than one non-option arguments are given */
  156. show_usage_and_exit(config);
  157. } else if (argv[i] != NULL) {
  158. /* Just one non-option argument is given, this is config file */
  159. config_file = argv[i];
  160. } else {
  161. /* No config file specified. Look for one where binary lives */
  162. if ((p = strrchr(argv[0], DIRSEP)) != 0) {
  163. (void) snprintf(path, sizeof(path), "%.*s%s",
  164. (int) (p - argv[0]) + 1, argv[0], config_file);
  165. config_file = path;
  166. }
  167. }
  168. fp = fopen(config_file, "r");
  169. /* If config file was set in command line and open failed, exit */
  170. if (fp == NULL && argv[i] != NULL) {
  171. (void) fprintf(stderr, "cannot open config file %s: %s\n",
  172. config_file, strerror(errno));
  173. exit(EXIT_FAILURE);
  174. }
  175. /* Reset temporary value holders */
  176. (void) memset(vals, 0, sizeof(vals));
  177. if (fp != NULL) {
  178. (void) printf("Loading config file %s, "
  179. "ignoring command line arguments\n", config_file);
  180. /* Loop over the lines in config file */
  181. while (fgets(line, sizeof(line), fp) != NULL) {
  182. line_no++;
  183. /* Ignore empty lines and comments */
  184. if (line[0] == '#' || line[0] == '\n')
  185. continue;
  186. if (sscanf(line, "%s %[^\r\n#]", opt, val) != 2) {
  187. fprintf(stderr, "%s: line %d is invalid\n",
  188. config_file, (int) line_no);
  189. exit(EXIT_FAILURE);
  190. }
  191. /* TODO(lsm): free this at some point */
  192. p = malloc(strlen(val) + 1);
  193. (void) strcpy(p, val);
  194. set_option(config, opt, p);
  195. }
  196. (void) fclose(fp);
  197. } else {
  198. for (i = 1; argv[i] != NULL && argv[i][0] == '-'; i += 2)
  199. set_option(config, &argv[i][1], argv[i + 1]);
  200. }
  201. }
  202. int
  203. main(int argc, char *argv[])
  204. {
  205. struct mg_config config;
  206. struct mg_context *ctx;
  207. /* Initialize configuration with default values */
  208. (void) memset(&config, 0, sizeof(config));
  209. config.document_root = ".";
  210. config.enable_directory_listing = "yes";
  211. config.auth_domain = "mydomain.com";
  212. config.num_threads = "20";
  213. config.index_files = "index.html,index.htm,index.cgi";
  214. config.cgi_extensions = ".cgi,.pl,.php";
  215. config.ssi_extensions = ".shtml,.shtm";
  216. config.listening_ports = "8080";
  217. /* Edit passwords file if -A option is specified */
  218. if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'A') {
  219. if (argc != 6)
  220. show_usage_and_exit(&config);
  221. exit(mg_edit_passwords(argv[2], argv[3], argv[4], argv[5]) ==
  222. MG_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE);
  223. }
  224. /* Show usage if -h or --help options are specified */
  225. if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")))
  226. show_usage_and_exit(&config);
  227. /* Update config based on command line arguments */
  228. process_command_line_arguments(&config, argv);
  229. /* Setup signal handler: quit on Ctrl-C */
  230. #ifndef _WIN32
  231. (void) signal(SIGCHLD, signal_handler);
  232. #endif /* _WIN32 */
  233. (void) signal(SIGTERM, signal_handler);
  234. (void) signal(SIGINT, signal_handler);
  235. /* Start Mongoose */
  236. if ((ctx = mg_start(&config)) == NULL) {
  237. (void) printf("%s\n", "Cannot initialize Mongoose context");
  238. exit(EXIT_FAILURE);
  239. }
  240. (void) printf("Mongoose %s started on port(s) %s "
  241. "with web root [%s]\n",
  242. mg_version(), config.listening_ports, config.document_root);
  243. fflush(stdout);
  244. while (exit_flag == 0)
  245. sleep(1);
  246. (void) printf("Exiting on signal %d, "
  247. "waiting for all threads to finish...", exit_flag);
  248. fflush(stdout);
  249. mg_stop(ctx);
  250. (void) printf("%s", " done.\n");
  251. return (EXIT_SUCCESS);
  252. }