Example #1
0
static void start_mongoose(int argc, char *argv[]) {
  char *options[MAX_OPTIONS];
  int i;

  // Edit passwords file if -A option is specified
  if (argc > 1 && !strcmp(argv[1], "-A")) {
    if (argc != 6) {
      show_usage_and_exit();
    }
    exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
         EXIT_SUCCESS : EXIT_FAILURE);
  }

  // Show usage if -h or --help options are specified
  if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
    show_usage_and_exit();
  }

  /* Update config based on command line arguments */
  process_command_line_arguments(argv, options);

  /* Setup signal handler: quit on Ctrl-C */
  signal(SIGTERM, signal_handler);
  signal(SIGINT, signal_handler);

  /* Start Mongoose */
  ctx = mg_start(&mongoose_callback, NULL, (const char **) options);
  for (i = 0; options[i] != NULL; i++) {
    free(options[i]);
  }

  if (ctx == NULL) {
    die("%s", "Failed to start Mongoose.");
  }
}
Example #2
0
static void start_mongoose(int argc, char *argv[]) {
  struct mg_callbacks callbacks;
  char *options[MAX_OPTIONS];
  int i;

  // Edit passwords file if -A option is specified
  if (argc > 1 && !strcmp(argv[1], "-A")) {
    if (argc != 6) {
      show_usage_and_exit();
    }
    exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
         EXIT_SUCCESS : EXIT_FAILURE);
  }

  // Show usage if -h or --help options are specified
  if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
    show_usage_and_exit();
  }

  options[0] = NULL;
  set_option(options, "document_root", ".");

  // Update config based on command line arguments
  process_command_line_arguments(argv, options);

  // Make sure we have absolute paths for files and directories
  // https://github.com/valenok/mongoose/issues/181
  set_absolute_path(options, "document_root", argv[0]);
  set_absolute_path(options, "put_delete_auth_file", argv[0]);
  set_absolute_path(options, "cgi_interpreter", argv[0]);
  set_absolute_path(options, "access_log_file", argv[0]);
  set_absolute_path(options, "error_log_file", argv[0]);
  set_absolute_path(options, "global_auth_file", argv[0]);
  set_absolute_path(options, "ssl_certificate", argv[0]);

  // Make extra verification for certain options
  verify_existence(options, "document_root", 1);
  verify_existence(options, "cgi_interpreter", 0);
  verify_existence(options, "ssl_certificate", 0);

  // Setup signal handler: quit on Ctrl-C
  signal(SIGTERM, signal_handler);
  signal(SIGINT, signal_handler);

  // Start Mongoose
  memset(&callbacks, 0, sizeof(callbacks));
  callbacks.log_message = &log_message;
  ctx = mg_start(&callbacks, NULL, (const char **) options);
  for (i = 0; options[i] != NULL; i++) {
    free(options[i]);
  }

  if (ctx == NULL) {
    die("%s", "Failed to start Mongoose.");
  }
}
Example #3
0
static void start_mongoose(int argc, char *argv[]) {
  char *options[MAX_OPTIONS * 2] = { NULL };
  int i;
  struct mg_user_class_t userdef = {
      0,
      &mongoose_callback
  };

  /* Edit passwords file if -A option is specified */
  if (argc > 1 && !strcmp(argv[1], "-A")) {
    if (argc != 6) {
      show_usage_and_exit(ctx);
    }
    exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
         EXIT_SUCCESS : EXIT_FAILURE);
  }

  /* Show usage if -h or --help options are specified */
  if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
    show_usage_and_exit(ctx);
  }

  /* Update config based on command line arguments */
  process_command_line_arguments(argv, options);

  /* Setup signal handler: quit on Ctrl-C */
  signal(SIGTERM, signal_handler);
  signal(SIGINT, signal_handler);
  signal(SIGABRT, signal_handler);
  signal(SIGILL, signal_handler);
  signal(SIGSEGV, signal_handler);
  signal(SIGFPE, signal_handler);
  // SIGINT and SIGTERM are pretty darn useless for Win32 applications.
  // See http://msdn.microsoft.com/en-us/library/ms685049%28VS.85%29.aspx
