#!/usr/bin/perl -w
# ------------------------------------------------------------
# script:  ldapchfn.pl
# Author:  Brad Marshall (bmarshal@pisoftware.com)
# License: GPL
# Date:    20000207
#
# Purpose: Changes a users finger information, in LDAP
#
# Copyright (c) 2000 Plugged In Software Pty Ltd.  All rights reserved.

# TODO

# Modules
use strict;
use Net::LDAP;
use Getopt::Std;
use Term::ReadKey;

use vars qw( $opt_f $opt_o $opt_p $opt_h $opt_u $opt_v );

# Variables
my($gecos);
my($fullname,$office,$officephone,$homephone);
my($inputname, $inputoffice, $inputofficephone, $inputhomephone);
my($username);
my($uid);
my($dn);

my($root) = "dc=pisoftware,dc=com";
my($host) = "ldap.staff.plugged.com.au";
my($manager) = "cn=Manager,$root";
my $exception = 0;

# Signal handlers
$SIG{'INT'} = $SIG{'QUIT'} = $SIG{'HUP'} = sub { $exception=1; };

#  chfn  [ -f full-name ]  [ -o office ] [ -p office-phone ] [ -h home-phone ] [ -u ]
#  [ -v ] [ username ]

# $ chfn
# Changing finger information for bmarshal.
# Password: 
# Name [Bradley Marshall]: 
# Office []: 
# Office Phone []: 
# Home Phone []: 

# Handle the command line options
getopts('f:o:p:h:u:v:');

if (! $ARGV[0]) {
	$uid = $>;
	print "\$uid = $uid\n";

	# We don't have any options, ask for all the info

	# Search for the gecos
} else {
	if ($> != 0) {
		die "Sorry, must be root to change other peoples GECOS";
	}
	$username = $ARGV[0];
	$uid = &finduid;
} 

$gecos = &getgecos;
($fullname,$office,$officephone,$homephone) = split(",", $gecos);
print "full = `$fullname' office = `$office' officephone = `$officephone' homephone = `$homephone'\n";

exit 0;

$dn = "uid=$username,ou=People,$root";
print "\$dn = $dn\n";

print "Name: [$fullname]: ";
chomp($inputname = <>);
print "Office: [$office]: ";
chomp($inputoffice = <>);
print "Office Phone: [$officephone]: ";
chomp($inputofficephone = <>);
print "Home Phone: [$homephone]: ";
chomp($inputhomephone = <>);

if (($fullname eq $inputname) && ($office eq $inputoffice) && ($officephone eq $inputofficephone) && ($homephone eq $inputhomephone)) {
	die "No changes";
}

exit 0;

print "Please enter LDAP Managers password: ";

ReadMode 'noecho';
my $password = ReadLine 0;
chomp $password;
ReadMode 'normal';
print "\n";

my($ldap) = Net::LDAP->new($host) or die "Can't bind to ldap: $!\n";

# Bind to ldap as manager
$ldap->bind(
			dn       => $manager,
			password => $password,
		);

if ($exception) {
	die "Caught a signal";
} else {

	#$result->code && warn "failed to add entry: ", $result->error;
}

# Subroutines

my(%results);

sub getgecos {
	my($ldap) = Net::LDAP->new($host) or die "Can't bind to ldap: $!\n";

	$ldap->bind;

	# Search for all accounts
	print "gecos \$uid = $uid\n";
	my($mesg) = $ldap->search( base => $root,
                               filter => "(uidnumber=$uid)"
                             );

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

	my($entry);
	# Loop thru all the groups
	foreach $entry ($mesg->entries) {
		my($gecos) = $entry->get('gecos')->[0];
		$username = $entry->get('uid')->[0];
		print "\$gecos = $gecos\n";
		$ldap->unbind;
		return $gecos;
	}
}

sub finduid {
	my($uid);

	my($ldap) = Net::LDAP->new($host) or die "Can't bind to ldap: $!\n";

	$ldap->bind;

	# Search for all accounts
	#print "gecos \$uid = $uid\n";
	my($mesg) = $ldap->search( base => $root,
                               filter => "(uid=$username)"
                             );

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

	my($entry);
	# Loop thru all the groups
	foreach $entry ($mesg->entries) {
		$uid = $entry->get('uidnumber')->[0];
		$ldap->unbind;
		return $uid;
	}
}

