#!/usr/bin/perl
#
# Tool to export netgroups from LDAP to /etc/netgroup format.

use strict;
use warnings;
use Net::LDAP;
use Debian::Edu;

use vars qw($ldaphost $base $debug);

$debug = 0;

$ldaphost = $ARGV[0] || find_ldap_server() || 'ldap';
$base     = $ARGV[1] || find_ldap_base($ldaphost)
    || 'dc=skole,dc=skolelinux,dc=no';

my $ldap = new Net::LDAP($ldaphost)
    || die "Unable to connect to LDAP host '$ldaphost'";

my $mesg = $ldap->bind ; # as anonymous

die("Unable to bind anonymously: $mesg->error") unless ( 0 == $mesg->code );

$mesg = $ldap->search( base   => $base,
                       filter => '(objectClass=nisNetgroup)' );

$mesg->code && die $mesg->error;

foreach my $entry ($mesg->all_entries) {
    my $name    = $entry->get_value("cn");


    print "$name";
    map { print " $_" } $entry->get_attribute("memberNisNetgroup");
    map { print " $_" } $entry->get_attribute("nisNetgroupTriple");
    print "\n";
    $entry->dump if $debug;
}

$ldap->unbind;   # take down session
