/usr/bin/sitesummary2ldapdhcp is in debian-edu-config 1.702.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | #!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
use Socket;
use Net::LDAP; # From libnet-ldap-perl debian package
use SiteSummary;
use Debian::Edu qw(prompt4password find_ldap_server find_ldap_base);
my $debug = 0;
my %opts;
getopts("adi:u:", \%opts) || usage(1);
$debug = 1 if $opts{d};
my $server = $ARGV[0] || find_ldap_server() || "ldap";
my $base = $ARGV[1] || find_ldap_base($server)
|| "dc=skole,dc=skolelinux,dc=no";
my $systembase = "ou=netdevices,ou=systems,$base";
my $dhcpbase = "cn=dhcp,cn=tjener,ou=servers,ou=systems,$base";
my $userfilter = $opts{u} || "(cn=admin)";
sub usage {
my $retval = shift;
print <<EOF;
sitesummary2ldapdhcp [FQDN to ldap server] [LDAP base to use]
Update or add machine objects to LDAP based on sitesummary
information.
-a Add new hosts as well as update existing hosts.
-d Enable debug output.
-i ID Handle host with the given sitesummary ID only.
-u userfiler Filter used to find LDAP user used to update LDAP.
EOF
exit $retval;
}
my $retval = 0;
my %hostinfo;
if ($opts{i}) {
host_handler($opts{i});
} else {
for_all_hosts(\&host_handler);
}
my $ldap = Net::LDAP->new( $server ) or die "$@";
$ldap->start_tls();
my $binddn = (find_ldap_objects($ldap, $userfilter))[0]->dn();
print "\nEnter password if you want to activate these changes, and ^c to abort.\n\n";
print "Connecting to LDAP as $binddn\n";
my $bindpw = prompt4password('enter password: ', -echo => '*');
my $mesg = $ldap->bind($binddn, password => $bindpw);
die "Unable to bind to LDAP server: " . $mesg->error if $mesg->code;
for my $hostid (keys %hostinfo) {
my ($fqdn, $ipaddr, $macref, $usedhcp) = @{$hostinfo{$hostid}};
add_or_update_machine($ldap, $fqdn, $ipaddr, $macref, $usedhcp);
}
$mesg = $ldap->unbind;
exit $retval;
sub host_handler {
my $hostid = shift;
my $ipaddr = SiteSummary::get_primary_ip_address($hostid);
my $fqdn = scalar gethostbyaddr(inet_aton($ipaddr), AF_INET);
my $macref = [(get_ether_hwaddr($hostid))];
# Update MAC and IP if name has been changed
if ($fqdn && $fqdn !~ m/^auto-mac-/) {
print "info: Updating machine $fqdn [$ipaddr] id $hostid.\n";
$hostinfo{$hostid} = [$fqdn, $ipaddr, $macref, 1];
} elsif ($opts{a}) {
my $mac = get_primary_ether_hwaddr($macref);
$mac =~ s/[^0-9a-f-]/-/gi;
$fqdn = "auto-mac-$mac";
print "info: Create GOsa machine for $fqdn [$ipaddr] id $hostid.\n";
$hostinfo{$hostid} = [$fqdn, $ipaddr, $macref, 0];
} else {
print "info: Ignoring new machine [$ipaddr] id $hostid.\n";
}
}
sub get_ether_hwaddr {
my $hostid = shift;
my $path = get_filepath_current($hostid, "/system/ifconfig-a");
if (open(FILE, "<", $path)) {
my $sysinfo = 0;
my @hwaddr = ();
while (<FILE>) {
chomp;
if (m/Link encap:Ethernet HWaddr (.+\S)\s*$/) {
push(@hwaddr, $1);
}
}
close(FILE);
return @hwaddr;
} else {
return undef;
}
}
sub get_primary_ether_hwaddr {
# FIXME How to handle several MAC addresses?
my $macref = shift;
my $mac = (@{$macref})[0];
}
sub find_ldap_objects {
my ($ldap, $filter) = @_;
my $mesg = $ldap->search(
base => $base,
filter => $filter,
);
return $mesg->all_entries;
}
sub add_or_update_gosasystem {
my ($ldap, $fqdn, $ipaddr, $macref) = @_;
my $mac = get_primary_ether_hwaddr($macref);
my ($hostname, $dnsdomain) = split(/\./, $fqdn, 2);
my $cn = "cn=$hostname,$systembase";
my $mesg = $ldap->search(
base => $base,
filter => "(&(|(objectClass=goServer)(objectClass=device))(|(cn=$hostname)(macAddress=$mac)))",
);
print "info: search found " . $mesg->count . " objects\n" if $debug;
if (0 == $mesg->count) {
# Create
my $attr = [
'objectClass' => ['top', 'device', 'ipHost', 'ieee802Device'],
'cn' => $hostname,
'macAddress' => $mac,
'ipHostNumber' => $ipaddr,
];
my $result = $ldap->add($cn, attr => $attr);
if ($result->code) {
my $err = $result->error;
print STDERR "error: Unable to add LDAP object $cn - $err\n";
}
} elsif (1 == $mesg->count) {
# Update
foreach my $entry ($mesg->all_entries) {
$entry->dump if $debug;
$entry->replace(
'macAddress' => $mac,
'ipHostNumber' => $ipaddr,
);
my $result = $entry->update($ldap);
if ($result->code) {
my $err = $result->error;
print STDERR "error: Unable to update LDAP object $cn - $err\n";
}
}
} else {
print STDERR "error: Not sure how to handle several objects for the same host\n";
}
}
sub add_or_update_dhcp {
my ($ldap, $fqdn, $ipaddr, $macref) = @_;
my $mac = get_primary_ether_hwaddr($macref);
my ($hostname, $dnsdomain) = split(/\./, $fqdn, 2);
my $cn = "cn=$hostname,$dhcpbase";
my $mesg = $ldap->search(
base => $base,
filter => "(&(objectClass=dhcpHost)(cn=$hostname))"
);
print "info: dhcpHost search found " . $mesg->count . " objects\n" if $debug;
if (0 == $mesg->count) {
# Create
my $attr = [
'objectClass' => 'dhcpHost',
'cn' => $hostname,
'dhcpHWAddress' => "ethernet $mac",
'dhcpStatements' => "fixed-address $fqdn"
];
my $result = $ldap->add($cn, attr => $attr);
if ($result->code) {
my $err = $result->error;
print STDERR "error: Unable to add DHCP LDAP object $cn - $err\n";
}
} elsif (1 == $mesg->count) {
# Update
foreach my $entry ($mesg->all_entries) {
$entry->dump if $debug;
$entry->replace(
'dhcpHWAddress' => "ethernet $mac",
'dhcpStatements' => "fixed-address $fqdn"
);
my $result = $entry->update($ldap);
if ($result->code) {
my $err = $result->error;
print STDERR "error: Unable to update DHCP LDAP object $cn - $err\n";
}
}
} else {
print STDERR "error: Not sure how to handle this\n";
}
}
sub add_or_update_machine {
my ($ldap, $fqdn, $ipaddr, $macref, $usedhcp) = @_;
print "Updating host entry for $fqdn\n" if $debug;
add_or_update_gosasystem($ldap, $fqdn, $ipaddr, $macref);
if ($usedhcp) {
add_or_update_dhcp($ldap, $fqdn, $ipaddr, $macref);
}
}
|