#!/usr/bin/perl
#
# QLANADMIN - Jim Dutton - 04/09/99,04/30/99,05/10/99,06/08/99,01/05/00
#                          02/17/00
# query an LDAP database for specific LAN Admin data, and
# display results in somewhat nice, line-mode format
#
# Requires: Perl (obviously) and LDAPSEARCH (UMich, Netscape, OpenLDAP)
#
$numargs = scalar @ARGV;
if ($numargs == 0){
	print "\nUsage (one of the following):\n\n";
	print "    qlanadmin <subdomain name>\n";
	print "    qlanadmin <organization/department name>\n";
	print "    qlanadmin <router subnet number>\n";
	print "    qlanadmin <organizational unit name>\n";
        print "    qlanadmin <AppleTalk Zone>\n\n";
	exit;
}
$s = shift @ARGV;

$ldapcmd = "/usr/local/bin/ldapsearch"; $ldaphost = "-h <LDAP server>";
$ldapbase = "-b o=<org>,c=US";
$filter = "(|(dc=$s)(subnet=$s)(o=$s)(ou=$s)(appletalkzone=$s))";
$attributes = "lanadmin postofficebox telephonenumber dnsadmin o";
$org_seen = 0;
$dnsadmin = "None"; $cn = "Unknown"; $telephonenumber = "Unknown";
$mailaddr  = "Unknown"; $organization = "Unknown";

$result = `$ldapcmd $ldaphost $ldapbase "$filter" $attributes`;
if ($? != 0) {
	print "\n*** LDAPSEARCH ERROR (rc=$?) ***\n";
	print "--- search terminated ---\n\n"; exit;
}

@results = split(/\n/,$result);
$numargs = scalar @results;

for ($i=0;$i<$numargs;$i++) {
  ($attr,$remainder) = split(/=/,@results[$i],2);

  if ($attr eq "") {
	write;
	$dnsadmin = "None"; $cn = "Unknown"; $telephonenumber = "Unknown";
	$mailaddr  = "Unknown"; $organization = "Unknown"; $org_seen = 0;
  }

  elsif ($attr eq "dc") {
     ($subdomain,$remainder) = split(/,/,$remainder,2);
     ($attr,$remainder) = split(/=/,$remainder,2);
     ($value,$remainder) = split(/,/,$remainder,2);
     if ($value ne "AppleTalk") {$protocol = "IP"}
     else {$protocol = $value}
  }

  elsif ($attr eq "lanadmin") {
	if (substr($remainder,0,3) eq "cn=") {
           ($cn,$remainder) = split(/,/,$remainder,2);
           $cn = substr($cn,3);}
	else {$cn = "Unknown";}
  }

  elsif ($attr eq "postofficebox") {
        $mailaddr = $remainder;
  }

  elsif ($attr eq "telephonenumber") {
        $telephonenumber = $remainder;
  }

  elsif ($attr eq "dnsadmin") {
        ($dnsadmin,$remainder) = split(/,/,$remainder,2);
        $dnsadmin = substr($dnsadmin,3);
        if ($dnsadmin eq "") {$dnsadmin = "Unknown";}
  }

  elsif ($attr eq "o") {
        if ($org_seen == 0) {
            $organization = $remainder;
#           ($organization,$remainder) = split(/,/,$remainder,2);
            $org_seen = 1;
        }
  }
}
format =

==================================================================
Subdomain        : @<<<<<<<<<<<<<<<<
$subdomain
Network Protocol : @<<<<<<<<<<<<<<<<
$protocol
LAN Administrator: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$cn
Department       : @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$organization
E-mail Address   : @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$mailaddr
Telephone Number : @<<<<<<<<<<<<<
$telephonenumber
DNS Administrator: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$dnsadmin
==================================================================

.
write;
exit;
