2016-07-13 21:10:10 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-07-25 14:24:22 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2016-07-13 21:10:10 +00:00
|
|
|
)
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
const (
|
|
|
|
// According to http://man7.org/linux/man-pages/man5/resolv.conf.5.html:
|
|
|
|
// "The search list is currently limited to six domains with a total of 256 characters."
|
|
|
|
maxDNSSearches = 6
|
|
|
|
)
|
|
|
|
|
|
|
|
func int64Ptr(i int64) *int64 {
|
|
|
|
return &i
|
|
|
|
}
|
|
|
|
|
|
|
|
func int32Ptr(i int32) *int32 {
|
|
|
|
return &i
|
|
|
|
}
|
|
|
|
|
|
|
|
func sPtr(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
2016-07-25 14:24:22 +00:00
|
|
|
func copyFile(src, dest string) error {
|
|
|
|
in, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
|
|
|
|
out, err := os.Create(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
_, err = io.Copy(out, in)
|
|
|
|
return err
|
2016-07-25 14:24:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeFile(path string) error {
|
|
|
|
if _, err := os.Stat(path); err == nil {
|
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-24 14:57:40 +00:00
|
|
|
func parseDNSOptions(servers, searches, options []string, path string) error {
|
2016-07-25 14:24:22 +00:00
|
|
|
nServers := len(servers)
|
|
|
|
nSearches := len(searches)
|
2016-10-24 14:57:40 +00:00
|
|
|
nOptions := len(options)
|
|
|
|
if nServers == 0 && nSearches == 0 && nOptions == 0 {
|
2016-07-25 14:24:22 +00:00
|
|
|
return copyFile("/etc/resolv.conf", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nSearches > maxDNSSearches {
|
|
|
|
return fmt.Errorf("DNSOption.Searches has more than 6 domains")
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if nSearches > 0 {
|
|
|
|
data := fmt.Sprintf("search %s\n", strings.Join(searches, " "))
|
|
|
|
_, err = f.Write([]byte(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if nServers > 0 {
|
|
|
|
data := fmt.Sprintf("nameserver %s\n", strings.Join(servers, "\nnameserver "))
|
|
|
|
_, err = f.Write([]byte(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 14:57:40 +00:00
|
|
|
if nOptions > 0 {
|
|
|
|
data := fmt.Sprintf("options %s\n", strings.Join(options, " "))
|
|
|
|
_, err = f.Write([]byte(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 14:24:22 +00:00
|
|
|
return nil
|
|
|
|
}
|