* Fixed a regression bug in handling of symbolic links.
authorUrban Wallasch <urban.wallasch@freenet.de>
Sat, 6 Apr 2019 07:55:40 +0000 (09:55 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Sat, 6 Apr 2019 07:55:40 +0000 (09:55 +0200)
* Added option to disable directory indexes, in preparation for index page support.
* Some minor adjustments.

config.go
gogopherd.go

index 9fd4001562d1f8ea1463eef9b506dc358a9f1705..8b0564d425d0116e0105f29d71d7ddd5ccdc5071 100644 (file)
--- 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)
index a4e95ce9fe10ece7e5f88726f017352ccfe8ea04..ee5a6187969ca51d4bef21409f5c32b913602e8c 100644 (file)
@@ -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.")