web_server.c
Go to the documentation of this file.
00001 // Copyright (c) 2004-2013 Sergey Lyubka
00002 // Copyright (c) 2013-2014 Cesanta Software Limited
00003 
00004 #undef UNICODE                    // Use ANSI WinAPI functions
00005 #undef _UNICODE                   // Use multibyte encoding on Windows
00006 #define _MBCS                     // Use multibyte encoding on Windows
00007 #define _WIN32_WINNT 0x500        // Enable MIIM_BITMAP
00008 #define _CRT_SECURE_NO_WARNINGS   // Disable deprecation warning in VS2005
00009 #define _XOPEN_SOURCE 600         // For PATH_MAX on linux
00010 #undef WIN32_LEAN_AND_MEAN        // Let windows.h always include winsock2.h
00011 
00012 #include <sys/stat.h>
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <signal.h>
00016 #include <string.h>
00017 #include <errno.h>
00018 #include <limits.h>
00019 #include <stddef.h>
00020 #include <stdarg.h>
00021 #include <ctype.h>
00022 #include <time.h>
00023 
00024 #include "mongoose.h"
00025 
00026 #ifdef _WIN32
00027 #include <windows.h>
00028 #include <direct.h>  // For chdir()
00029 #include <winsvc.h>
00030 #include <shlobj.h>
00031 
00032 #ifndef PATH_MAX
00033 #define PATH_MAX MAX_PATH
00034 #endif
00035 
00036 #ifndef S_ISDIR
00037 #define S_ISDIR(x) ((x) & _S_IFDIR)
00038 #endif
00039 
00040 #define DIRSEP '\\'
00041 #define snprintf _snprintf
00042 #define vsnprintf _vsnprintf
00043 #define sleep(x) Sleep((x) * 1000)
00044 #define abs_path(rel, abs, abs_size) _fullpath((abs), (rel), (abs_size))
00045 #define SIGCHLD 0
00046 typedef struct _stat file_stat_t;
00047 #define stat(x, y) _stat((x), (y))
00048 #else
00049 typedef struct stat file_stat_t;
00050 #include <sys/wait.h>
00051 #include <unistd.h>
00052 
00053 #ifdef IOS
00054 #include <ifaddrs.h>
00055 #endif
00056 
00057 #define DIRSEP '/'
00058 #define __cdecl
00059 #define abs_path(rel, abs, abs_size) realpath((rel), (abs))
00060 #endif // _WIN32
00061 
00062 #define MAX_OPTIONS 100
00063 #define MAX_CONF_FILE_LINE_SIZE (8 * 1024)
00064 
00065 #ifndef MVER
00066 #define MVER MONGOOSE_VERSION
00067 #endif
00068 
00069 static int exit_flag;
00070 static char server_name[50];        // Set by init_server_name()
00071 static char s_config_file[PATH_MAX];  // Set by process_command_line_arguments
00072 static struct mg_server *server;    // Set by start_mongoose()
00073 static const char *s_default_document_root = ".";
00074 static const char *s_default_listening_port = "8080";
00075 static char **s_argv = { NULL };
00076 
00077 static void set_options(char *argv[]);
00078 
00079 #if !defined(CONFIG_FILE)
00080 #define CONFIG_FILE "mongoose.conf"
00081 #endif /* !CONFIG_FILE */
00082 
00083 static void __cdecl signal_handler(int sig_num) {
00084   // Reinstantiate signal handler
00085   signal(sig_num, signal_handler);
00086 
00087 #ifndef _WIN32
00088   // Do not do the trick with ignoring SIGCHLD, cause not all OSes (e.g. QNX)
00089   // reap zombies if SIGCHLD is ignored. On QNX, for example, waitpid()
00090   // fails if SIGCHLD is ignored, making system() non-functional.
00091   if (sig_num == SIGCHLD) {
00092     do {} while (waitpid(-1, &sig_num, WNOHANG) > 0);
00093   } else
00094 #endif
00095   { exit_flag = sig_num; }
00096 }
00097 
00098 static void vnotify(const char *fmt, va_list ap, int must_exit) {
00099   vfprintf(stderr, fmt, ap);
00100   fputc('\n', stderr);
00101   if (must_exit) {
00102     exit(EXIT_FAILURE);
00103   }
00104 }
00105 
00106 static void notify(const char *fmt, ...) {
00107   va_list ap;
00108   va_start(ap, fmt);
00109   vnotify(fmt, ap, 0);
00110   va_end(ap);
00111 }
00112 
00113 static void die(const char *fmt, ...) {
00114   va_list ap;
00115   va_start(ap, fmt);
00116   vnotify(fmt, ap, 1);
00117   va_end(ap);
00118 }
00119 
00120 static void show_usage_and_exit(void) {
00121   const char **names;
00122   int i;
00123 
00124   fprintf(stderr, "Mongoose version %s (c) Sergey Lyubka, built on %s\n",
00125           MVER, __DATE__);
00126   fprintf(stderr, "Usage:\n");
00127 #if !defined(MONGOOSE_NO_AUTH) && !defined(MONGOOSE_NO_FILESYSTEM)
00128   fprintf(stderr, "  mongoose -A <htpasswd_file> <realm> <user> <passwd>\n");
00129 #endif
00130   fprintf(stderr, "  mongoose [config_file]\n");
00131   fprintf(stderr, "  mongoose [-option value ...]\n");
00132   fprintf(stderr, "\nOPTIONS:\n");
00133 
00134   names = mg_get_valid_option_names();
00135   for (i = 0; names[i] != NULL; i += 2) {
00136     fprintf(stderr, "  -%s %s\n",
00137             names[i], names[i + 1] == NULL ? "<empty>" : names[i + 1]);
00138   }
00139   exit(EXIT_FAILURE);
00140 }
00141 
00142 #define EV_HANDLER NULL
00143 
00144 static char *sdup(const char *str) {
00145   char *p;
00146   if ((p = (char *) malloc(strlen(str) + 1)) != NULL) {
00147     strcpy(p, str);
00148   }
00149   return p;
00150 }
00151 
00152 static void set_option(char **options, const char *name, const char *value) {
00153   int i;
00154 
00155   for (i = 0; i < MAX_OPTIONS - 3; i++) {
00156     if (options[i] == NULL) {
00157       options[i] = sdup(name);
00158       options[i + 1] = sdup(value);
00159       options[i + 2] = NULL;
00160       break;
00161     } else if (!strcmp(options[i], name)) {
00162       free(options[i + 1]);
00163       options[i + 1] = sdup(value);
00164       break;
00165     }
00166   }
00167 
00168   if (i == MAX_OPTIONS - 3) {
00169     die("%s", "Too many options specified");
00170   }
00171 }
00172 
00173 static void process_command_line_arguments(char *argv[], char **options) {
00174   char line[MAX_CONF_FILE_LINE_SIZE], opt[sizeof(line)], val[sizeof(line)],
00175        *p, cpath[PATH_MAX];
00176   FILE *fp = NULL;
00177   size_t i, cmd_line_opts_start = 1, line_no = 0;
00178 
00179   // Should we use a config file ?
00180   if (argv[1] != NULL && argv[1][0] != '-') {
00181     snprintf(cpath, sizeof(cpath), "%s", argv[1]);
00182     cmd_line_opts_start = 2;
00183   } else if ((p = strrchr(argv[0], DIRSEP)) == NULL) {
00184     // No command line flags specified. Look where binary lives
00185     snprintf(cpath, sizeof(cpath), "%s", CONFIG_FILE);
00186   } else {
00187     snprintf(cpath, sizeof(cpath), "%.*s%c%s",
00188              (int) (p - argv[0]), argv[0], DIRSEP, CONFIG_FILE);
00189   }
00190   abs_path(cpath, s_config_file, sizeof(s_config_file));
00191 
00192   fp = fopen(s_config_file, "r");
00193 
00194   // If config file was set in command line and open failed, die
00195   if (cmd_line_opts_start == 2 && fp == NULL) {
00196     die("Cannot open config file %s: %s", s_config_file, strerror(errno));
00197   }
00198 
00199   // Load config file settings first
00200   if (fp != NULL) {
00201     fprintf(stderr, "Loading config file %s\n", s_config_file);
00202 
00203     // Loop over the lines in config file
00204     while (fgets(line, sizeof(line), fp) != NULL) {
00205       line_no++;
00206 
00207       // Ignore empty lines and comments
00208       for (i = 0; isspace(* (unsigned char *) &line[i]); ) i++;
00209       if (line[i] == '#' || line[i] == '\0') {
00210         continue;
00211       }
00212 
00213       if (sscanf(line, "%s %[^\r\n#]", opt, val) != 2) {
00214         printf("%s: line %d is invalid, ignoring it:\n %s",
00215                s_config_file, (int) line_no, line);
00216       } else {
00217         set_option(options, opt, val);
00218       }
00219     }
00220 
00221     fclose(fp);
00222   }
00223 
00224   // If we're under MacOS and started by launchd, then the second
00225   // argument is process serial number, -psn_.....
00226   // In this case, don't process arguments at all.
00227   if (argv[1] == NULL || memcmp(argv[1], "-psn_", 5) != 0) {
00228     // Handle command line flags.
00229     // They override config file and default settings.
00230     for (i = cmd_line_opts_start; argv[i] != NULL; i += 2) {
00231       if (argv[i][0] != '-' || argv[i + 1] == NULL) {
00232         show_usage_and_exit();
00233       }
00234       set_option(options, &argv[i][1], argv[i + 1]);
00235     }
00236   }
00237 }
00238 
00239 static void init_server_name(void) {
00240   const char *descr = "";
00241   snprintf(server_name, sizeof(server_name), "Mongoose web server v.%s%s",
00242            MVER, descr);
00243 }
00244 
00245 static int is_path_absolute(const char *path) {
00246 #ifdef _WIN32
00247   return path != NULL &&
00248     ((path[0] == '\\' && path[1] == '\\') ||  // UNC path, e.g. \\server\dir
00249      (isalpha(path[0]) && path[1] == ':' && path[2] == '\\'));  // E.g. X:\dir
00250 #else
00251   return path != NULL && path[0] == '/';
00252 #endif
00253 }
00254 
00255 static char *get_option(char **options, const char *option_name) {
00256   int i;
00257 
00258   for (i = 0; options[i] != NULL; i++)
00259     if (!strcmp(options[i], option_name))
00260       return options[i + 1];
00261 
00262   return NULL;
00263 }
00264 
00265 static void *serving_thread_func(void *param) {
00266   struct mg_server *srv = (struct mg_server *) param;
00267   while (exit_flag == 0) {
00268     mg_poll_server(srv, 1000);
00269   }
00270   return NULL;
00271 }
00272 
00273 static int path_exists(const char *path, int is_dir) {
00274   file_stat_t st;
00275   return path == NULL || (stat(path, &st) == 0 &&
00276                           ((S_ISDIR(st.st_mode) ? 1 : 0) == is_dir));
00277 }
00278 
00279 static void verify_existence(char **options, const char *name, int is_dir) {
00280   const char *path = get_option(options, name);
00281   if (!path_exists(path, is_dir)) {
00282     notify("Invalid path for %s: [%s]: (%s). Make sure that path is either "
00283            "absolute, or it is relative to mongoose executable.",
00284            name, path, strerror(errno));
00285   }
00286 }
00287 
00288 static void set_absolute_path(char *options[], const char *option_name) {
00289   char path[PATH_MAX], abs[PATH_MAX], *option_value;
00290   const char *p;
00291 
00292   // Check whether option is already set
00293   option_value = get_option(options, option_name);
00294 
00295   // If option is already set and it is an absolute path,
00296   // leave it as it is -- it's already absolute.
00297   if (option_value != NULL && !is_path_absolute(option_value)) {
00298     // Not absolute. Use the directory where mongoose executable lives
00299     // be the relative directory for everything.
00300     // Extract mongoose executable directory into path.
00301     if ((p = strrchr(s_config_file, DIRSEP)) == NULL) {
00302       getcwd(path, sizeof(path));
00303     } else {
00304       snprintf(path, sizeof(path), "%.*s", (int) (p - s_config_file),
00305                s_config_file);
00306     }
00307 
00308     strncat(path, "/", sizeof(path) - 1);
00309     strncat(path, option_value, sizeof(path) - 1);
00310 
00311     // Absolutize the path, and set the option
00312     abs_path(path, abs, sizeof(abs));
00313     set_option(options, option_name, abs);
00314   }
00315 }
00316 
00317 #if !defined(MONGOOSE_NO_AUTH) && !defined(MONGOOSE_NO_FILESYSTEM)
00318 int modify_passwords_file(const char *fname, const char *domain,
00319                           const char *user, const char *pass) {
00320   int found;
00321   char line[512], u[512], d[512], ha1[33], tmp[PATH_MAX];
00322   FILE *fp, *fp2;
00323 
00324   found = 0;
00325   fp = fp2 = NULL;
00326 
00327   // Regard empty password as no password - remove user record.
00328   if (pass != NULL && pass[0] == '\0') {
00329     pass = NULL;
00330   }
00331 
00332   (void) snprintf(tmp, sizeof(tmp), "%s.tmp", fname);
00333 
00334   // Create the file if does not exist
00335   if ((fp = fopen(fname, "a+")) != NULL) {
00336     fclose(fp);
00337   }
00338 
00339   // Open the given file and temporary file
00340   if ((fp = fopen(fname, "r")) == NULL) {
00341     return 0;
00342   } else if ((fp2 = fopen(tmp, "w+")) == NULL) {
00343     fclose(fp);
00344     return 0;
00345   }
00346 
00347   // Copy the stuff to temporary file
00348   while (fgets(line, sizeof(line), fp) != NULL) {
00349     if (sscanf(line, "%[^:]:%[^:]:%*s", u, d) != 2) {
00350       continue;
00351     }
00352 
00353     if (!strcmp(u, user) && !strcmp(d, domain)) {
00354       found++;
00355       if (pass != NULL) {
00356         mg_md5(ha1, user, ":", domain, ":", pass, NULL);
00357         fprintf(fp2, "%s:%s:%s\n", user, domain, ha1);
00358       }
00359     } else {
00360       fprintf(fp2, "%s", line);
00361     }
00362   }
00363 
00364   // If new user, just add it
00365   if (!found && pass != NULL) {
00366     mg_md5(ha1, user, ":", domain, ":", pass, NULL);
00367     fprintf(fp2, "%s:%s:%s\n", user, domain, ha1);
00368   }
00369 
00370   // Close files
00371   fclose(fp);
00372   fclose(fp2);
00373 
00374   // Put the temp file in place of real file
00375   remove(fname);
00376   rename(tmp, fname);
00377 
00378   return 1;
00379 }
00380 #endif
00381 
00382 static void start_mongoose(int argc, char *argv[]) {
00383   s_argv = argv;
00384   if ((server = mg_create_server(NULL, EV_HANDLER)) == NULL) {
00385     die("%s", "Failed to start Mongoose.");
00386   }
00387 
00388 #if !defined(MONGOOSE_NO_AUTH) && !defined(MONGOOSE_NO_FILESYSTEM)
00389   // Edit passwords file if -A option is specified
00390   if (argc > 1 && !strcmp(argv[1], "-A")) {
00391     if (argc != 6) {
00392       show_usage_and_exit();
00393     }
00394     exit(modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
00395          EXIT_SUCCESS : EXIT_FAILURE);
00396   }
00397 #endif
00398 
00399   // Show usage if -h or --help options are specified
00400   if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
00401     show_usage_and_exit();
00402   }
00403   set_options(argv);
00404 }
00405 
00406 static void set_options(char *argv[]) {
00407   char *options[MAX_OPTIONS];
00408   int i;
00409 
00410   options[0] = NULL;
00411   set_option(options, "document_root", s_default_document_root);
00412   set_option(options, "listening_port", s_default_listening_port);
00413 
00414   // Update config based on command line arguments
00415   process_command_line_arguments(argv, options);
00416 
00417   // Make sure we have absolute paths for files and directories
00418   // https://github.com/valenok/mongoose/issues/181
00419   set_absolute_path(options, "document_root");
00420   set_absolute_path(options, "dav_auth_file");
00421   set_absolute_path(options, "cgi_interpreter");
00422   set_absolute_path(options, "access_log_file");
00423   set_absolute_path(options, "global_auth_file");
00424   set_absolute_path(options, "ssl_certificate");
00425 
00426   if (!path_exists(get_option(options, "document_root"), 1)) {
00427     set_option(options, "document_root", s_default_document_root);
00428     set_absolute_path(options, "document_root");
00429     notify("Setting document_root to [%s]",
00430            mg_get_option(server, "document_root"));
00431   }
00432 
00433   // Make extra verification for certain options
00434   verify_existence(options, "document_root", 1);
00435   verify_existence(options, "cgi_interpreter", 0);
00436   verify_existence(options, "ssl_certificate", 0);
00437 
00438   for (i = 0; options[i] != NULL; i += 2) {
00439     const char *msg = mg_set_option(server, options[i], options[i + 1]);
00440     if (msg != NULL) {
00441       notify("Failed to set option [%s] to [%s]: %s",
00442              options[i], options[i + 1], msg);
00443       if (!strcmp(options[i], "listening_port")) {
00444         mg_set_option(server, "listening_port", s_default_listening_port);
00445         notify("Setting %s to [%s]", options[i], s_default_listening_port);
00446       }
00447     }
00448     free(options[i]);
00449     free(options[i + 1]);
00450   }
00451 
00452   // Change current working directory to document root. This way,
00453   // scripts can use relative paths.
00454   chdir(mg_get_option(server, "document_root"));
00455 
00456 #if 0
00457   // Add an ability to pass listening socket to mongoose
00458   {
00459     const char *env = getenv("MONGOOSE_LISTENING_SOCKET");
00460     if (env != NULL && atoi(env) > 0 ) {
00461       mg_set_listening_socket(server, atoi(env));
00462     }
00463   }
00464 #endif
00465 
00466   // Setup signal handler: quit on Ctrl-C
00467   signal(SIGTERM, signal_handler);
00468   signal(SIGINT, signal_handler);
00469 #ifndef _WIN32
00470   signal(SIGCHLD, signal_handler);
00471 #endif
00472 }
00473 
00474 int main(int argc, char *argv[]) {
00475   init_server_name();
00476   start_mongoose(argc, argv);
00477   printf("%s serving [%s] on port %s\n",
00478          server_name, mg_get_option(server, "document_root"),
00479          mg_get_option(server, "listening_port"));
00480   fflush(stdout);  // Needed, Windows terminals might not be line-buffered
00481   serving_thread_func(server);
00482   printf("Exiting on signal %d ...", exit_flag);
00483   fflush(stdout);
00484   mg_destroy_server(&server);
00485   printf("%s\n", " done.");
00486 
00487   return EXIT_SUCCESS;
00488 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:07