2020-12-04 11:36:24 -07:00
|
|
|
package app_wkd
|
2020-11-30 11:54:37 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-03 12:32:24 -07:00
|
|
|
"crypto/sha1"
|
2020-11-30 11:54:37 -07:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-12-03 12:32:24 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
2020-11-30 11:54:37 -07:00
|
|
|
"net/http"
|
2020-12-03 12:32:24 -07:00
|
|
|
"net/url"
|
2020-11-30 11:54:37 -07:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/rs/zerolog/log"
|
2020-12-03 12:32:24 -07:00
|
|
|
"github.com/sour-is/crypto/openpgp"
|
|
|
|
"github.com/tv42/zbase32"
|
2020-12-04 11:36:24 -07:00
|
|
|
|
|
|
|
"github.com/sour-is/keyproofs/pkg/graceful"
|
|
|
|
"github.com/sour-is/keyproofs/pkg/opgp/entity"
|
2020-11-30 11:54:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type wkdApp struct {
|
|
|
|
path string
|
|
|
|
domain string
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
func New(ctx context.Context, path, domain string) (*wkdApp, error) {
|
2020-11-30 11:54:37 -07:00
|
|
|
log := log.Ctx(ctx)
|
2020-12-03 12:32:24 -07:00
|
|
|
log.Debug().Str("domain", domain).Str("path", path).Msg("NewWKDApp")
|
2020-11-30 11:54:37 -07:00
|
|
|
|
|
|
|
path = filepath.Clean(path)
|
2020-12-03 12:32:24 -07:00
|
|
|
app := &wkdApp{path: path, domain: domain}
|
2020-11-30 11:54:37 -07:00
|
|
|
err := app.CheckFiles(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
watch, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, typ := range []string{"keys"} {
|
|
|
|
err = watch.Add(filepath.Join(path, typ))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msg("startup wkd watcher")
|
|
|
|
wg := graceful.WaitGroup(ctx)
|
|
|
|
wg.Go(func() error {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Debug().Msg("shutdown wkd watcher")
|
|
|
|
return nil
|
|
|
|
case op := <-watch.Events:
|
|
|
|
log.Print(op)
|
|
|
|
switch op.Op {
|
|
|
|
case fsnotify.Create:
|
|
|
|
path = filepath.Dir(op.Name)
|
|
|
|
kind := filepath.Base(path)
|
|
|
|
name := filepath.Base(op.Name)
|
|
|
|
if err := app.createLinks(kind, name); err != nil {
|
2020-12-03 12:32:24 -07:00
|
|
|
log.Err(err).Send()
|
2020-11-30 11:54:37 -07:00
|
|
|
}
|
|
|
|
case fsnotify.Remove, fsnotify.Rename:
|
|
|
|
path = filepath.Dir(op.Name)
|
|
|
|
kind := filepath.Base(path)
|
|
|
|
name := filepath.Base(op.Name)
|
|
|
|
if err := app.removeLinks(kind, name); err != nil {
|
|
|
|
log.Error().Err(err).Send()
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
case err := <-watch.Errors:
|
2020-12-03 12:32:24 -07:00
|
|
|
log.Err(err).Send()
|
2020-11-30 11:54:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return app, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *wkdApp) CheckFiles(ctx context.Context) error {
|
|
|
|
log := log.Ctx(ctx)
|
|
|
|
|
2020-12-03 12:32:24 -07:00
|
|
|
for _, name := range []string{".links", "keys"} {
|
2020-11-30 11:54:37 -07:00
|
|
|
log.Debug().Msgf("mkdir: %s", filepath.Join(app.path, name))
|
|
|
|
err := os.MkdirAll(filepath.Join(app.path, name), 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Walk(app.path, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-03 12:32:24 -07:00
|
|
|
log.Debug().Msg(info.Name())
|
|
|
|
if path == app.path {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-30 11:54:37 -07:00
|
|
|
if info.IsDir() {
|
2020-12-03 12:32:24 -07:00
|
|
|
switch info.Name() {
|
|
|
|
case "keys":
|
|
|
|
return nil
|
2020-11-30 11:54:37 -07:00
|
|
|
}
|
2020-12-03 12:32:24 -07:00
|
|
|
return filepath.SkipDir
|
|
|
|
|
2020-11-30 11:54:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
path = filepath.Dir(path)
|
|
|
|
kind := filepath.Base(path)
|
|
|
|
name := info.Name()
|
|
|
|
|
|
|
|
log.Debug().Msgf("link: %s %s %s", app.path, kind, name)
|
|
|
|
|
|
|
|
return app.createLinks(kind, name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-03 12:32:24 -07:00
|
|
|
func (app *wkdApp) getRedirect(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
log := log.Ctx(ctx)
|
2020-11-30 11:54:37 -07:00
|
|
|
|
|
|
|
log.Print(r.Host)
|
|
|
|
|
|
|
|
hash := chi.URLParam(r, "hash")
|
|
|
|
|
|
|
|
if strings.ContainsRune(hash, '@') {
|
2020-12-03 12:32:24 -07:00
|
|
|
hash, domain := hashHuman(hash)
|
|
|
|
log.Debug().Str("hash", hash).Str("domain", domain).Msg("redirect")
|
|
|
|
if host, adv := getWKDDomain(ctx, domain); adv {
|
|
|
|
log.Debug().Str("host", host).Str("domain", domain).Bool("adv", adv).Msg("redirect")
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("https://%s/.well-known/openpgpkey/hu/%s/%s", host, domain, hash), http.StatusTemporaryRedirect)
|
|
|
|
} else {
|
|
|
|
log.Debug().Str("host", host).Str("domain", domain).Bool("adv", adv).Msg("redirect")
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("https://%s/.well-known/openpgpkey/hu/%s", domain, hash), http.StatusTemporaryRedirect)
|
2020-11-30 11:54:37 -07:00
|
|
|
}
|
2020-12-03 12:32:24 -07:00
|
|
|
|
2020-11-30 11:54:37 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-03 12:32:24 -07:00
|
|
|
writeText(w, http.StatusBadRequest, "Bad Request")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *wkdApp) get(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
log := log.Ctx(ctx)
|
|
|
|
|
|
|
|
log.Print(r.Host)
|
|
|
|
|
|
|
|
hash := chi.URLParam(r, "hash")
|
|
|
|
domain := chi.URLParam(r, "domain")
|
|
|
|
if domain == "" {
|
|
|
|
domain = app.domain
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.ContainsRune(hash, '@') {
|
|
|
|
hash, domain = hashHuman(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
fname := filepath.Join(app.path, ".links", strings.Join([]string{"keys", domain, hash}, "-"))
|
2020-11-30 11:54:37 -07:00
|
|
|
log.Debug().Msgf("path: %s", fname)
|
|
|
|
|
|
|
|
f, err := os.Open(fname)
|
|
|
|
if err != nil {
|
|
|
|
writeText(w, 500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(w, f)
|
|
|
|
if err != nil {
|
|
|
|
writeText(w, 500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *wkdApp) Routes(r *chi.Mux) {
|
2020-12-03 12:32:24 -07:00
|
|
|
r.MethodFunc("GET", "/wkd/{hash}", app.getRedirect)
|
|
|
|
r.MethodFunc("GET", "/key/{hash}", app.get)
|
|
|
|
r.MethodFunc("POST", "/pks/add", app.postKey)
|
2020-11-30 11:54:37 -07:00
|
|
|
r.MethodFunc("GET", "/.well-known/openpgpkey/hu/{hash}", app.get)
|
|
|
|
r.MethodFunc("GET", "/.well-known/openpgpkey/hu/{domain}/{hash}", app.get)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *wkdApp) createLinks(kind, name string) error {
|
|
|
|
if !strings.ContainsRune(name, '@') {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
src := filepath.Join("..", kind, name)
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
|
2020-12-03 12:32:24 -07:00
|
|
|
hash, domain := hashHuman(name)
|
2020-12-18 10:33:12 -07:00
|
|
|
link := filepath.Join(app.path, ".links", strings.Join([]string{kind, hash}, "-"))
|
2020-11-30 11:54:37 -07:00
|
|
|
err := app.replaceLink(src, link)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2020-12-03 12:32:24 -07:00
|
|
|
func hashHuman(name string) (string, string) {
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
parts := strings.SplitN(name, "@", 2)
|
|
|
|
hash := sha1.Sum([]byte(parts[0]))
|
|
|
|
lp := zbase32.EncodeToString(hash[:])
|
|
|
|
|
|
|
|
return lp, parts[1]
|
|
|
|
}
|
2020-11-30 11:54:37 -07:00
|
|
|
|
|
|
|
func (app *wkdApp) removeLinks(kind, name string) error {
|
|
|
|
if !strings.ContainsRune(name, '@') {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
|
2020-12-03 12:32:24 -07:00
|
|
|
hash, domain := hashHuman(name)
|
2020-12-18 10:33:12 -07:00
|
|
|
link := filepath.Join(app.path, ".links", strings.Join([]string{kind, hash}, "-"))
|
2020-11-30 11:54:37 -07:00
|
|
|
err := os.Remove(link)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *wkdApp) replaceLink(src, link string) error {
|
|
|
|
if dst, err := os.Readlink(link); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = os.Symlink(src, link)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if dst != src {
|
|
|
|
err = os.Remove(link)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Symlink(src, link)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-03 12:32:24 -07:00
|
|
|
|
|
|
|
func getWKDDomain(ctx context.Context, domain string) (string, bool) {
|
2020-12-03 13:40:39 -07:00
|
|
|
adv := "openpgpkey." + domain
|
|
|
|
_, err := net.DefaultResolver.LookupCNAME(ctx, adv)
|
2020-12-03 12:32:24 -07:00
|
|
|
if err == nil {
|
2020-12-03 13:40:39 -07:00
|
|
|
return adv, true
|
2020-12-03 12:32:24 -07:00
|
|
|
}
|
|
|
|
return domain, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *wkdApp) postKey(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
log := log.Ctx(ctx)
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
r.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Send()
|
|
|
|
writeText(w, http.StatusBadRequest, "ERR BODY")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
q, err := url.ParseQuery(string(body))
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Send()
|
|
|
|
writeText(w, http.StatusBadRequest, "ERR PARSE")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lis, err := openpgp.ReadArmoredKeyRing(strings.NewReader(q.Get("keytext")))
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Send()
|
|
|
|
writeText(w, http.StatusBadRequest, "ERR READ KEY")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
e, err := entity.GetOne(lis)
|
2020-12-03 12:32:24 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Send()
|
|
|
|
writeText(w, http.StatusBadRequest, "ERR ENTITY")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
fname := filepath.Join(app.path, "keys", e.Primary.Address)
|
2020-12-03 12:32:24 -07:00
|
|
|
|
|
|
|
f, err := os.Open(fname)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
out, err := os.Create(fname)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Send()
|
|
|
|
writeText(w, http.StatusInternalServerError, "ERR CREATE")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
err = e.Serialize(out)
|
2020-12-03 12:32:24 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Send()
|
|
|
|
writeText(w, http.StatusInternalServerError, "ERR WRITE")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("X-HKP-Status", "Created key")
|
|
|
|
writeText(w, http.StatusOK, "OK CREATED")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
current, err := openpgp.ReadKeyRing(f)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Send()
|
|
|
|
writeText(w, http.StatusInternalServerError, "ERR READ")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
compare, err := entity.GetOne(current)
|
2020-12-03 12:32:24 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Send()
|
|
|
|
writeText(w, http.StatusInternalServerError, "ERR PARSE")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
if e.Fingerprint != compare.Fingerprint {
|
2020-12-03 12:32:24 -07:00
|
|
|
w.Header().Set("X-HKP-Status", "Mismatch fingerprint")
|
|
|
|
writeText(w, http.StatusBadRequest, "ERR FINGERPRINT")
|
|
|
|
return
|
|
|
|
}
|
2020-12-04 11:36:24 -07:00
|
|
|
if e.SelfSignature == nil || compare.SelfSignature == nil {
|
2020-12-03 12:32:24 -07:00
|
|
|
w.Header().Set("X-HKP-Status", "Missing signature")
|
|
|
|
writeText(w, http.StatusBadRequest, "ERR SIGNATURE")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
log.Debug().Msgf("%v < %v", e.SelfSignature.CreationTime, compare.SelfSignature.CreationTime)
|
2020-12-03 12:32:24 -07:00
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
if !compare.SelfSignature.CreationTime.Before(e.SelfSignature.CreationTime) {
|
2020-12-03 12:32:24 -07:00
|
|
|
w.Header().Set("X-HKP-Status", "out of date")
|
|
|
|
writeText(w, http.StatusBadRequest, "ERR OUT OF DATE")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := os.Create(fname)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Send()
|
|
|
|
writeText(w, http.StatusInternalServerError, "ERR CREATE")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
err = e.Serialize(out)
|
2020-12-03 12:32:24 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Send()
|
|
|
|
writeText(w, http.StatusInternalServerError, "ERR WRITE")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("X-HKP-Status", "Updated key")
|
|
|
|
writeText(w, http.StatusOK, "OK UPDATED")
|
|
|
|
}
|
2020-12-04 11:36:24 -07:00
|
|
|
|
|
|
|
// WriteText writes plain text
|
|
|
|
func writeText(w http.ResponseWriter, code int, o string) {
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
w.WriteHeader(code)
|
|
|
|
_, _ = w.Write([]byte(o))
|
|
|
|
}
|