#if defined(_WIN32)
  if (!SetConsoleCtrlHandler(mg_win32_break_handler, TRUE))
  {
    die("Failed to set up the Win32 console Ctrl-Break handler.");
  }
#endif

  /* Start Mongoose */
  ctx = mg_start(&userdef, (const char **)options);
  for (i = 0; options[i] != NULL; i++) {
    free(options[i]);
  }

  if (ctx == NULL) {
    die("Failed to start Mongoose. Maybe some options are "
        "assigned bad values?\nTry to run with '-e error_log.txt' "
        "and check error_log.txt for more information.");
  }
}
Example #4
0
/*
 * Edit the passwords file.
 */
static int mg_edit_passwords(const char *fname, const char *domain,
                             const char *user, const char *pass) {
  struct mg_context *ctx;
  const char *options[] = {"authentication_domain", NULL, NULL};
  int success;

  options[1] = domain;
  ctx = mg_start(NULL, NULL, options);
  success = mg_modify_passwords_file(ctx, fname, user, pass);
  mg_stop(ctx);

  return success;
}
Example #5
0
/*
 * Edit the passwords file.
 */
static int
mg_edit_passwords(const char *fname, const char *domain,
		const char *user, const char *pass)
{
	struct mg_context	*ctx;
	int			retval;

	ctx = mg_start();
	(void) mg_set_option(ctx, "auth_realm", domain);
	retval = mg_modify_passwords_file(ctx, fname, user, pass);
	mg_stop(ctx);

	return (retval);
}
Example #6
0
static void start_monguvse(int argc, char** argv)
{
  int i;
  char *options[MAX_OPTIONS];
  struct mg_callbacks callbacks;

  // Edit passwords file if -A option is specified
  if (argc > 1 && !strcmp(argv[1], "-A")) {
    if (argc != 6) {
      show_usage_and_exit();
    }
    exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
         EXIT_SUCCESS : EXIT_FAILURE);
  }

  // Show usage if -h or --help options are specified
  if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
    show_usage_and_exit();
  }

  /* Update config based on command line arguments */
  process_command_line_arguments(argv, options);

  /* Setup signal handler: quit on Ctrl-C */
