2021-05-17 07:50:45 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-05-20 06:23:18 -06:00
|
|
|
"crypto/tls"
|
2021-05-17 07:50:45 -06:00
|
|
|
"embed"
|
|
|
|
"encoding/csv"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2021-05-17 15:59:48 -06:00
|
|
|
"regexp"
|
2021-05-17 08:01:31 -06:00
|
|
|
"strings"
|
2021-12-09 05:16:00 -07:00
|
|
|
"sync"
|
2021-05-19 21:14:22 -06:00
|
|
|
"text/template"
|
2021-05-17 07:50:45 -06:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"golang.org/x/net/webdav"
|
2021-12-10 15:49:52 -07:00
|
|
|
"golang.org/x/term"
|
2021-05-17 07:50:45 -06:00
|
|
|
"suah.dev/protect"
|
|
|
|
)
|
|
|
|
|
2021-05-19 21:14:22 -06:00
|
|
|
// Landing will be used to fill our landing template
|
|
|
|
type Landing struct {
|
|
|
|
User string
|
|
|
|
URL string
|
|
|
|
}
|
|
|
|
|
|
|
|
const landingPage = `
|
|
|
|
<h1>Hello{{if .User}} {{.User}}{{end}}! Welcome to widdler!</h1>
|
|
|
|
|
|
|
|
<p>To create a new TiddlyWiki html file, simply append an html file name to the URL in the address bar!</p>
|
|
|
|
|
|
|
|
<h3>For example:</h3>
|
|
|
|
|
|
|
|
<a href="{{.URL}}">{{.URL}}</a>
|
|
|
|
|
|
|
|
<p>This will create a new wiki called "<b>wiki.html</b>"</p>
|
2021-05-17 07:50:45 -06:00
|
|
|
|
2021-05-19 21:14:22 -06:00
|
|
|
<p>After creating a wiki, this message will be replaced by a list of your wiki files.</p>
|
|
|
|
`
|
|
|
|
|
|
|
|
var (
|
2021-12-09 06:28:37 -07:00
|
|
|
twFile = "empty-5.2.1.html"
|
2021-05-19 21:14:22 -06:00
|
|
|
|
2021-12-09 06:28:37 -07:00
|
|
|
//go:embed empty-5.2.1.html
|
2021-05-19 21:14:22 -06:00
|
|
|
tiddly embed.FS
|
|
|
|
templ *template.Template
|
|
|
|
)
|
2021-05-17 07:50:45 -06:00
|
|
|
|
2021-12-09 05:16:00 -07:00
|
|
|
type userHandler struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
dav *webdav.Handler
|
|
|
|
fs http.Handler
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:27:27 -07:00
|
|
|
type userHandlers struct {
|
|
|
|
list []userHandler
|
|
|
|
mu sync.RWMutex
|
|
|
|
}
|
2021-12-09 05:16:00 -07:00
|
|
|
|
2021-12-10 06:27:27 -07:00
|
|
|
func (u *userHandlers) find(name string) *userHandler {
|
|
|
|
for i := range u.list {
|
|
|
|
if u.list[i].name == name {
|
|
|
|
return &u.list[i]
|
2021-12-09 05:16:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-05-19 20:30:45 -06:00
|
|
|
}
|
|
|
|
|
2021-05-17 07:50:45 -06:00
|
|
|
var (
|
2021-12-08 05:42:48 -07:00
|
|
|
auth string
|
2021-05-20 06:23:18 -06:00
|
|
|
davDir string
|
|
|
|
fullListen string
|
2021-05-20 06:47:38 -06:00
|
|
|
genHtpass bool
|
2021-12-09 05:16:00 -07:00
|
|
|
handlers userHandlers
|
2021-05-20 06:23:18 -06:00
|
|
|
listen string
|
|
|
|
passPath string
|
|
|
|
tlsCert string
|
|
|
|
tlsKey string
|
|
|
|
users map[string]string
|
2021-12-10 15:50:07 -07:00
|
|
|
version bool
|
|
|
|
build string
|
2021-05-17 07:50:45 -06:00
|
|
|
)
|
|
|
|
|
2021-07-08 19:05:28 -06:00
|
|
|
var pledges = "stdio wpath rpath cpath tty inet dns unveil"
|
|
|
|
|
2021-05-17 07:50:45 -06:00
|
|
|
func init() {
|
|
|
|
users = make(map[string]string)
|
|
|
|
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2021-05-18 16:34:49 -06:00
|
|
|
flag.StringVar(&davDir, "wikis", dir, "Directory of TiddlyWikis to serve over WebDAV.")
|
2021-05-19 21:14:22 -06:00
|
|
|
flag.StringVar(&listen, "http", "localhost:8080", "Listen on")
|
2021-05-20 06:23:18 -06:00
|
|
|
flag.StringVar(&tlsCert, "tlscert", "", "TLS certificate.")
|
|
|
|
flag.StringVar(&tlsKey, "tlskey", "", "TLS key.")
|
2021-05-17 07:50:45 -06:00
|
|
|
flag.StringVar(&passPath, "htpass", fmt.Sprintf("%s/.htpasswd", dir), "Path to .htpasswd file..")
|
2021-12-08 05:42:48 -07:00
|
|
|
flag.StringVar(&auth, "auth", "basic", "Enable HTTP Basic Authentication.")
|
2021-05-20 06:47:38 -06:00
|
|
|
flag.BoolVar(&genHtpass, "gen", false, "Generate a .htpasswd file or add a new entry to an existing file.")
|
2021-12-10 15:50:07 -07:00
|
|
|
flag.BoolVar(&version, "v", false, "Show version and exit.")
|
2021-05-17 07:50:45 -06:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// These are OpenBSD specific protections used to prevent unnecessary file access.
|
2021-05-20 18:33:35 -06:00
|
|
|
_ = protect.Unveil(passPath, "rwc")
|
2021-05-17 07:50:45 -06:00
|
|
|
_ = protect.Unveil(davDir, "rwc")
|
|
|
|
_ = protect.Unveil("/etc/ssl/cert.pem", "r")
|
|
|
|
_ = protect.Unveil("/etc/resolv.conf", "r")
|
2021-07-08 19:05:28 -06:00
|
|
|
_ = protect.Pledge(pledges)
|
2021-05-17 07:50:45 -06:00
|
|
|
|
2021-05-19 21:14:22 -06:00
|
|
|
templ, err = template.New("landing").Parse(landingPage)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2021-05-17 07:50:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func authenticate(user string, pass string) bool {
|
|
|
|
htpass, exists := users[user]
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(htpass), []byte(pass))
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func logger(f http.HandlerFunc) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
f(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createEmpty(path string) error {
|
|
|
|
_, fErr := os.Stat(path)
|
|
|
|
if os.IsNotExist(fErr) {
|
|
|
|
log.Printf("creating %q\n", path)
|
|
|
|
twData, _ := tiddly.ReadFile(twFile)
|
2021-12-06 06:07:35 -07:00
|
|
|
wErr := ioutil.WriteFile(path, twData, 0600)
|
2021-05-17 07:50:45 -06:00
|
|
|
if wErr != nil {
|
|
|
|
return wErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-20 06:47:38 -06:00
|
|
|
func prompt(prompt string, secure bool) (string, error) {
|
|
|
|
var input string
|
|
|
|
fmt.Print(prompt)
|
|
|
|
|
|
|
|
if secure {
|
2021-12-10 15:49:52 -07:00
|
|
|
b, err := term.ReadPassword(int(os.Stdin.Fd()))
|
2021-05-20 06:47:38 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
input = string(b)
|
|
|
|
} else {
|
2021-12-09 05:16:00 -07:00
|
|
|
_, err := fmt.Scanln(&input)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-05-20 06:47:38 -06:00
|
|
|
}
|
|
|
|
return input, nil
|
|
|
|
}
|
|
|
|
|
2021-12-11 16:07:52 -07:00
|
|
|
func addHandler(u, uPath string) {
|
|
|
|
handlers.list = append(handlers.list, userHandler{
|
|
|
|
name: u,
|
|
|
|
dav: &webdav.Handler{
|
|
|
|
LockSystem: webdav.NewMemLS(),
|
|
|
|
FileSystem: webdav.Dir(uPath),
|
|
|
|
Logger: func(r *http.Request, err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fs: http.FileServer(http.Dir(uPath)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-17 07:50:45 -06:00
|
|
|
func main() {
|
2021-12-10 15:50:07 -07:00
|
|
|
if version {
|
|
|
|
fmt.Println(build)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2021-05-20 06:47:38 -06:00
|
|
|
if genHtpass {
|
|
|
|
user, err := prompt("Username: ", false)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pass, err := prompt("Password: ", true)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(pass), 11)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2021-12-10 15:50:28 -07:00
|
|
|
f, err := os.OpenFile(filepath.Clean(passPath), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
2021-05-20 06:47:38 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := f.WriteString(fmt.Sprintf("%s:%s\n", user, hash)); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2021-12-09 05:16:00 -07:00
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2021-05-20 06:47:38 -06:00
|
|
|
fmt.Printf("Added %q to %q\n", user, passPath)
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2021-07-08 19:05:28 -06:00
|
|
|
pledges, _ = protect.ReducePledges(pledges, "tty")
|
2021-05-20 06:47:38 -06:00
|
|
|
|
2021-05-20 18:33:35 -06:00
|
|
|
// drop to only read on passPath
|
|
|
|
_ = protect.Unveil(passPath, "r")
|
2021-07-08 19:05:28 -06:00
|
|
|
pledges, _ = protect.ReducePledges(pledges, "unveil")
|
2021-05-20 18:33:35 -06:00
|
|
|
|
2021-05-20 06:47:38 -06:00
|
|
|
_, fErr := os.Stat(passPath)
|
|
|
|
if os.IsNotExist(fErr) {
|
2021-12-08 05:42:48 -07:00
|
|
|
if auth == "basic" || auth == "header" {
|
2021-05-20 06:47:38 -06:00
|
|
|
fmt.Println("No .htpasswd file found!")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
} else {
|
2021-12-10 15:50:28 -07:00
|
|
|
p, err := os.Open(filepath.Clean(passPath))
|
2021-05-20 06:47:38 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ht := csv.NewReader(p)
|
|
|
|
ht.Comma = ':'
|
|
|
|
ht.Comment = '#'
|
|
|
|
ht.TrimLeadingSpace = true
|
|
|
|
|
2021-12-10 06:27:27 -07:00
|
|
|
entries, err := ht.ReadAll()
|
2021-12-09 05:16:00 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:27:27 -07:00
|
|
|
err = p.Close()
|
2021-05-20 06:47:38 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, parts := range entries {
|
|
|
|
users[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 05:42:48 -07:00
|
|
|
if auth == "basic" || auth == "header" {
|
2021-05-19 20:30:45 -06:00
|
|
|
for u := range users {
|
|
|
|
uPath := path.Join(davDir, u)
|
2021-12-11 16:07:52 -07:00
|
|
|
addHandler(u, uPath)
|
2021-05-19 20:30:45 -06:00
|
|
|
}
|
|
|
|
} else {
|
2021-12-11 16:07:52 -07:00
|
|
|
addHandler("", davDir)
|
2021-05-17 07:50:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", logger(func(w http.ResponseWriter, r *http.Request) {
|
2021-05-19 20:30:45 -06:00
|
|
|
user, pass := "", ""
|
|
|
|
var ok bool
|
2021-05-17 15:59:48 -06:00
|
|
|
|
2021-05-17 08:01:31 -06:00
|
|
|
if strings.Contains(r.URL.Path, ".htpasswd") {
|
2021-05-19 20:30:45 -06:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent directory traversal
|
|
|
|
if strings.Contains(r.URL.Path, "..") {
|
|
|
|
http.NotFound(w, r)
|
2021-05-17 08:01:31 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-08 05:42:48 -07:00
|
|
|
if auth == "basic" {
|
2021-05-19 20:30:45 -06:00
|
|
|
user, pass, ok = r.BasicAuth()
|
2021-12-08 05:42:48 -07:00
|
|
|
if !(ok && authenticate(user, pass)) {
|
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="widdler"`)
|
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if auth == "header" {
|
|
|
|
var prefix = "Auth"
|
|
|
|
for name, values := range r.Header {
|
|
|
|
if strings.HasPrefix(name, prefix) {
|
|
|
|
user = strings.TrimLeft(name, prefix)
|
|
|
|
pass = values[0]
|
|
|
|
ok = true
|
2021-12-08 05:48:13 -07:00
|
|
|
break
|
2021-12-08 05:42:48 -07:00
|
|
|
}
|
|
|
|
}
|
2021-12-08 05:48:13 -07:00
|
|
|
|
2021-05-18 16:34:49 -06:00
|
|
|
if !(ok && authenticate(user, pass)) {
|
2021-05-19 20:30:45 -06:00
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="widdler"`)
|
2021-05-18 16:34:49 -06:00
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2021-05-17 07:50:45 -06:00
|
|
|
}
|
|
|
|
|
2021-12-10 06:27:27 -07:00
|
|
|
handlers.mu.RLock()
|
2021-12-09 05:16:00 -07:00
|
|
|
handler := handlers.find(user)
|
2021-12-10 06:27:27 -07:00
|
|
|
handlers.mu.RUnlock()
|
|
|
|
|
2021-12-09 05:16:00 -07:00
|
|
|
if handler == nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.mu.Lock()
|
|
|
|
|
|
|
|
defer handler.mu.Unlock()
|
|
|
|
|
2021-05-19 21:14:22 -06:00
|
|
|
userPath := path.Join(davDir, user)
|
|
|
|
fullPath := path.Join(davDir, user, r.URL.Path)
|
2021-05-19 20:30:45 -06:00
|
|
|
|
2021-05-19 21:14:22 -06:00
|
|
|
_, dErr := os.Stat(userPath)
|
2021-05-19 20:30:45 -06:00
|
|
|
if os.IsNotExist(dErr) {
|
2021-05-19 21:14:22 -06:00
|
|
|
mErr := os.Mkdir(userPath, 0700)
|
2021-05-19 20:30:45 -06:00
|
|
|
if mErr != nil {
|
|
|
|
http.Error(w, mErr.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2021-05-17 15:59:48 -06:00
|
|
|
}
|
2021-05-19 20:30:45 -06:00
|
|
|
}
|
2021-05-17 15:59:48 -06:00
|
|
|
|
|
|
|
isHTML, err := regexp.Match(`\.html$`, []byte(r.URL.Path))
|
2021-05-17 07:50:45 -06:00
|
|
|
if err != nil {
|
2021-05-17 15:59:48 -06:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-05-17 07:50:45 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-17 15:59:48 -06:00
|
|
|
if isHTML {
|
|
|
|
// HTML files will be created or sent back
|
2021-05-19 21:14:22 -06:00
|
|
|
err := createEmpty(fullPath)
|
2021-05-17 15:59:48 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-05-19 20:30:45 -06:00
|
|
|
handler.dav.ServeHTTP(w, r)
|
2021-05-17 15:59:48 -06:00
|
|
|
} else {
|
|
|
|
// Everything else is browsable
|
2021-05-19 21:14:22 -06:00
|
|
|
entries, err := os.ReadDir(userPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entries) > 0 {
|
2021-12-12 09:08:46 -07:00
|
|
|
if r.URL.Path == "/" {
|
|
|
|
// If we have entries, and are serving up /, check for
|
|
|
|
// index.html and redirect to that if it exists. We redirect
|
|
|
|
// because net/http handles index.html magically for FileServer
|
|
|
|
_, fErr := os.Stat(filepath.Clean(path.Join(userPath, "index.html")))
|
|
|
|
if !os.IsNotExist(fErr) {
|
2021-12-12 14:54:26 -07:00
|
|
|
http.Redirect(w, r, "/index.html", http.StatusMovedPermanently)
|
2021-12-12 09:08:46 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-05-19 21:14:22 -06:00
|
|
|
handler.fs.ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
l := Landing{
|
2021-05-20 06:23:18 -06:00
|
|
|
URL: fmt.Sprintf("%s/wiki.html", fullListen),
|
2021-05-19 21:14:22 -06:00
|
|
|
}
|
|
|
|
if user != "" {
|
|
|
|
l.User = user
|
|
|
|
}
|
|
|
|
err = templ.ExecuteTemplate(w, "landing", l)
|
2021-05-19 21:15:46 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2021-05-19 21:14:22 -06:00
|
|
|
}
|
2021-05-17 07:50:45 -06:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
s := http.Server{
|
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", listen)
|
|
|
|
if err != nil {
|
2021-05-20 06:23:18 -06:00
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tlsCert != "" && tlsKey != "" {
|
|
|
|
fullListen = fmt.Sprintf("https://%s", listen)
|
|
|
|
|
|
|
|
s.TLSConfig = &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
|
|
|
|
PreferServerCipherSuites: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Listening for HTTPS on 'https://%s'", listen)
|
|
|
|
log.Fatalln(s.ServeTLS(lis, tlsCert, tlsKey))
|
|
|
|
} else {
|
|
|
|
fullListen = fmt.Sprintf("http://%s", listen)
|
|
|
|
|
|
|
|
log.Printf("Listening for HTTP on 'http://%s'", listen)
|
|
|
|
log.Fatalln(s.Serve(lis))
|
2021-05-17 07:50:45 -06:00
|
|
|
}
|
2021-12-12 09:08:46 -07:00
|
|
|
}
|