main.c 8.0 KB

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