From dbfc20ede36fd5d798380bb69b7a0bd0a01479cc Mon Sep 17 00:00:00 2001 From: Rudi Date: Wed, 25 May 2022 12:51:14 -0400 Subject: [PATCH] Initial commit of fork --- go.mod | 1 + go.sum | 2 ++ main.go | 99 ++++++++++++++++++++++++++++++++++++++------------------- 3 files changed, 69 insertions(+), 33 deletions(-) diff --git a/go.mod b/go.mod index 4941747..1e83235 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.hugfreevikings.wtf/UniventionDNS2Go go 1.18 require ( + github.com/zcalusic/sysinfo v0.9.5 // indirect gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect gopkg.in/ldap.v2 v2.5.1 // indirect ) diff --git a/go.sum b/go.sum index 96272d8..0955011 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/zcalusic/sysinfo v0.9.5 h1:ivoHyj9aIAYkwzo1+8QgJ5s4oeE6Etx9FmZtqa4wJjQ= +github.com/zcalusic/sysinfo v0.9.5/go.mod h1:Z/gPVufBrFc8X5sef3m6kkw3r3nlNFp+I6bvASfvBZQ= gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d h1:TxyelI5cVkbREznMhfzycHdkp5cLA7DpE+GKjSslYhM= gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw= gopkg.in/ldap.v2 v2.5.1 h1:wiu0okdNfjlBzg6UWvd1Hn8Y+Ux17/u/4nlk4CQr6tU= diff --git a/main.go b/main.go index ebba80f..5738b19 100644 --- a/main.go +++ b/main.go @@ -3,22 +3,24 @@ package main import ( "bufio" "crypto/tls" - "encoding/json" "flag" "fmt" - "io/ioutil" "log" - "net/http" + "net" "os" + "os/user" + "runtime" "strings" + "github.com/zcalusic/sysinfo" "gopkg.in/ldap.v2" ) var ( configFile string - domain string host string + domain string + role string ldap_uri string ldap_default_bind_dn string @@ -31,13 +33,29 @@ type IP struct { } func init() { + test, err := os.Hostname() + if err == nil { + host = test + } flag.StringVar(&configFile, "c", "/etc/sssd/sssd.conf", "Path to SSSD Config.") - flag.StringVar(&host, "h", host, "Host ID for DNS (relativeDomainName)") - flag.StringVar(&domain, "d", "", "Domain for DNS (zoneName)") + flag.StringVar(&host, "h", "", "Host ID") + flag.StringVar(&domain, "d", "", "Domain for association") flag.Parse() } func main() { + current, err := user.Current() + if err != nil { + log.Fatal(err) + } + + if current.Uid != "0" { + log.Fatal("requires superuser privilege") + } + + var si sysinfo.SysInfo + + si.GetSysInfo() log.Printf("Using %+v as config file path", configFile) file, err := os.Open(configFile) @@ -93,10 +111,12 @@ func main() { log.Printf("Connected as read-only user, searching for DNS Record") // Search for the given username searchRequest := ldap.NewSearchRequest( - fmt.Sprintf("zoneName=%s,cn=dns,%s", ldap.EscapeFilter(domain), ldap.EscapeFilter(ldap_search_base)), + fmt.Sprintf("cn=computers,%s", ldap.EscapeFilter(ldap_search_base)), ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, - fmt.Sprintf("(relativeDomainName=%s)", ldap.EscapeFilter(host)), - []string{"dn","aRecord"}, + fmt.Sprintf("(cn=%s)", ldap.EscapeFilter(host)), + []string{"dn", "sn", "cn", "uid", "univentionObjectType", "description", "univentionOperatingSystem", + "univentionOperatingSystemVersion", "aRecord", "associatedDomain", "univentionNagiosEnabled", + "macAddress", "univentionInventoryNumber"}, nil, ) @@ -108,36 +128,49 @@ func main() { if len(sr.Entries) != 1 { log.Fatalf("Host does not exist or too many entries returned (%+v)", len(sr.Entries)) } - log.Printf("Record found for %s.%s: %+v", host, domain, sr.Entries[0]) - newIp := getip2() - if sr.Entries[0].GetAttributeValue("aRecord") == newIp { - log.Println("New IP is same as old IP, exiting gracefully.") - return + log.Printf("Record found for %s. %+v", host, sr.Entries[0]) + for _, attribute := range sr.Entries[0].Attributes { + log.Printf("%+v: %+v", attribute.Name, strings.Join(attribute.Values, ", ")) } - req := ldap.NewModifyRequest(sr.Entries[0].DN) - req.Replace("aRecord", []string{newIp}) - if err = l.Modify(req); err != nil { - log.Fatalf("Failed to modify DN: %s\n", err) + var macAddresses []string + var ipAddresses []string + test, err := net.Interfaces() + if err != nil { + log.Fatalf("Unable to get network interfaces: %+v", err) + } + for _, v := range test { + macAddresses = append(macAddresses, v.HardwareAddr.String()) + addrs, err := v.Addrs() + if err != nil { + log.Fatalf("Unable to get network interfaces: %+v", err) + } + for _, addr := range addrs { + switch v2 := addr.(type) { + case *net.IPNet: + ipAddresses = append(ipAddresses, v2.IP.String()) + case *net.IPAddr: + ipAddresses = append(ipAddresses, v2.IP.String()) + } + } } - log.Printf("Updated record successfully.") -} + req := ldap.NewModifyRequest(sr.Entries[0].DN) -func getip2() string { - req, err := http.Get("http://ip-api.com/json/") - if err != nil { - return err.Error() - } - defer req.Body.Close() + req.Replace("univentionOperatingSystem", []string{si.OS.Name}) + req.Replace("univentionOperatingSystemVersion", []string{fmt.Sprintf("%+v (%+v)", si.OS.Version, si.OS.Release)}) + req.Replace("macAddress", macAddresses) + req.Replace("aRecord", ipAddresses) + req.Replace("univentionInventoryNumber", []string{si.Node.MachineID}) + req.Replace("description", []string{si.Board.Name}) + req.Replace("sn", []string{host}) + req.Replace("cn", []string{host}) + req.Replace("associatedDomain", []string{domain}) + req.Replace("univentionObjectType", []string{fmt.Sprintf("computers/%s", runtime.GOOS)}) - body, err := ioutil.ReadAll(req.Body) - if err != nil { - return err.Error() + if err = l.Modify(req); err != nil { + log.Fatalf("Failed to modify DN: %s\n", err) } + log.Printf("Updated record successfully.") - var ip IP - json.Unmarshal(body, &ip) - log.Printf("Detected IP as %s", ip.Query) - return ip.Query }