main.c 7.9 KB

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