Browse Source

Initial commit of fork

master
Gregory Rudolph 2 years ago
parent
commit
dbfc20ede3
Signed by: rudi
GPG Key ID: EF64F3CBD1A1EBDD
  1. 1
      go.mod
  2. 2
      go.sum
  3. 99
      main.go

1
go.mod

@ -3,6 +3,7 @@ module git.hugfreevikings.wtf/UniventionDNS2Go
go 1.18 go 1.18
require ( require (
github.com/zcalusic/sysinfo v0.9.5 // indirect
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect
gopkg.in/ldap.v2 v2.5.1 // indirect gopkg.in/ldap.v2 v2.5.1 // indirect
) )

2
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 h1:TxyelI5cVkbREznMhfzycHdkp5cLA7DpE+GKjSslYhM=
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw= 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= gopkg.in/ldap.v2 v2.5.1 h1:wiu0okdNfjlBzg6UWvd1Hn8Y+Ux17/u/4nlk4CQr6tU=

99
main.go

@ -3,22 +3,24 @@ package main
import ( import (
"bufio" "bufio"
"crypto/tls" "crypto/tls"
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"net/http" "net"
"os" "os"
"os/user"
"runtime"
"strings" "strings"
"github.com/zcalusic/sysinfo"
"gopkg.in/ldap.v2" "gopkg.in/ldap.v2"
) )
var ( var (
configFile string configFile string
domain string
host string host string
domain string
role string
ldap_uri string ldap_uri string
ldap_default_bind_dn string ldap_default_bind_dn string
@ -31,13 +33,29 @@ type IP struct {
} }
func init() { 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(&configFile, "c", "/etc/sssd/sssd.conf", "Path to SSSD Config.")
flag.StringVar(&host, "h", host, "Host ID for DNS (relativeDomainName)") flag.StringVar(&host, "h", "", "Host ID")
flag.StringVar(&domain, "d", "", "Domain for DNS (zoneName)") flag.StringVar(&domain, "d", "", "Domain for association")
flag.Parse() flag.Parse()
} }
func main() { 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) log.Printf("Using %+v as config file path", configFile)
file, err := os.Open(configFile) file, err := os.Open(configFile)
@ -93,10 +111,12 @@ func main() {
log.Printf("Connected as read-only user, searching for DNS Record") log.Printf("Connected as read-only user, searching for DNS Record")
// Search for the given username // Search for the given username
searchRequest := ldap.NewSearchRequest( 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, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(relativeDomainName=%s)", ldap.EscapeFilter(host)), fmt.Sprintf("(cn=%s)", ldap.EscapeFilter(host)),
[]string{"dn","aRecord"}, []string{"dn", "sn", "cn", "uid", "univentionObjectType", "description", "univentionOperatingSystem",
"univentionOperatingSystemVersion", "aRecord", "associatedDomain", "univentionNagiosEnabled",
"macAddress", "univentionInventoryNumber"},
nil, nil,
) )
@ -108,36 +128,49 @@ func main() {
if len(sr.Entries) != 1 { if len(sr.Entries) != 1 {
log.Fatalf("Host does not exist or too many entries returned (%+v)", len(sr.Entries)) 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]) log.Printf("Record found for %s. %+v", host, sr.Entries[0])
newIp := getip2() for _, attribute := range sr.Entries[0].Attributes {
if sr.Entries[0].GetAttributeValue("aRecord") == newIp { log.Printf("%+v: %+v", attribute.Name, strings.Join(attribute.Values, ", "))
log.Println("New IP is same as old IP, exiting gracefully.")
return
} }
req := ldap.NewModifyRequest(sr.Entries[0].DN)
req.Replace("aRecord", []string{newIp}) var macAddresses []string
if err = l.Modify(req); err != nil { var ipAddresses []string
log.Fatalf("Failed to modify DN: %s\n", err) 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.Replace("univentionOperatingSystem", []string{si.OS.Name})
req, err := http.Get("http://ip-api.com/json/") req.Replace("univentionOperatingSystemVersion", []string{fmt.Sprintf("%+v (%+v)", si.OS.Version, si.OS.Release)})
if err != nil { req.Replace("macAddress", macAddresses)
return err.Error() req.Replace("aRecord", ipAddresses)
} req.Replace("univentionInventoryNumber", []string{si.Node.MachineID})
defer req.Body.Close() 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 = l.Modify(req); err != nil {
if err != nil { log.Fatalf("Failed to modify DN: %s\n", err)
return err.Error()
} }
log.Printf("Updated record successfully.")
var ip IP
json.Unmarshal(body, &ip)
log.Printf("Detected IP as %s", ip.Query)
return ip.Query
} }

Loading…
Cancel
Save