2019-11-07 08:05:34 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-07 18:00:07 -07:00
|
|
|
"encoding/csv"
|
2019-11-07 08:05:34 -07:00
|
|
|
"flag"
|
2019-11-07 18:00:07 -07:00
|
|
|
"fmt"
|
2019-11-07 08:05:34 -07:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-11-07 18:00:07 -07:00
|
|
|
"time"
|
2019-11-07 08:05:34 -07:00
|
|
|
|
2019-11-07 18:00:07 -07:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2019-11-07 08:05:34 -07:00
|
|
|
"golang.org/x/net/webdav"
|
2020-05-08 07:04:02 -06:00
|
|
|
"suah.dev/gavin/pu"
|
2019-11-07 08:05:34 -07:00
|
|
|
)
|
|
|
|
|
2019-11-07 18:00:07 -07:00
|
|
|
var (
|
|
|
|
davDir string
|
|
|
|
listen string
|
|
|
|
passPath string
|
2020-05-08 07:04:02 -06:00
|
|
|
davPath string
|
2019-11-07 18:00:07 -07:00
|
|
|
staticDir string
|
|
|
|
users map[string]string
|
|
|
|
)
|
2019-11-07 08:05:34 -07:00
|
|
|
|
|
|
|
func init() {
|
2019-11-07 18:00:07 -07:00
|
|
|
users = make(map[string]string)
|
2019-11-07 08:05:34 -07:00
|
|
|
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
if err != nil {
|
2020-05-08 07:04:02 -06:00
|
|
|
log.Fatalln(err)
|
2019-11-07 08:05:34 -07:00
|
|
|
}
|
|
|
|
|
2020-05-07 21:54:39 -06:00
|
|
|
flag.StringVar(&davDir, "davdir", dir, "Directory to serve over WebDAV.")
|
2019-11-07 18:00:07 -07:00
|
|
|
flag.StringVar(&listen, "http", ":8080", "Listen on")
|
|
|
|
flag.StringVar(&passPath, "htpass", fmt.Sprintf("%s/.htpasswd", dir), "Path to .htpasswd file..")
|
2020-05-08 07:04:02 -06:00
|
|
|
flag.StringVar(&davPath, "davpath", "/dav/", "Directory containing files to serve over WebDAV.")
|
|
|
|
flag.StringVar(&staticDir, "static", dir, "Directory to serve static resources from. Served at '/'.")
|
2019-11-07 08:05:34 -07:00
|
|
|
flag.Parse()
|
|
|
|
|
2020-05-08 07:04:02 -06:00
|
|
|
// These are OpenBSD specific protections used to prevent un-necesary file access.
|
|
|
|
pu.Unveil(staticDir, "r")
|
|
|
|
pu.Unveil(passPath, "r")
|
|
|
|
pu.Unveil(davDir, "rwc")
|
|
|
|
err = pu.UnveilBlock()
|
2019-11-07 08:05:34 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-11-07 18:00:07 -07:00
|
|
|
|
|
|
|
p, err := os.Open(passPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-03-24 07:09:08 -06:00
|
|
|
defer p.Close()
|
2019-11-07 18:00:07 -07:00
|
|
|
|
|
|
|
ht := csv.NewReader(p)
|
|
|
|
ht.Comma = ':'
|
|
|
|
ht.Comment = '#'
|
|
|
|
ht.TrimLeadingSpace = true
|
|
|
|
|
|
|
|
entries, err := ht.ReadAll()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, parts := range entries {
|
|
|
|
users[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validate(user string, pass string) bool {
|
|
|
|
htpass, exists := users[user]
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(htpass), []byte(pass))
|
2020-03-24 07:09:08 -06:00
|
|
|
return err == nil
|
2019-11-07 08:05:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
wdav := &webdav.Handler{
|
2020-05-08 07:04:02 -06:00
|
|
|
Prefix: davPath,
|
2019-11-07 08:05:34 -07:00
|
|
|
LockSystem: webdav.NewMemLS(),
|
|
|
|
FileSystem: webdav.Dir(davDir),
|
|
|
|
Logger: func(r *http.Request, err error) {
|
2019-11-07 18:00:07 -07:00
|
|
|
n := time.Now()
|
|
|
|
fmt.Printf("%s [%s] \"%s %s %s\" %03d\n",
|
|
|
|
r.RemoteAddr,
|
|
|
|
n.Format(time.RFC822Z),
|
|
|
|
r.Method,
|
|
|
|
r.URL.Path,
|
|
|
|
r.Proto,
|
|
|
|
r.ContentLength,
|
|
|
|
)
|
2019-11-07 08:05:34 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/", http.FileServer(http.Dir(staticDir)))
|
2020-05-08 07:04:02 -06:00
|
|
|
mux.HandleFunc(davPath, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2019-11-07 18:00:07 -07:00
|
|
|
user, pass, ok := r.BasicAuth()
|
2020-03-24 07:09:08 -06:00
|
|
|
if !(ok && validate(user, pass)) {
|
2019-11-07 08:05:34 -07:00
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="davfs"`)
|
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wdav.ServeHTTP(w, r)
|
|
|
|
}))
|
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", listen)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := http.Server{Handler: mux}
|
2019-11-07 18:00:07 -07:00
|
|
|
|
|
|
|
log.Printf("Listening on '%s'", listen)
|
2019-11-07 08:05:34 -07:00
|
|
|
log.Panic(s.Serve(lis))
|
|
|
|
}
|