main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright (c) 2004-2009 Sergey Lyubka
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. #if defined(_WIN32)
  21. #define _CRT_SECURE_NO_WARNINGS // Disable deprecation warning in VS2005
  22. #else
  23. #define _XOPEN_SOURCE 600 // For PATH_MAX on linux
  24. #endif
  25. #include <sys/stat.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <signal.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include <limits.h>
  32. #include <stddef.h>
  33. #include "mongoose.h"
  34. #ifdef _WIN32
  35. #include <windows.h>
  36. #include <winsvc.h>
  37. #define PATH_MAX MAX_PATH
  38. #define S_ISDIR(x) ((x) & _S_IFDIR)
  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 += 3) {
  91. fprintf(stderr, " -%s %s (default: \"%s\")\n",
  92. names[i], names[i + 1], names[i + 2] == NULL ? "" : names[i + 2]);
  93. }
  94. fprintf(stderr, "See http://code.google.com/p/mongoose/wiki/MongooseManual"
  95. " for more details.\n");
  96. fprintf(stderr, "Example:\n mongoose -s cert.pem -p 80,443s -d no\n");
  97. exit(EXIT_FAILURE);
  98. }
  99. static void verify_document_root(const char *root) {
  100. const char *p, *path;
  101. char buf[PATH_MAX];
  102. struct stat st;
  103. path = root;
  104. if ((p = strchr(root, ',')) != NULL && (size_t) (p - root) < sizeof(buf)) {
  105. strncpy(buf, root, p - root);
  106. path = buf;
  107. }
  108. if (stat(path, &st) != 0 || !S_ISDIR(st.st_mode)) {
  109. fprintf(stderr, "Invalid root directory: \"%s\"\n", root);
  110. exit(EXIT_FAILURE);
  111. }
  112. }
  113. static char *sdup(const char *str) {
  114. char *p;
  115. if ((p = malloc(strlen(str) + 1)) != NULL) {
  116. strcpy(p, str);
  117. }
  118. return p;
  119. }
  120. static void set_option(char **options, const char *name, const char *value) {
  121. int i;
  122. if (!strcmp(name, "document_root") || !(strcmp(name, "r"))) {
  123. verify_document_root(value);
  124. }
  125. for (i = 0; i < MAX_OPTIONS - 3; i++) {
  126. if (options[i] == NULL) {
  127. options[i] = sdup(name);
  128. options[i + 1] = sdup(value);
  129. options[i + 2] = NULL;
  130. break;
  131. }
  132. }
  133. if (i == MAX_OPTIONS - 3) {
  134. fprintf(stderr, "%s\n", "Too many options specified");
  135. exit(EXIT_FAILURE);
  136. }
  137. }
  138. static void process_command_line_arguments(char *argv[], char **options) {
  139. const char *config_file = NULL;
  140. char line[512], opt[512], val[512], path[PATH_MAX], *p;
  141. FILE *fp = NULL;
  142. struct stat st;
  143. size_t i, line_no = 0;
  144. /* Should we use a config file ? */
  145. if (argv[1] != NULL && argv[2] == NULL) {
  146. config_file = argv[1];
  147. } else if (argv[1] == NULL) {
  148. /* No command line flags specified. Look where binary lives */
  149. if ((p = strrchr(argv[0], DIRSEP)) != 0) {
  150. snprintf(path, sizeof(path), "%.*s%s",
  151. (int) (p - argv[0]) + 1, argv[0], CONFIG_FILE);
  152. }
  153. if (stat(path, &st) == 0) {
  154. config_file = path;
  155. }
  156. }
  157. /* If config file was set in command line and open failed, exit */
  158. if (config_file != NULL && (fp = fopen(config_file, "r")) == NULL) {
  159. fprintf(stderr, "cannot open config file %s: %s\n",
  160. config_file, strerror(errno));
  161. exit(EXIT_FAILURE);
  162. }
  163. if (fp != NULL) {
  164. fprintf(stderr, "Loading config file %s\n", config_file);
  165. /* Loop over the lines in config file */
  166. while (fgets(line, sizeof(line), fp) != NULL) {
  167. line_no++;
  168. /* Ignore empty lines and comments */
  169. if (line[0] == '#' || line[0] == '\n')
  170. continue;
  171. if (sscanf(line, "%s %[^\r\n#]", opt, val) != 2) {
  172. fprintf(stderr, "%s: line %d is invalid\n",
  173. config_file, (int) line_no);
  174. exit(EXIT_FAILURE);
  175. }
  176. set_option(options, opt, val);
  177. }
  178. (void) fclose(fp);
  179. } else {
  180. for (i = 1; argv[i] != NULL; i += 2) {
  181. if (argv[i][0] != '-' || argv[i + 1] == NULL || argv[i + 1][0] == '-') {
  182. show_usage_and_exit();
  183. }
  184. set_option(options, &argv[i][1], argv[i + 1]);
  185. }
  186. }
  187. }
  188. int main(int argc, char *argv[]) {
  189. struct mg_context *ctx;
  190. char *options[MAX_OPTIONS];
  191. int i;
  192. /* Edit passwords file if -A option is specified */
  193. if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'A') {
  194. if (argc != 6) {
  195. show_usage_and_exit();
  196. }
  197. exit(mg_edit_passwords(argv[2], argv[3], argv[4], argv[5]) ?
  198. EXIT_SUCCESS : EXIT_FAILURE);
  199. }
  200. /* Show usage if -h or --help options are specified */
  201. if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
  202. show_usage_and_exit();
  203. }
  204. /* Update config based on command line arguments */
  205. options[0] = NULL;
  206. process_command_line_arguments(argv, options);
  207. /* Setup signal handler: quit on Ctrl-C */
  208. #ifndef _WIN32
  209. signal(SIGCHLD, signal_handler);
  210. #endif /* _WIN32 */
  211. signal(SIGTERM, signal_handler);
  212. signal(SIGINT, signal_handler);
  213. /* Start Mongoose */
  214. ctx = mg_start(NULL, (const char **) options);
  215. for (i = 0; options[i] != NULL; i++) {
  216. free(options[i]);
  217. }
  218. if (ctx == NULL) {
  219. (void) printf("%s\n", "Cannot initialize Mongoose context");
  220. exit(EXIT_FAILURE);
  221. }
  222. printf("Mongoose %s started on port(s) %s with web root [%s]\n",
  223. mg_version(), mg_get_option(ctx, "listening_ports"),
  224. mg_get_option(ctx, "document_root"));
  225. fflush(stdout);
  226. while (exit_flag == 0) {
  227. sleep(1);
  228. }
  229. printf("Exiting on signal %d, waiting for all threads to finish...",
  230. exit_flag);
  231. fflush(stdout);
  232. mg_stop(ctx);
  233. printf("%s", " done.\n");
  234. return EXIT_SUCCESS;
  235. }