2024-01-15 19:24:48 -06:00
|
|
|
// figures out if your hostname is valid
|
|
|
|
// then checks if your DNS is setup correctly
|
|
|
|
package linuxstatus
|
|
|
|
|
|
|
|
import (
|
2024-11-01 21:41:46 -05:00
|
|
|
"fmt"
|
2024-01-15 19:24:48 -06:00
|
|
|
"io/ioutil"
|
2024-11-01 21:41:46 -05:00
|
|
|
"strings"
|
2024-01-21 02:23:47 -06:00
|
|
|
|
2024-11-01 21:41:46 -05:00
|
|
|
"go.wit.com/lib/gui/shell"
|
2024-01-15 19:24:48 -06:00
|
|
|
"go.wit.com/log"
|
|
|
|
|
|
|
|
// will try to get this hosts FQDN
|
|
|
|
"github.com/Showmax/go-fqdn"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ls *LinuxStatus) GetDomainName() string {
|
2024-01-21 02:23:47 -06:00
|
|
|
if !me.Ready() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return me.domainname.String()
|
2024-01-15 19:24:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *LinuxStatus) setDomainName() {
|
2024-01-21 02:23:47 -06:00
|
|
|
if !me.Ready() {
|
|
|
|
return
|
|
|
|
}
|
2024-01-15 19:24:48 -06:00
|
|
|
|
|
|
|
dn := run("domainname")
|
2024-01-21 02:23:47 -06:00
|
|
|
if me.domainname.String() != dn {
|
2024-01-15 19:24:48 -06:00
|
|
|
log.Log(CHANGE, "domainname has changed from", me.GetDomainName(), "to", dn)
|
2024-01-31 12:41:28 -06:00
|
|
|
me.domainname.SetText(dn)
|
2024-01-15 19:24:48 -06:00
|
|
|
me.changed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *LinuxStatus) GetHostname() string {
|
2024-01-21 02:23:47 -06:00
|
|
|
if !me.Ready() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return me.hostname.String()
|
2024-01-15 19:24:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *LinuxStatus) ValidHostname() bool {
|
2024-01-21 02:23:47 -06:00
|
|
|
if !me.Ready() {
|
2024-01-31 12:41:28 -06:00
|
|
|
log.Info("ValidHostname() not ready")
|
2024-01-21 02:23:47 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if me.hostnameStatus.String() == "WORKING" {
|
2024-01-15 19:24:48 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *LinuxStatus) setHostname(newname string) {
|
2024-01-21 02:23:47 -06:00
|
|
|
if !me.Ready() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if newname == me.hostname.String() {
|
2024-01-15 19:24:48 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Log(CHANGE, "hostname has changed from", me.GetHostname(), "to", newname)
|
2024-01-31 12:41:28 -06:00
|
|
|
me.hostname.SetText(newname)
|
2024-01-15 19:24:48 -06:00
|
|
|
me.changed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *LinuxStatus) GetHostShort() string {
|
2024-01-21 02:23:47 -06:00
|
|
|
if !me.Ready() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return me.hostshort.String()
|
2024-01-15 19:24:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *LinuxStatus) setHostShort() {
|
2024-01-21 02:23:47 -06:00
|
|
|
if !me.Ready() {
|
|
|
|
return
|
|
|
|
}
|
2024-01-15 19:24:48 -06:00
|
|
|
hshort := run("hostname -s")
|
2024-01-21 02:23:47 -06:00
|
|
|
if me.hostshort.String() != hshort {
|
|
|
|
log.Log(CHANGE, "hostname -s has changed from", me.hostshort.String(), "to", hshort)
|
2024-01-31 12:41:28 -06:00
|
|
|
me.hostshort.SetText(hshort)
|
2024-01-15 19:24:48 -06:00
|
|
|
me.changed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-01 21:41:46 -05:00
|
|
|
// getDomainName extracts the domain name from a given hostname
|
|
|
|
func getDomainName(hostname string) (string, error) {
|
|
|
|
parts := strings.Split(hostname, ".")
|
|
|
|
if len(parts) < 3 {
|
|
|
|
return "", fmt.Errorf("hostname '%s' is too short to extract a domain name", hostname)
|
|
|
|
}
|
|
|
|
// Join all parts except the first one, which is assumed to be a subdomain
|
|
|
|
domain := strings.Join(parts[1:], ".")
|
|
|
|
return domain, nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 19:24:48 -06:00
|
|
|
func lookupHostname() {
|
2024-01-21 02:23:47 -06:00
|
|
|
if !me.Ready() {
|
|
|
|
return
|
|
|
|
}
|
2024-01-15 19:24:48 -06:00
|
|
|
var err error
|
|
|
|
var hostfqdn string = "broken"
|
|
|
|
hostfqdn, err = fqdn.FqdnHostname()
|
2024-01-21 02:23:47 -06:00
|
|
|
if err != nil {
|
2024-01-15 19:24:48 -06:00
|
|
|
log.Error(err, "FQDN hostname error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Log(NET, "full hostname should be: ", hostfqdn)
|
|
|
|
|
|
|
|
me.setDomainName()
|
|
|
|
me.setHostShort()
|
|
|
|
|
|
|
|
// these are authoritative
|
|
|
|
// if they work wrong, your linux configuration is wrong.
|
|
|
|
// Do not complain.
|
|
|
|
// Fix your distro if your box is otherwise not working this way
|
2024-01-21 02:23:47 -06:00
|
|
|
hshort := me.GetHostShort() // from `hostname -s`
|
|
|
|
dn := me.GetDomainName() // from `domanname`
|
2024-01-15 19:24:48 -06:00
|
|
|
hostname := me.GetHostname() // from `hostname -f`
|
|
|
|
|
|
|
|
if hostfqdn != hostname {
|
|
|
|
log.Log(WARN, "hostname", hostname, "does not equal fqdn.FqdnHostname()", hostfqdn)
|
|
|
|
// TODO: figure out what is wrong
|
2024-11-01 21:41:46 -05:00
|
|
|
if dn == "(none)" {
|
|
|
|
realdn, err := getDomainName(hostfqdn)
|
|
|
|
if err == nil {
|
|
|
|
log.Log(WARN, "need to run: 'domainname", realdn, "' here")
|
|
|
|
me.changed = true
|
|
|
|
me.hostnameStatus.SetText("FIXING")
|
|
|
|
shell.RunPath("", []string{"domainname", realdn})
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
log.Log(WARN, "getDomainName() err =", err)
|
|
|
|
log.Log(WARN, "/etc/hostname is too short. let the user set the name here.")
|
|
|
|
// me.changed = true
|
|
|
|
me.hostnameStatus.SetText("INVALID DOMAIN NAME")
|
|
|
|
// return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Log(WARN, "don't know what to do here with domainname")
|
|
|
|
log.Log(WARN, "check that /etc/hostname is valid?")
|
|
|
|
// me.changed = true
|
|
|
|
me.hostnameStatus.SetText("UNKNOWN")
|
|
|
|
// return
|
2024-01-15 19:24:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var test string
|
|
|
|
test = hshort + "." + dn
|
|
|
|
|
|
|
|
me.setHostname(test)
|
|
|
|
|
2024-01-21 02:23:47 -06:00
|
|
|
if hostname != test {
|
2024-01-15 19:24:48 -06:00
|
|
|
log.Log(CHANGE, "hostname", hostname, "does not equal", test)
|
2024-01-21 02:23:47 -06:00
|
|
|
if me.hostnameStatus.String() != "BROKEN" {
|
2024-01-15 19:24:48 -06:00
|
|
|
log.Log(CHANGE, "hostname", hostname, "does not equal", test)
|
|
|
|
me.changed = true
|
2024-01-31 12:41:28 -06:00
|
|
|
me.hostnameStatus.SetText("BROKEN")
|
2024-01-15 19:24:48 -06:00
|
|
|
}
|
|
|
|
} else {
|
2024-01-21 02:23:47 -06:00
|
|
|
if me.hostnameStatus.String() != "WORKING" {
|
2024-01-15 19:24:48 -06:00
|
|
|
log.Log(CHANGE, "hostname", hostname, "is valid")
|
2024-01-31 12:41:28 -06:00
|
|
|
me.hostnameStatus.SetText("WORKING")
|
2024-01-15 19:24:48 -06:00
|
|
|
me.changed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns true if the hostname is good
|
|
|
|
// check that all the OS settings are correct here
|
|
|
|
// On Linux, /etc/hosts, /etc/hostname
|
2024-01-21 02:23:47 -06:00
|
|
|
//
|
|
|
|
// and domainname and hostname
|
2024-01-15 19:24:48 -06:00
|
|
|
func goodHostname() bool {
|
|
|
|
content, err := ioutil.ReadFile("/etc/hostname")
|
|
|
|
if err != nil {
|
|
|
|
// this needs to be a fixWindow() error
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hostname := string(content)
|
|
|
|
|
|
|
|
log.Log(NOW, "hostname =", hostname)
|
|
|
|
|
|
|
|
hs := run("hostname -s")
|
|
|
|
dn := run("domainname")
|
|
|
|
log.Log(NOW, "hostname short =", hs, "domainname =", dn)
|
|
|
|
|
|
|
|
tmp := hs + "." + dn
|
2024-01-21 02:23:47 -06:00
|
|
|
if hostname == tmp {
|
2024-01-15 19:24:48 -06:00
|
|
|
log.Log(NOW, "hostname seems to be good", hostname)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|