From de9cc37738950a48ecb2c7a6b464f72b706f6a1d Mon Sep 17 00:00:00 2001 From: Thomas Preisner Date: Sun, 12 Sep 2021 01:11:45 +0200 Subject: [PATCH] web.go: adjust order of functions to match call order in RequestHandler --- web.go | 58 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/web.go b/web.go index cd60001..0d7caf2 100644 --- a/web.go +++ b/web.go @@ -30,6 +30,35 @@ func isAuthenticated(cfg *Config, r *http.Request) *User { return user } +// returns api-response on failure and RRConfig on success +func verifyHostname(cfg *Config, user *User, hostname string) (string, *RRConfig) { + if len(hostname) <= 0 { + return "nohost", nil + } + + // check whether the authenticated user is allowed to update the dns record + _, ok := user.records[hostname] + if !ok { + return "badauth", nil + } + + // this should not fail as it is verified in LoadConfig, but better be safe + entry, ok := cfg.rrconfigs[hostname] + if !ok { + return "nohost", nil + } + + // TODO: return notfqdn -> differentiate between 'hostname doesnt exist' and + // 'hostname is not fqdn' + + // again, this should not fail since 'hostname' was the key used for + // cfg.rrconfigs to acquire the entry + if hostname != entry.Recordname { + return "nohost", nil + } + return "", entry +} + func getIpAddress(r *http.Request) net.IP { addr := r.URL.Query().Get("myip") if len(addr) > 0 { @@ -61,35 +90,6 @@ func getIpAddress(r *http.Request) net.IP { return net.ParseIP(addr) } -// returns api-response on failure and RRConfig on success -func verifyHostname(cfg *Config, user *User, hostname string) (string, *RRConfig) { - if len(hostname) <= 0 { - return "nohost", nil - } - - // check whether the authenticated user is allowed to update the dns record - _, ok := user.records[hostname] - if !ok { - return "badauth", nil - } - - // this should not fail as it is verified in LoadConfig, but better be safe - entry, ok := cfg.rrconfigs[hostname] - if !ok { - return "nohost", nil - } - - // TODO: return notfqdn -> differentiate between 'hostname doesnt exist' and - // 'hostname is not fqdn' - - // again, this should not fail since 'hostname' was the key used for - // cfg.rrconfigs to acquire the entry - if hostname != entry.Recordname { - return "nohost", nil - } - return "", entry -} - func RequestHandler(cfg *Config) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { user := isAuthenticated(cfg, r)