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