2016-11-11 21:48:00 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-11-13 19:50:44 +07:00
|
|
|
"crypto/rand"
|
2018-08-12 23:48:39 +07:00
|
|
|
"errors"
|
2017-11-15 04:54:29 +07:00
|
|
|
"fmt"
|
2016-11-13 19:50:44 +07:00
|
|
|
"math/big"
|
2017-11-14 05:42:30 +07:00
|
|
|
"os"
|
2016-11-11 21:48:00 +07:00
|
|
|
"regexp"
|
2016-11-13 19:50:44 +07:00
|
|
|
"strings"
|
2016-12-02 22:04:16 +07:00
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
2017-11-14 05:42:30 +07:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-11-11 21:48:00 +07:00
|
|
|
)
|
|
|
|
|
2017-11-15 04:54:29 +07:00
|
|
|
func jsonError(message string) []byte {
|
|
|
|
return []byte(fmt.Sprintf("{\"error\": \"%s\"}", message))
|
|
|
|
}
|
|
|
|
|
2018-08-12 22:49:17 +07:00
|
|
|
func fileIsAccessible(fname string) bool {
|
2017-11-14 05:42:30 +07:00
|
|
|
_, err := os.Stat(fname)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2018-08-12 22:49:17 +07:00
|
|
|
f, err := os.Open(fname)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
f.Close()
|
2017-11-14 05:42:30 +07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-08-12 22:49:17 +07:00
|
|
|
func readConfig(fname string) (DNSConfig, error) {
|
2016-11-17 00:15:36 +07:00
|
|
|
var conf DNSConfig
|
2018-08-12 22:49:17 +07:00
|
|
|
_, err := toml.DecodeFile(fname, &conf)
|
2018-08-12 23:48:39 +07:00
|
|
|
if err != nil {
|
|
|
|
// Return with config file parsing errors from toml package
|
|
|
|
return conf, err
|
|
|
|
}
|
|
|
|
return prepareConfig(conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareConfig checks that mandatory values exist, and can be used to set default values in the future
|
|
|
|
func prepareConfig(conf DNSConfig) (DNSConfig, error) {
|
|
|
|
if conf.Database.Engine == "" {
|
|
|
|
return conf, errors.New("missing database configuration option \"engine\"")
|
|
|
|
}
|
|
|
|
if conf.Database.Connection == "" {
|
|
|
|
return conf, errors.New("missing database configuration option \"connection\"")
|
|
|
|
}
|
2018-08-13 00:19:58 +07:00
|
|
|
|
|
|
|
// Default values for options added to config to keep backwards compatibility with old config
|
|
|
|
if conf.API.ACMECacheDir == "" {
|
|
|
|
conf.API.ACMECacheDir = "api-certs"
|
|
|
|
}
|
|
|
|
|
2018-08-12 23:48:39 +07:00
|
|
|
return conf, nil
|
2016-11-16 20:31:40 +07:00
|
|
|
}
|
|
|
|
|
2016-11-23 22:11:31 +07:00
|
|
|
func sanitizeString(s string) string {
|
2016-11-13 19:50:44 +07:00
|
|
|
// URL safe base64 alphabet without padding as defined in ACME
|
2021-01-11 22:31:09 +07:00
|
|
|
re, _ := regexp.Compile(`[^A-Za-z\-\_0-9]+`)
|
2016-11-13 19:50:44 +07:00
|
|
|
return re.ReplaceAllString(s, "")
|
|
|
|
}
|
|
|
|
|
2018-03-15 05:23:55 +07:00
|
|
|
func sanitizeIPv6addr(s string) string {
|
|
|
|
// Remove brackets from IPv6 addresses, net.ParseCIDR needs this
|
2021-01-11 22:31:09 +07:00
|
|
|
re, _ := regexp.Compile(`[\[\]]+`)
|
2018-03-15 05:23:55 +07:00
|
|
|
return re.ReplaceAllString(s, "")
|
|
|
|
}
|
|
|
|
|
2016-11-28 02:21:38 +07:00
|
|
|
func generatePassword(length int) string {
|
2016-11-13 19:50:44 +07:00
|
|
|
ret := make([]byte, length)
|
|
|
|
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_"
|
|
|
|
alphalen := big.NewInt(int64(len(alphabet)))
|
|
|
|
for i := 0; i < length; i++ {
|
2016-11-28 02:21:38 +07:00
|
|
|
c, _ := rand.Int(rand.Reader, alphalen)
|
2016-11-13 19:50:44 +07:00
|
|
|
r := int(c.Int64())
|
|
|
|
ret[i] = alphabet[r]
|
|
|
|
}
|
2016-11-28 02:21:38 +07:00
|
|
|
return string(ret)
|
2016-11-13 19:50:44 +07:00
|
|
|
}
|
|
|
|
|
2016-11-23 22:11:31 +07:00
|
|
|
func sanitizeDomainQuestion(d string) string {
|
2016-11-28 00:41:54 +07:00
|
|
|
dom := strings.ToLower(d)
|
|
|
|
firstDot := strings.Index(d, ".")
|
|
|
|
if firstDot > 0 {
|
|
|
|
dom = dom[0:firstDot]
|
2016-11-13 19:50:44 +07:00
|
|
|
}
|
|
|
|
return dom
|
|
|
|
}
|
|
|
|
|
2016-11-28 02:21:38 +07:00
|
|
|
func setupLogging(format string, level string) {
|
2016-11-28 04:21:46 +07:00
|
|
|
if format == "json" {
|
2016-11-26 20:42:35 +07:00
|
|
|
log.SetFormatter(&log.JSONFormatter{})
|
2016-11-23 22:11:31 +07:00
|
|
|
}
|
2016-11-28 03:09:13 +07:00
|
|
|
switch level {
|
2016-11-23 22:11:31 +07:00
|
|
|
default:
|
2016-11-26 20:42:35 +07:00
|
|
|
log.SetLevel(log.WarnLevel)
|
|
|
|
case "debug":
|
|
|
|
log.SetLevel(log.DebugLevel)
|
2016-11-23 22:11:31 +07:00
|
|
|
case "info":
|
2016-11-26 20:42:35 +07:00
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
case "error":
|
|
|
|
log.SetLevel(log.ErrorLevel)
|
2016-11-23 22:11:31 +07:00
|
|
|
}
|
2016-11-26 20:42:35 +07:00
|
|
|
// TODO: file logging
|
2016-11-23 22:11:31 +07:00
|
|
|
}
|
2016-11-28 02:21:38 +07:00
|
|
|
|
2016-12-02 22:04:16 +07:00
|
|
|
func getIPListFromHeader(header string) []string {
|
|
|
|
iplist := []string{}
|
|
|
|
for _, v := range strings.Split(header, ",") {
|
|
|
|
if len(v) > 0 {
|
|
|
|
// Ignore empty values
|
|
|
|
iplist = append(iplist, strings.TrimSpace(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return iplist
|
|
|
|
}
|