/*
  signal(SIGTERM, signal_handler);
  signal(SIGINT, signal_handler);
*/

  /* Start Mongoose */
  extern int request_scheduler(struct mg_connection *conn);
  extern int request_router(struct mg_connection *conn);
  memset(&callbacks, 0, sizeof(callbacks));
  callbacks.log_message = &log_message;
  callbacks.log_message2 = &log_message2;
  callbacks.schedule_request = &request_scheduler;
  callbacks.begin_request = &request_router;

  _ctx = mg_start(&callbacks, NULL, (const char **) options);
  for (i = 0; options[i] != NULL; i++) {
    free(options[i]);
  }

  if (_ctx == NULL) {
    die("%s", "Failed to start Mongoose.");
  }
}
Example #7
0
static void start_server(int argc, char *argv[]) {
  struct mg_callbacks callbacks;
  char *options[MAX_OPTIONS];
  int i;

  // Edit passwords file if -A option is specified
  if (argc > 1 && !strcmp(argv[1], "-A")) {
    if (argc != 6) {
      show_usage_and_exit();
    }
    exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
         EXIT_SUCCESS : EXIT_FAILURE);
  }

  // Show usage if -h or --help options are specified
  if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
    show_usage_and_exit();
  }

  /* Update config based on command line arguments */
  process_command_line_arguments(argv, options);

  /* Setup signal handler: quit on Ctrl-C */
  signal(SIGTERM, signal_handler);
  signal(SIGINT, signal_handler);

  /* Start Tinyweb server */
  memset(&callbacks, 0, sizeof(callbacks));
  callbacks.websocket_ready = websocket_ready_handler;
  callbacks.websocket_data = websocket_data_handler;

  callbacks.log_message = &log_message;
  ctx = mg_start(&callbacks, NULL, (const char **) options);
  for (i = 0; options[i] != NULL; i++) {
    free(options[i]);
  }

  if (ctx == NULL) {
    die("%s", "Failed to start Tinyweb.");
  }
}
static void start_mongoose(int argc, char *argv[]) {
  char *options[MAX_OPTIONS];
  int i;

  /* Edit passwords file if -A option is specified */
  if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'A') {
    if (argc != 6) {
      show_usage_and_exit();
    }
    exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
         EXIT_SUCCESS : EXIT_FAILURE);
  }

  /* Show usage if -h or --help options are specified */
  if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
    show_usage_and_exit();
  }

  /* Update config based on command line arguments */
  process_command_line_arguments(argv, options);

  /* Setup signal handler: quit on Ctrl-C */
  signal(SIGTERM, signal_handler);
  signal(SIGINT, signal_handler);

  /* Start Mongoose */
  ctx = mg_start(NULL, NULL, (const char **) options);
  for (i = 0; options[i] != NULL; i++) {
    free(options[i]);
  }

  if (ctx == NULL) {
    die("%s", "Failed to start Mongoose. Maybe some options are "
        "assigned bad values?\nTry to run with '-e error_log.txt' "
        "and check error_log.txt for more information.");
  }
}
Example #9
0
int
AuthStartHandler(struct mg_connection *conn, void *cbdata)
{
	static unsigned long long firstload = 0;
	const char *passfile = "password_example_file.txt";
	const char *realm = "password_example";
	const char *user = "******";
	char passwd[64];

	if (firstload == 0) {

		/* Set a random password (4 digit number - bad idea from a security
		 * point of view, but this is an API demo, not a security tutorial),
		 * and store it in some directory within the document root (extremely
		 * bad idea, but this is still not a security tutorial).
		 * The reason we create a new password every time the server starts
		 * is just for demonstration - we don't want the browser to store the
		 * password, so when we repeat the test we start with a new password.
		 */
		firstload = (unsigned long long)time(NULL);
		sprintf(passwd, "%04u", (unsigned int)(firstload % 10000));
		mg_modify_passwords_file(passfile, realm, user, passwd);

		/* Just tell the user the new password generated for this test. */
		mg_printf(conn,
		          "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
		          "close\r\n\r\n");

		mg_printf(conn, "<!DOCTYPE html>\n");
		mg_printf(conn, "<html>\n<head>\n");
		mg_printf(conn, "<meta charset=\"UTF-8\">\n");
		mg_printf(conn, "<title>Auth handlerexample</title>\n");
		mg_printf(conn, "</head>\n");

		mg_printf(conn, "<body>\n");
		mg_printf(conn,
		          "<p>The first time you visit this page, it's free!</p>\n");
		mg_printf(conn,
		          "<p>Next time, use username \"%s\" and password \"%s\"</p>\n",
		          user,
		          passwd);
		mg_printf(conn, "</body>\n</html>\n");

		return 1;
	}

	if (mg_check_digest_access_authentication(conn, realm, passfile) <= 0) {
		/* No valid authorization */
		mg_send_digest_access_authentication_request(conn, realm);
		return 1;
	}

	mg_printf(conn,
	          "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
	          "close\r\n\r\n");

	mg_printf(conn, "<!DOCTYPE html>\n");
	mg_printf(conn, "<html>\n<head>\n");
	mg_printf(conn, "<meta charset=\"UTF-8\">\n");
	mg_printf(conn, "<title>Auth handlerexample</title>\n");
	mg_printf(conn, "</head>\n");

	mg_printf(conn, "<body>\n");
	mg_printf(conn, "<p>This is the password protected contents</p>\n");
	mg_printf(conn, "</body>\n</html>\n");

	return 1;
}