web_server.c
Go to the documentation of this file.
1 // Copyright (c) 2004-2013 Sergey Lyubka
2 // Copyright (c) 2013-2014 Cesanta Software Limited
3 
4 #undef UNICODE // Use ANSI WinAPI functions
5 #undef _UNICODE // Use multibyte encoding on Windows
6 #define _MBCS // Use multibyte encoding on Windows
7 #define _WIN32_WINNT 0x500 // Enable MIIM_BITMAP
8 #define _CRT_SECURE_NO_WARNINGS // Disable deprecation warning in VS2005
9 #define _XOPEN_SOURCE 600 // For PATH_MAX on linux
10 #undef WIN32_LEAN_AND_MEAN // Let windows.h always include winsock2.h
11 
12 #include <sys/stat.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <signal.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <stddef.h>
20 #include <stdarg.h>
21 #include <ctype.h>
22 #include <time.h>
23 
24 #include "mongoose.h"
25 
26 #ifdef _WIN32
27 #include <windows.h>
28 #include <direct.h> // For chdir()
29 #include <winsvc.h>
30 #include <shlobj.h>
31 
32 #ifndef PATH_MAX
33 #define PATH_MAX MAX_PATH
34 #endif
35 
36 #ifndef S_ISDIR
37 #define S_ISDIR(x) ((x) & _S_IFDIR)
38 #endif
39 
40 #define DIRSEP '\\'
41 #define snprintf _snprintf
42 #define vsnprintf _vsnprintf
43 #define sleep(x) Sleep((x) * 1000)
44 #define abs_path(rel, abs, abs_size) _fullpath((abs), (rel), (abs_size))
45 #define SIGCHLD 0
46 typedef struct _stat file_stat_t;
47 #define stat(x, y) _stat((x), (y))
48 #else
49 typedef struct stat file_stat_t;
50 #include <sys/wait.h>
51 #include <unistd.h>
52 
53 #ifdef IOS
54 #include <ifaddrs.h>
55 #endif
56 
57 #define DIRSEP '/'
58 #define __cdecl
59 #define abs_path(rel, abs, abs_size) realpath((rel), (abs))
60 #endif // _WIN32
61 
62 #define MAX_OPTIONS 100
63 #define MAX_CONF_FILE_LINE_SIZE (8 * 1024)
64 
65 #ifndef MVER
66 #define MVER MONGOOSE_VERSION
67 #endif
68 
69 static int exit_flag;
70 static char server_name[50]; // Set by init_server_name()
71 static char s_config_file[PATH_MAX]; // Set by process_command_line_arguments
72 static struct mg_server *server; // Set by start_mongoose()
73 static const char *s_default_document_root = ".";
74 static const char *s_default_listening_port = "8080";
75 static char **s_argv = { NULL };
76 
77 static void set_options(char *argv[]);
78 
79 #if !defined(CONFIG_FILE)
80 #define CONFIG_FILE "mongoose.conf"
81 #endif /* !CONFIG_FILE */
82 
83 static void __cdecl signal_handler(int sig_num) {
84  // Reinstantiate signal handler
85  signal(sig_num, signal_handler);
86 
87 #ifndef _WIN32
88  // Do not do the trick with ignoring SIGCHLD, cause not all OSes (e.g. QNX)
89  // reap zombies if SIGCHLD is ignored. On QNX, for example, waitpid()
90  // fails if SIGCHLD is ignored, making system() non-functional.
91  if (sig_num == SIGCHLD) {
92  do {} while (waitpid(-1, &sig_num, WNOHANG) > 0);
93  } else
94 #endif
95  { exit_flag = sig_num; }
96 }
97 
98 static void vnotify(const char *fmt, va_list ap, int must_exit) {
99  vfprintf(stderr, fmt, ap);
100  fputc('\n', stderr);
101  if (must_exit) {
102  exit(EXIT_FAILURE);
103  }
104 }
105 
106 static void notify(const char *fmt, ...) {
107  va_list ap;
108  va_start(ap, fmt);
109  vnotify(fmt, ap, 0);
110  va_end(ap);
111 }
112 
113 static void die(const char *fmt, ...) {
114  va_list ap;
115  va_start(ap, fmt);
116  vnotify(fmt, ap, 1);
117  va_end(ap);
118 }
119 
120 static void show_usage_and_exit(void) {
121  const char **names;
122  int i;
123 
124  fprintf(stderr, "Mongoose version %s (c) Sergey Lyubka, built on %s\n",
125  MVER, __DATE__);
126  fprintf(stderr, "Usage:\n");
127 #if !defined(MONGOOSE_NO_AUTH) && !defined(MONGOOSE_NO_FILESYSTEM)
128  fprintf(stderr, " mongoose -A <htpasswd_file> <realm> <user> <passwd>\n");
129 #endif
130  fprintf(stderr, " mongoose [config_file]\n");
131  fprintf(stderr, " mongoose [-option value ...]\n");
132  fprintf(stderr, "\nOPTIONS:\n");
133 
134  names = mg_get_valid_option_names();
135  for (i = 0; names[i] != NULL; i += 2) {
136  fprintf(stderr, " -%s %s\n",
137  names[i], names[i + 1] == NULL ? "<empty>" : names[i + 1]);
138  }
139  exit(EXIT_FAILURE);
140 }
141 
142 #define EV_HANDLER NULL
143 
144 static char *sdup(const char *str) {
145  char *p;
146  if ((p = (char *) malloc(strlen(str) + 1)) != NULL) {
147  strcpy(p, str);
148  }
149  return p;
150 }
151 
152 static void set_option(char **options, const char *name, const char *value) {
153  int i;
154 
155  for (i = 0; i < MAX_OPTIONS - 3; i++) {
156  if (options[i] == NULL) {
157  options[i] = sdup(name);
158  options[i + 1] = sdup(value);
159  options[i + 2] = NULL;
160  break;
161  } else if (!strcmp(options[i], name)) {
162  free(options[i + 1]);
163  options[i + 1] = sdup(value);
164  break;
165  }
166  }
167 
168  if (i == MAX_OPTIONS - 3) {
169  die("%s", "Too many options specified");
170  }
171 }
172 
173 static void process_command_line_arguments(char *argv[], char **options) {
174  char line[MAX_CONF_FILE_LINE_SIZE], opt[sizeof(line)], val[sizeof(line)],
175  *p, cpath[PATH_MAX];
176  FILE *fp = NULL;
177  size_t i, cmd_line_opts_start = 1, line_no = 0;
178 
179  // Should we use a config file ?
180  if (argv[1] != NULL && argv[1][0] != '-') {
181  snprintf(cpath, sizeof(cpath), "%s", argv[1]);
182  cmd_line_opts_start = 2;
183  } else if ((p = strrchr(argv[0], DIRSEP)) == NULL) {
184  // No command line flags specified. Look where binary lives
185  snprintf(cpath, sizeof(cpath), "%s", CONFIG_FILE);
186  } else {
187  snprintf(cpath, sizeof(cpath), "%.*s%c%s",
188  (int) (p - argv[0]), argv[0], DIRSEP, CONFIG_FILE);
189  }
190  abs_path(cpath, s_config_file, sizeof(s_config_file));
191 
192  fp = fopen(s_config_file, "r");
193 
194  // If config file was set in command line and open failed, die
195  if (cmd_line_opts_start == 2 && fp == NULL) {
196  die("Cannot open config file %s: %s", s_config_file, strerror(errno));
197  }
198 
199  // Load config file settings first
200  if (fp != NULL) {
201  fprintf(stderr, "Loading config file %s\n", s_config_file);
202 
203  // Loop over the lines in config file
204  while (fgets(line, sizeof(line), fp) != NULL) {
205  line_no++;
206 
207  // Ignore empty lines and comments
208  for (i = 0; isspace(* (unsigned char *) &line[i]); ) i++;
209  if (line[i] == '#' || line[i] == '\0') {
210  continue;
211  }
212 
213  if (sscanf(line, "%s %[^\r\n#]", opt, val) != 2) {
214  printf("%s: line %d is invalid, ignoring it:\n %s",
215  s_config_file, (int) line_no, line);
216  } else {
217  set_option(options, opt, val);
218  }
219  }
220 
221  fclose(fp);
222  }
223 
224  // If we're under MacOS and started by launchd, then the second
225  // argument is process serial number, -psn_.....
226  // In this case, don't process arguments at all.
227  if (argv[1] == NULL || memcmp(argv[1], "-psn_", 5) != 0) {
228  // Handle command line flags.
229  // They override config file and default settings.
230  for (i = cmd_line_opts_start; argv[i] != NULL; i += 2) {
231  if (argv[i][0] != '-' || argv[i + 1] == NULL) {
233  }
234  set_option(options, &argv[i][1], argv[i + 1]);
235  }
236  }
237 }
238 
239 static void init_server_name(void) {
240  const char *descr = "";
241  snprintf(server_name, sizeof(server_name), "Mongoose web server v.%s%s",
242  MVER, descr);
243 }
244 
245 static int is_path_absolute(const char *path) {
246 #ifdef _WIN32
247  return path != NULL &&
248  ((path[0] == '\\' && path[1] == '\\') || // UNC path, e.g. \\server\dir
249  (isalpha(path[0]) && path[1] == ':' && path[2] == '\\')); // E.g. X:\dir
250 #else
251  return path != NULL && path[0] == '/';
252 #endif
253 }
254 
255 static char *get_option(char **options, const char *option_name) {
256  int i;
257 
258  for (i = 0; options[i] != NULL; i++)
259  if (!strcmp(options[i], option_name))
260  return options[i + 1];
261 
262  return NULL;
263 }
264 
265 static void *serving_thread_func(void *param) {
266  struct mg_server *srv = (struct mg_server *) param;
267  while (exit_flag == 0) {
268  mg_poll_server(srv, 1000);
269  }
270  return NULL;
271 }
272 
273 static int path_exists(const char *path, int is_dir) {
274  file_stat_t st;
275  return path == NULL || (stat(path, &st) == 0 &&
276  ((S_ISDIR(st.st_mode) ? 1 : 0) == is_dir));
277 }
278 
279 static void verify_existence(char **options, const char *name, int is_dir) {
280  const char *path = get_option(options, name);
281  if (!path_exists(path, is_dir)) {
282  notify("Invalid path for %s: [%s]: (%s). Make sure that path is either "
283  "absolute, or it is relative to mongoose executable.",
284  name, path, strerror(errno));
285  }
286 }
287 
288 static void set_absolute_path(char *options[], const char *option_name) {
289  char path[PATH_MAX], abs[PATH_MAX], *option_value;
290  const char *p;
291 
292  // Check whether option is already set
293  option_value = get_option(options, option_name);
294 
295  // If option is already set and it is an absolute path,
296  // leave it as it is -- it's already absolute.
297  if (option_value != NULL && !is_path_absolute(option_value)) {
298  // Not absolute. Use the directory where mongoose executable lives
299  // be the relative directory for everything.
300  // Extract mongoose executable directory into path.
301  if ((p = strrchr(s_config_file, DIRSEP)) == NULL) {
302  getcwd(path, sizeof(path));
303  } else {
304  snprintf(path, sizeof(path), "%.*s", (int) (p - s_config_file),
305  s_config_file);
306  }
307 
308  strncat(path, "/", sizeof(path) - 1);
309  strncat(path, option_value, sizeof(path) - 1);
310 
311  // Absolutize the path, and set the option
312  abs_path(path, abs, sizeof(abs));
313  set_option(options, option_name, abs);
314  }
315 }
316 
317 #if !defined(MONGOOSE_NO_AUTH) && !defined(MONGOOSE_NO_FILESYSTEM)
318 int modify_passwords_file(const char *fname, const char *domain,
319  const char *user, const char *pass) {
320  int found;
321  char line[512], u[512], d[512], ha1[33], tmp[PATH_MAX];
322  FILE *fp, *fp2;
323 
324  found = 0;
325  fp = fp2 = NULL;
326 
327  // Regard empty password as no password - remove user record.
328  if (pass != NULL && pass[0] == '\0') {
329  pass = NULL;
330  }
331 
332  (void) snprintf(tmp, sizeof(tmp), "%s.tmp", fname);
333 
334  // Create the file if does not exist
335  if ((fp = fopen(fname, "a+")) != NULL) {
336  fclose(fp);
337  }
338 
339  // Open the given file and temporary file
340  if ((fp = fopen(fname, "r")) == NULL) {
341  return 0;
342  } else if ((fp2 = fopen(tmp, "w+")) == NULL) {
343  fclose(fp);
344  return 0;
345  }
346 
347  // Copy the stuff to temporary file
348  while (fgets(line, sizeof(line), fp) != NULL) {
349  if (sscanf(line, "%[^:]:%[^:]:%*s", u, d) != 2) {
350  continue;
351  }
352 
353  if (!strcmp(u, user) && !strcmp(d, domain)) {
354  found++;
355  if (pass != NULL) {
356  mg_md5(ha1, user, ":", domain, ":", pass, NULL);
357  fprintf(fp2, "%s:%s:%s\n", user, domain, ha1);
358  }
359  } else {
360  fprintf(fp2, "%s", line);
361  }
362  }
363 
364  // If new user, just add it
365  if (!found && pass != NULL) {
366  mg_md5(ha1, user, ":", domain, ":", pass, NULL);
367  fprintf(fp2, "%s:%s:%s\n", user, domain, ha1);
368  }
369 
370  // Close files
371  fclose(fp);
372  fclose(fp2);
373 
374  // Put the temp file in place of real file
375  remove(fname);
376  rename(tmp, fname);
377 
378  return 1;
379 }
380 #endif
381 
382 static void start_mongoose(int argc, char *argv[]) {
383  s_argv = argv;
384  if ((server = mg_create_server(NULL, EV_HANDLER)) == NULL) {
385  die("%s", "Failed to start Mongoose.");
386  }
387 
388 #if !defined(MONGOOSE_NO_AUTH) && !defined(MONGOOSE_NO_FILESYSTEM)
389  // Edit passwords file if -A option is specified
390  if (argc > 1 && !strcmp(argv[1], "-A")) {
391  if (argc != 6) {
393  }
394  exit(modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
395  EXIT_SUCCESS : EXIT_FAILURE);
396  }
397 #endif
398 
399  // Show usage if -h or --help options are specified
400  if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
402  }
403  set_options(argv);
404 }
405 
406 static void set_options(char *argv[]) {
407  char *options[MAX_OPTIONS];
408  int i;
409 
410  options[0] = NULL;
411  set_option(options, "document_root", s_default_document_root);
412  set_option(options, "listening_port", s_default_listening_port);
413 
414  // Update config based on command line arguments
415  process_command_line_arguments(argv, options);
416 
417  // Make sure we have absolute paths for files and directories
418  // https://github.com/valenok/mongoose/issues/181
419  set_absolute_path(options, "document_root");
420  set_absolute_path(options, "dav_auth_file");
421  set_absolute_path(options, "cgi_interpreter");
422  set_absolute_path(options, "access_log_file");
423  set_absolute_path(options, "global_auth_file");
424  set_absolute_path(options, "ssl_certificate");
425 
426  if (!path_exists(get_option(options, "document_root"), 1)) {
427  set_option(options, "document_root", s_default_document_root);
428  set_absolute_path(options, "document_root");
429  notify("Setting document_root to [%s]",
430  mg_get_option(server, "document_root"));
431  }
432 
433  // Make extra verification for certain options
434  verify_existence(options, "document_root", 1);
435  verify_existence(options, "cgi_interpreter", 0);
436  verify_existence(options, "ssl_certificate", 0);
437 
438  for (i = 0; options[i] != NULL; i += 2) {
439  const char *msg = mg_set_option(server, options[i], options[i + 1]);
440  if (msg != NULL) {
441  notify("Failed to set option [%s] to [%s]: %s",
442  options[i], options[i + 1], msg);
443  if (!strcmp(options[i], "listening_port")) {
444  mg_set_option(server, "listening_port", s_default_listening_port);
445  notify("Setting %s to [%s]", options[i], s_default_listening_port);
446  }
447  }
448  free(options[i]);
449  free(options[i + 1]);
450  }
451 
452  // Change current working directory to document root. This way,
453  // scripts can use relative paths.
454  chdir(mg_get_option(server, "document_root"));
455 
456 #if 0
457  // Add an ability to pass listening socket to mongoose
458  {
459  const char *env = getenv("MONGOOSE_LISTENING_SOCKET");
460  if (env != NULL && atoi(env) > 0 ) {
461  mg_set_listening_socket(server, atoi(env));
462  }
463  }
464 #endif
465 
466  // Setup signal handler: quit on Ctrl-C
467  signal(SIGTERM, signal_handler);
468  signal(SIGINT, signal_handler);
469 #ifndef _WIN32
470  signal(SIGCHLD, signal_handler);
471 #endif
472 }
473 
474 int main(int argc, char *argv[]) {
476  start_mongoose(argc, argv);
477  printf("%s serving [%s] on port %s\n",
478  server_name, mg_get_option(server, "document_root"),
479  mg_get_option(server, "listening_port"));
480  fflush(stdout); // Needed, Windows terminals might not be line-buffered
481  serving_thread_func(server);
482  printf("Exiting on signal %d ...", exit_flag);
483  fflush(stdout);
484  mg_destroy_server(&server);
485  printf("%s\n", " done.");
486 
487  return EXIT_SUCCESS;
488 }
#define free(ptr)
Definition: curl_memory.h:130
d
static void die(const char *fmt,...)
Definition: web_server.c:113
#define getenv
Definition: setup-vms.h:52
static void __cdecl signal_handler(int sig_num)
Definition: web_server.c:83
int stat(const char *path, struct stat *buffer)
static const char * s_default_listening_port
Definition: web_server.c:74
#define __cdecl
Definition: web_server.c:58
static void set_absolute_path(char *options[], const char *option_name)
Definition: web_server.c:288
static char server_name[50]
Definition: web_server.c:70
char * mg_md5(char buf[33],...)
Definition: mongoose.c:4122
struct mg_server * mg_create_server(void *server_data, mg_handler_t handler)
Definition: mongoose.c:5431
const char * mg_get_option(const struct mg_server *server, const char *name)
Definition: mongoose.c:5425
static void show_usage_and_exit(void)
Definition: web_server.c:120
const char ** mg_get_valid_option_names(void)
Definition: mongoose.c:5095
static int path_exists(const char *path, int is_dir)
Definition: web_server.c:273
static void process_command_line_arguments(char *argv[], char **options)
Definition: web_server.c:173
#define malloc(size)
Definition: curl_memory.h:124
static char s_config_file[PATH_MAX]
Definition: web_server.c:71
const char ** p
Definition: unit1394.c:76
unsigned int i
Definition: unit1303.c:79
static int is_path_absolute(const char *path)
Definition: web_server.c:245
#define DIRSEP
Definition: web_server.c:57
time_t mg_poll_server(struct mg_server *server, int milliseconds)
Definition: mongoose.c:4965
#define MAX_CONF_FILE_LINE_SIZE
Definition: web_server.c:63
#define MAX_OPTIONS
Definition: web_server.c:62
static void verify_existence(char **options, const char *name, int is_dir)
Definition: web_server.c:279
const char * str
Definition: unit1398.c:33
static void start_mongoose(int argc, char *argv[])
Definition: web_server.c:382
static void set_options(char *argv[])
Definition: web_server.c:406
options
#define printf
Definition: curl_printf.h:40
static char ** s_argv
Definition: web_server.c:75
static struct mg_server * server
Definition: web_server.c:72
#define vfprintf
Definition: curl_printf.h:44
#define EV_HANDLER
Definition: web_server.c:142
static void init_server_name(void)
Definition: web_server.c:239
static int exit_flag
Definition: web_server.c:69
static void * serving_thread_func(void *param)
Definition: web_server.c:265
INLINE Rall1d< T, V, S > abs(const Rall1d< T, V, S > &x)
#define abs_path(rel, abs, abs_size)
Definition: web_server.c:59
static void vnotify(const char *fmt, va_list ap, int must_exit)
Definition: web_server.c:98
const char * mg_set_option(struct mg_server *server, const char *name, const char *value)
Definition: mongoose.c:5143
#define MVER
Definition: web_server.c:66
struct stat file_stat_t
Definition: web_server.c:49
UNITTEST_START int * value
Definition: unit1602.c:51
int main(int argc, char *argv[])
Definition: web_server.c:474
static char * sdup(const char *str)
Definition: web_server.c:144
#define fprintf
Definition: curl_printf.h:41
#define snprintf
Definition: curl_printf.h:42
int modify_passwords_file(const char *fname, const char *domain, const char *user, const char *pass)
Definition: web_server.c:318
const char * name
Definition: curl_sasl.c:54
static void set_option(char **options, const char *name, const char *value)
Definition: web_server.c:152
void mg_destroy_server(struct mg_server **server)
Definition: mongoose.c:4969
#define CONFIG_FILE
Definition: web_server.c:80
static void notify(const char *fmt,...)
Definition: web_server.c:106
static const char * s_default_document_root
Definition: web_server.c:73
static char * get_option(char **options, const char *option_name)
Definition: web_server.c:255
const char * path
Definition: util.c:192


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:17