From 03f4c1f390494b8e87c6f05e43050a725bc75a11 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 19 May 2015 00:43:41 +0200 Subject: [PATCH] add option to deny directory listing Signed-off-by: Christian Hesse --- darkhttpd.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/darkhttpd.c b/darkhttpd.c index a8d704a..bab5224 100644 --- a/darkhttpd.c +++ b/darkhttpd.c @@ -272,6 +272,7 @@ static in_addr_t bindaddr = INADDR_ANY; static uint16_t bindport = 8080; /* or 80 if running as root */ static int max_connections = -1; /* kern.ipc.somaxconn */ static const char *index_name = "index.html"; +static int no_listing = 0; static int sockin = -1; /* socket to accept connections from */ static char *wwwroot = NULL; /* a path name */ @@ -886,6 +887,8 @@ static void usage(const char *argv0) { printf("\t--index filename (default: %s)\n" "\t\tDefault file to serve when a directory is requested.\n\n", index_name); + printf("\t--no-listing\n" + "\t\tDo not serve listing if directory is requested.\n\n"); printf("\t--mimetypes filename (optional)\n" "\t\tParses specified file for extension-MIME associations.\n\n"); printf("\t--uid uid/uname, --gid gid/gname (default: don't privdrop)\n" @@ -994,6 +997,9 @@ static void parse_commandline(const int argc, char *argv[]) { errx(1, "missing filename after --index"); index_name = argv[i]; } + else if (strcmp(argv[i], "--no-listing") == 0) { + no_listing = 1; + } else if (strcmp(argv[i], "--mimetypes") == 0) { if (++i >= argc) errx(1, "missing filename after --mimetypes"); @@ -1827,6 +1833,11 @@ static void process_get(struct connection *conn) { xasprintf(&target, "%s%s%s", wwwroot, decoded_url, index_name); if (!file_exists(target)) { free(target); + if (no_listing) { + default_reply(conn, 404, "Not Found", + "The URL you requested (%s) was not found.", conn->url); + return; + } xasprintf(&target, "%s%s", wwwroot, decoded_url); generate_dir_listing(conn, target); free(target);