From 4863a8c28e8d8e9c56925b99af53cfc85db473bf Mon Sep 17 00:00:00 2001 From: Urban Wallasch Date: Sat, 6 Apr 2019 09:55:40 +0200 Subject: [PATCH] * Fixed a regression bug in handling of symbolic links. * Added option to disable directory indexes, in preparation for index page support. * Some minor adjustments. --- config.go | 4 ++++ gogopherd.go | 13 +++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 9fd4001..8b0564d 100644 --- a/config.go +++ b/config.go @@ -22,6 +22,7 @@ var cfg = struct { message string fsymln bool showdot bool + noidx bool verbose bool }{ iface: "localhost", @@ -31,6 +32,7 @@ var cfg = struct { message: "", fsymln: false, showdot: false, + noidx: false, verbose: false, } @@ -44,6 +46,7 @@ func initialize() { flag.BoolVar(&help, "h", help, "show this help page") flag.BoolVar(&cfg.fsymln, "s", cfg.fsymln, "follow symbolic links") flag.BoolVar(&cfg.showdot, "d", cfg.showdot, "allow access to dotfiles") + flag.BoolVar(&cfg.noidx, "l", cfg.noidx, "allow directory listings") flag.BoolVar(&cfg.verbose, "v", cfg.verbose, "produce verbose output") flag.Parse() if help { @@ -74,6 +77,7 @@ func initialize() { tracer.Print("doc root: ", cfg.docRoot) tracer.Print("fsymlinks: ", cfg.fsymln) tracer.Print("showdot: ", cfg.showdot) + tracer.Print("noidx: ", cfg.noidx) tracer.Print("fqdn: ", cfg.fqdn) tracer.Print("message: ", cfg.message) tracer.Print("verbose: ", cfg.verbose) diff --git a/gogopherd.go b/gogopherd.go index a4e95ce..ee5a618 100644 --- a/gogopherd.go +++ b/gogopherd.go @@ -49,7 +49,7 @@ func createIndex(selector string) (string, error) { // create a file reference ftype, _ := guessFiletype(dirname + pathSep + fi.Name()) list += ftype + fi.Name() + " | " + humanSize(fi.Size()) + " | " + fts + "\t" + selector + pathSep + fi.Name() + loc - } else if cfg.fsymln == true && fmode&os.ModeSymlink != 0 { + } else if cfg.fsymln && fmode&os.ModeSymlink != 0 { // create a reference with attributes matching the link target linktarget, _ := os.Readlink(dirname + pathSep + fi.Name()) if linktarget[:1] != pathSep { @@ -59,7 +59,7 @@ func createIndex(selector string) (string, error) { if check(err, "canonicalizePath "+path) == nil { lfi, err := os.Stat(path) if check(err, "Stat "+path) == nil { - fmode := fi.Mode() + fmode := lfi.Mode() fts := "[" + humanDate(lfi.ModTime()) + "]" if fmode.IsDir() { // link points to a directory @@ -79,7 +79,7 @@ func createIndex(selector string) (string, error) { } func replyErr(conn net.Conn, msg string) { - tracer.Print("sending error reply:" + msg) + tracer.Print("sending error reply: " + msg) s := "iGopher Meditation: " + msg + "\tErr\t" + cfg.fqdn + "\t" + cfg.port + "\r\n" nb, err := conn.Write([]byte(s)) if check(err, "replyErr Write ") != nil { @@ -111,7 +111,7 @@ func handleRequest(conn net.Conn) { return } // check for symbolic link - if cfg.fsymln == false { + if !cfg.fsymln { fi, err := os.Lstat(path) if check(err, "request path Lstat "+path) != nil { replyErr(conn, "404") @@ -134,6 +134,10 @@ func handleRequest(conn net.Conn) { tracer.Print("selector: '", selector, "'") var nbytes int64 if fmode.IsDir() { + if cfg.noidx { + replyErr(conn, "403") + return + } diridx, err := createIndex(selector) if check(err, "makeIndex "+path) != nil { replyErr(conn, "404") @@ -156,6 +160,7 @@ func handleRequest(conn net.Conn) { return } } else { + replyErr(conn, "404") return } tracer.Print(strconv.FormatInt(nbytes, 10) + " bytes sent.") -- 2.30.2