You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
4.4 KiB
176 lines
4.4 KiB
package main |
|
|
|
import ( |
|
"bufio" |
|
"crypto/tls" |
|
"flag" |
|
"fmt" |
|
"log" |
|
"net" |
|
"os" |
|
"os/user" |
|
"runtime" |
|
"strings" |
|
|
|
"github.com/zcalusic/sysinfo" |
|
"gopkg.in/ldap.v2" |
|
) |
|
|
|
var ( |
|
configFile string |
|
host string |
|
domain string |
|
role string |
|
|
|
ldap_uri string |
|
ldap_default_bind_dn string |
|
ldap_default_authtok string |
|
ldap_search_base string |
|
) |
|
|
|
type IP struct { |
|
Query string |
|
} |
|
|
|
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 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) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
defer file.Close() |
|
|
|
scanner := bufio.NewScanner(file) |
|
// optionally, resize scanner's capacity for lines over 64K, see next example |
|
for scanner.Scan() { |
|
line := scanner.Text() |
|
mod := &ldap_uri |
|
switch { |
|
case strings.Contains(line, "ldap_uri"): |
|
mod = &ldap_uri |
|
case strings.Contains(line, "ldap_default_bind_dn"): |
|
mod = &ldap_default_bind_dn |
|
case strings.Contains(line, "ldap_default_authtok"): |
|
mod = &ldap_default_authtok |
|
case strings.Contains(line, "ldap_search_base"): |
|
mod = &ldap_search_base |
|
|
|
default: |
|
continue |
|
} |
|
*mod = strings.Split(line, "= ")[1] |
|
|
|
} |
|
|
|
if err := scanner.Err(); err != nil { |
|
log.Fatal(err) |
|
} |
|
log.Printf("Connecting to %s", ldap_uri) |
|
ldap_uri = strings.ReplaceAll(ldap_uri, "ldap://", "") |
|
l, err := ldap.Dial("tcp", ldap_uri) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
defer l.Close() |
|
log.Printf("Connected to %s, reconnecting with TLS", ldap_uri) |
|
// Reconnect with TLS |
|
err = l.StartTLS(&tls.Config{InsecureSkipVerify: true}) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
log.Printf("Binding as %s", ldap_default_bind_dn) |
|
// First bind with a read only user |
|
err = l.Bind(ldap_default_bind_dn, ldap_default_authtok) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
log.Printf("Connected as read-only user, searching for DNS Record") |
|
// Search for the given username |
|
searchRequest := ldap.NewSearchRequest( |
|
fmt.Sprintf("cn=computers,%s", ldap.EscapeFilter(ldap_search_base)), |
|
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, |
|
fmt.Sprintf("(cn=%s)", ldap.EscapeFilter(host)), |
|
[]string{"dn", "sn", "cn", "uid", "univentionObjectType", "description", "univentionOperatingSystem", |
|
"univentionOperatingSystemVersion", "aRecord", "associatedDomain", "univentionNagiosEnabled", |
|
"macAddress", "univentionInventoryNumber"}, |
|
nil, |
|
) |
|
|
|
sr, err := l.Search(searchRequest) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
|
|
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. %+v", host, sr.Entries[0]) |
|
for _, attribute := range sr.Entries[0].Attributes { |
|
log.Printf("%+v: %+v", attribute.Name, strings.Join(attribute.Values, ", ")) |
|
} |
|
|
|
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()) |
|
} |
|
} |
|
} |
|
|
|
req := ldap.NewModifyRequest(sr.Entries[0].DN) |
|
|
|
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)}) |
|
|
|
if err = l.Modify(req); err != nil { |
|
log.Fatalf("Failed to modify DN: %s\n", err) |
|
} |
|
log.Printf("Updated record successfully.") |
|
|
|
}
|
|
|