main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #define MAX_OPTIONS 40
  54. static void signal_handler(int sig_num) {
  55. #if !defined(_WIN32)
  56. if (sig_num == SIGCHLD) {
  57. do {
  58. } while (waitpid(-1, &sig_num, WNOHANG) > 0);
  59. } else
  60. #endif /* !_WIN32 */
  61. {
  62. exit_flag = sig_num;
  63. }
  64. }
  65. /*
  66. * Edit the passwords file.
  67. */
  68. static int mg_edit_passwords(const char *fname, const char *domain,
  69. const char *user, const char *pass) {
  70. struct mg_context *ctx;
  71. const char *options[] = {"authentication_domain", NULL, NULL};
  72. int success;
  73. options[1] = domain;
  74. ctx = mg_start(NULL, options);
  75. success = mg_modify_passwords_file(ctx, fname, user, pass);
  76. mg_stop(ctx);
  77. return success;
  78. }
  79. static void show_usage_and_exit(void) {
  80. const char **names;
  81. int i, len;
  82. fprintf(stderr, "Mongoose version %s (c) Sergey Lyubka\n", mg_version());
  83. fprintf(stderr, "Usage:\n");
  84. fprintf(stderr, " mongoose -A <htpasswd_file> <realm> <user> <passwd>\n");
  85. fprintf(stderr, " mongoose <config_file>\n");
  86. fprintf(stderr, " mongoose [-option value ...]\n");
  87. fprintf(stderr, "OPTIONS:\n ");
  88. names = mg_get_valid_option_names();
  89. len = 2;
  90. for (i = 0; names[i] != NULL; i++) {
  91. len += strlen(names[i]) + 1;
  92. if (len >= 80) {
  93. len = strlen(names[i]) + 3;
  94. fprintf(stderr, "%s", "\n ");
  95. }
  96. fprintf(stderr, "%s%c", names[i], names[i + 1] == NULL ? '\n' : ',');
  97. }
  98. fprintf(stderr, "See http://code.google.com/p/mongoose/wiki/MongooseManual"
  99. " for more details.\n");
  100. fprintf(stderr, "Example:\n mongoose -listening_ports 80,443s "
  101. "-enable_directory_listing no\n");
  102. exit(EXIT_FAILURE);
  103. }
  104. static char *sdup(const char *str) {
  105. char *p;
  106. if ((p = malloc(strlen(str) + 1)) != NULL) {
  107. strcpy(p, str);
  108. }
  109. return p;
  110. }
  111. static void set_option(char **options, const char *name, const char *value) {
  112. int i;
  113. for (i = 0; i < MAX_OPTIONS - 3; i++) {
  114. if (options[i] == NULL) {
  115. options[i] = sdup(name);
  116. options[i + 1] = sdup(value);
  117. options[i + 2] = NULL;
  118. break;
  119. }
  120. }
  121. if (i == MAX_OPTIONS - 3) {
  122. fprintf(stderr, "%s\n", "Too many options specified");
  123. exit(EXIT_FAILURE);
  124. }
  125. }
  126. static void process_command_line_arguments(char *argv[], char **options) {
  127. const char *config_file = CONFIG_FILE;
  128. char line[512], opt[512], *vals[100], val[512], path[FILENAME_MAX], *p;
  129. FILE *fp;
  130. size_t i, line_no = 0;
  131. /* First find out, which config file to open */
  132. for (i = 1; argv[i] != NULL && argv[i][0] == '-'; i += 2)
  133. if (argv[i + 1] == NULL)
  134. show_usage_and_exit();
  135. if (argv[i] != NULL && argv[i + 1] != NULL) {
  136. /* More than one non-option arguments are given */
  137. show_usage_and_exit();
  138. } else if (argv[i] != NULL) {
  139. /* Just one non-option argument is given, this is config file */
  140. config_file = argv[i];
  141. } else {
  142. /* No config file specified. Look for one where binary lives */
  143. if ((p = strrchr(argv[0], DIRSEP)) != 0) {
  144. snprintf(path, sizeof(path), "%.*s%s",
  145. (int) (p - argv[0]) + 1, argv[0], config_file);
  146. config_file = path;
  147. }
  148. }
  149. fp = fopen(config_file, "r");
  150. /* If config file was set in command line and open failed, exit */
  151. if (fp == NULL && argv[i] != NULL) {
  152. fprintf(stderr, "cannot open config file %s: %s\n",
  153. config_file, strerror(errno));
  154. exit(EXIT_FAILURE);
  155. }
  156. /* Reset temporary value holders */
  157. (void) memset(vals, 0, sizeof(vals));
  158. if (fp != NULL) {
  159. printf("Loading config file %s, ignoring command line arguments\n",
  160. config_file);
  161. /* Loop over the lines in config file */
  162. while (fgets(line, sizeof(line), fp) != NULL) {
  163. line_no++;
  164. /* Ignore empty lines and comments */
  165. if (line[0] == '#' || line[0] == '\n')
  166. continue;
  167. if (sscanf(line, "%s %[^\r\n#]", opt, val) != 2) {
  168. fprintf(stderr, "%s: line %d is invalid\n",
  169. config_file, (int) line_no);
  170. exit(EXIT_FAILURE);
  171. }
  172. set_option(options, opt, val);
  173. }
  174. (void) fclose(fp);
  175. } else {
  176. for (i = 1; argv[i] != NULL && argv[i][0] == '-'; i += 2)
  177. set_option(options, &argv[i][1], argv[i + 1]);
  178. }
  179. }
  180. int main(int argc, char *argv[]) {
  181. struct mg_context *ctx;
  182. char *options[MAX_OPTIONS];
  183. int i;
  184. /* Edit passwords file if -A option is specified */
  185. if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'A') {
  186. if (argc != 6) {
  187. show_usage_and_exit();
  188. }
  189. exit(mg_edit_passwords(argv[2], argv[3], argv[4], argv[5]) ?
  190. EXIT_SUCCESS : EXIT_FAILURE);
  191. }
  192. /* Show usage if -h or --help options are specified */
  193. if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
  194. show_usage_and_exit();
  195. }
  196. /* Update config based on command line arguments */
  197. options[0] = NULL;
  198. process_command_line_arguments(argv, options);
  199. /* Setup signal handler: quit on Ctrl-C */
  200. #ifndef _WIN32
  201. signal(SIGCHLD, signal_handler);
  202. #endif /* _WIN32 */
  203. signal(SIGTERM, signal_handler);
  204. signal(SIGINT, signal_handler);
  205. /* Start Mongoose */
  206. ctx = mg_start(NULL, (const char **) options);
  207. for (i = 0; options[i] != NULL; i++) {
  208. free(options[i]);
  209. }
  210. if (ctx == NULL) {
  211. (void) printf("%s\n", "Cannot initialize Mongoose context");
  212. exit(EXIT_FAILURE);
  213. }
  214. printf("Mongoose %s started on port(s) %s with web root [%s]\n",
  215. mg_version(), mg_get_option(ctx, "listening_ports"),
  216. mg_get_option(ctx, "document_root"));
  217. fflush(stdout);
  218. while (exit_flag == 0) {
  219. sleep(1);
  220. }
  221. printf("Exiting on signal %d, waiting for all threads to finish...",
  222. exit_flag);
  223. fflush(stdout);
  224. mg_stop(ctx);
  225. printf("%s", " done.\n");
  226. return EXIT_SUCCESS;
  227. }