sub log ($){
	my $ho=shift;
	my $date=localtime;
	my $logfile="/tmp/ldapadmin";
	open (LOG,">>$logfile") or die "cant do";
	print LOG "$date-->$name-->$ho \n";
	close LOG;
}

sub error_page($){
	my $ho=shift;
	my $q=new CGI;
	print $q->header;
	print $q->start_html;
	print $q->h3("<font color=red>");
	print $q->center,$q->h1("$ho");
	print $q->start_form('POST','/admin/index.cgi');
	print $q->submit('s','Back to Admin Page');
	print $q->end_form;
	print $q->end_html;
	exit;}

sub existing_users{
	
	$ld = Net::LDAP->new("$ldapserver")or die"$@";
	$mesg3 = $ld->bind();
	$mesg3 = $ld->search(base =>"ou=$year,ou=$stustaff,ou=People,$dn",filter=> "objectClass=sambaSamAccount",attrs=>[$attr]);
	&log ("from inside cliebow.pm $year");
	my $entry3;
        while ($entry3 = $mesg3->shift_entry()) {
        push(@dude, scalar($entry3->get_value('cn')));}
	$mesg3 = $ld->unbind; 
	@dudes=sort @dude;
	return @dudes;}

sub allusers{
	
	$ld = Net::LDAP->new("$ldapserver")or die"$@";
	$mesg3 = $ld->bind();
	$mesg3 = $ld->search(base =>"ou=Staff,ou=People,$dn",filter=> "objectClass=posixAccount",attrs=>[$attr]);
	&log ("Search Staff");
	while ($entry3 = $mesg3->shift_entry()) {
        push(@alluser, scalar($entry3->get_value('cn')));}
####	
	$mesg3 = $ld->search(base =>"ou=Students,ou=People,$dn",filter=> "objectClass=posixAccount",attrs=>[$attr]);
	&log ("Search Students");
	while ($entry3 = $mesg3->shift_entry()) {
        push(@alluser, scalar($entry3->get_value('cn')));}


	$mesg3 = $ld->unbind; 
	@allusers=sort @alluser;
	return @allusers;}

sub all_groups{
	my $lda = Net::LDAP->new("$ldapserver")or die"$@";
	my $mesg2 = $lda->bind();
	$mesg2 = $lda->search(base =>"ou=Group,$dn",filter=>"objectClass=posixGroup",attrs=>[$attr]);
       my $entry2;
        while ($entry2 = $mesg2->shift_entry()){
    push (@allgroup, scalar ($entry2->get_value('cn')));}
	$mesg2 = $lda->unbind;
	@allgroups=sort @allgroup;
	return @allgroups;}

sub users_groups{  
	my $ldap = Net::LDAP->new("$ldapserver")or die"$@";
	my $mesg = $ldap->bind();
	$mesg = $ldap->search(base =>"ou=Group,$dn",filter=> "$filter",attrs=>[$attr]);
	my $max=$mesg->count;
	#$mesg->code && die $mesg->error;
        my $entry;
        while ($entry = $mesg->shift_entry()){
	push(@group, scalar ($entry->get_value('cn')));}
	$mesg = $ldap->unbind;
	@groups= sort @group;
	return @groups;}

sub classes{
my $ldap = Net::LDAP->new("$ldapserver")or die"$@";
	my $mesg = $ldap->bind();
	$mesg = $ldap->search(base =>"ou=People,$dn",filter=> 'objectClass=organizationalUnit',attrs=>['ou']);
	#$mesg->code && die $mesg->error;
        my $entry;
        while ($entry = $mesg->shift_entry()){
	push(@classe, scalar ($entry->get_value('ou')));}
	$mesg = $ldap->unbind;
	@classes= sort @classe;
	&log ("Getting all organizational Units");
	return @classes;} 

sub stustaff{
if ($year eq "BEM"){$stustaff = "Staff";}
elsif ($year eq "CCK"){$stustaff = "Staff";}
elsif ($year eq "EHS"){$stustaff = "Staff";}
elsif ($year eq "EMS"){$stustaff = "Staff";}
elsif ($year eq "HCTC"){$stustaff = "Staff";}
elsif ($year eq "SO"){$stustaff = "Staff";}
return $year;

sub getldap_pw{
	open (FILEHANDLE, "</etc/ldap.secret"); 
	$rootpw=readline(*FILEHANDLE);
	close (FILEHANDLE);
        chomp $rootpw;	}
}
sub get_dn{
$ldap = Net::LDAP->new("$ldapserver")or die "$@";
	$mesg = $ldap->bind();
	$mesg = $ldap->search(base =>"ou=People,$dn",filter=> "$filter",attrs=>[$attr]);
	$mesg->code && die $mesg->error;
	$nouser=$mesg->count;
	foreach $entry ($mesg->all_entries) {
		$dn=$entry->dn;}	
	$mesg = $ldap->unbind;
if ($nouser==0){	
&error_page("No such User");}
}
##Stolen from smbldap_tools.pm
sub read_parameter
  {
	my $line=shift;
	## check for a param = value
	if ($_=~/=/) {
	  my ($param,$val);
	  if ($_=~/\s*.*?\s*=\s*".*"/) {
		($param,$val) = /\s*(.*?)\s*=\s*"(.*)"/;
	  } elsif ($_=~/\s*.*?\s*=\s*'.*'/) {
		($param,$val) = /\s*(.*?)\s*=\s*'(.*)'/;
	  } else {
		($param,$val) = /\s*(.*?)\s*=\s*(.*)/;
	  }
	  return ($param,$val);
	}
  }

sub subst_configvar
  {
	my $value = shift;
	my $vars = shift;

	$value =~ s/\$\{([^}]+)\}/$vars->{$1} ? $vars->{$1} : $1/eg;
	return $value;
  }
sub read_conf
  {
	my %smb;
	open (CONFIGFILE, "</etc/samba/smb.conf") || die "Unable to open $smbldap_conf for reading !\n";
	while (<CONFIGFILE>) {
	  chomp($_);
	  ## throw away comments
	  next if ( /^\s*#/ || /^\s*$/ || /^\s*\;/);
	  ## check for a param = value
	  my ($parameter,$value)=read_parameter($_);
	  $value = &subst_configvar($value, \%smb);
	  $smb{$parameter}=$value;
	
	}
	close (CONFIGFILE);
	&log ("reading smb.conf ");
	return %smb;}

sub students{
my $ldap = Net::LDAP->new("$ldapserver")or die"$@";
	my $mesg = $ldap->bind();
	$mesg = $ldap->search(base =>"ou=Students,ou=People,$dn",filter=> 'objectClass=organizationalUnit',attrs=>['ou']);
	#$mesg->code && die $mesg->error;
        my $entry;
        while ($entry = $mesg->shift_entry()){
	push(@student, scalar ($entry->get_value('ou')));}
	$mesg = $ldap->unbind;
	@students= sort @student;
	&log ("Getting All Student Names");
	return @students;} 

sub staff{
my $ldap = Net::LDAP->new("$ldapserver")or die"$@";
	my $mesg = $ldap->bind();
	$mesg = $ldap->search(base =>"ou=Staff,ou=People,$dn",filter=> 'objectClass=organizationalUnit',attrs=>['ou']);
	#$mesg->code && die $mesg->error;
        my $entry;
        while ($entry = $mesg->shift_entry()){
	push(@staf, scalar ($entry->get_value('ou')));}
	$mesg = $ldap->unbind;
	&log ("looking up staff- $dn  ");
	@staff= sort @staf;
	&log ("Getting All Staff Names ");
	return @staff;} 

sub getsid{
my $ldap = Net::LDAP->new("$ldapserver")or die"$@";
	my $mesg = $ldap->bind();
	$mesg = $ldap->search(base =>"$dn",filter=> 'objectClass=sambaDomain',attrs=>['sambaSID']);
	#$mesg->code && die $mesg->error;
        foreach my $entry ($mesg->all_entries) {$sid=$entry->get_value('sambaSID')};
#$sid=$entry->sambaSID;
&log ("looking up sid- $dn $sid ");
	$mesg = $ldap->unbind;
	return $sid;
	}

sub lastuid{
my $ldap = Net::LDAP->new("$ldapserver")or die"$@";
	my $mesg = $ldap->bind();
	my %mySMB = &read_conf();
	$wg=$mySMB{"workgroup"};	
	$mesg = $ldap->search(base =>"sambaDomainName=$wg,$dn",filter=> 'objectClass=sambaDomain',attrs=>['uidNumber']);
	#$mesg->code && die $mesg->error;
        foreach my $entry ($mesg->all_entries) {$lastuid=$entry->get_value('uidNumber')};
	&log ("looking up lastuid- $dn $lastuid");
	$mesg = $ldap->unbind;
	return $lastuid;
	}
sub updatelastuid{
	&log ("Amending lastuid- dn->$dn lastuid->$lastuid");
	#my $updatelastuid = $lastuid +1;
	my %mySMB = &read_conf();
	$wg=$mySMB{"workgroup"};
	&log ("wg--> $wg");
	my $d="sambaDomainName=$wg,$dn";
	&log ("here is sambaDomainName $d");
	$ldap = Net::LDAP->new("$ldapserver") or die "$@";
	$ldap->bind("cn=root,$dn",password=>"$rootpw");
	$mesg = $ldap->modify($d,changes=>
		[replace=> [uidNumber=>"$lastuid"]]);	
	$mesg = $ldap->unbind;
	&log ("New updatelastuid wg--> $wg $dn $lastuid $updatelastuid d--> $d");
}

sub unique_user
{
my $ldap = Net::LDAP->new("$ldapserver")or die"$@";
	my $mesg = $ldap->bind();
	$mesg = $ldap->search(base =>"ou=People,$dn",filter=> "cn=$name",attrs=>['cn']);
	$isuser=$mesg->count;
	$mesg = $ldap->unbind;
	&log ("from unique_user $isuser $name");
	return $isuser;
}

sub mkhomedir{
	if (!-d "/home/$year/$name"){mkdir "/home/$year/$name";
	#chown $name: "/home/$year/$name";
	copy ("/etc/skel/*,/home/$year/$name");
}
	if (-d "/home/$year/$name"){&log("from mkhomedir exists");}	

}

sub readfile($){
	my $file=shift;
	open (FILEHANDLE, "<$file"); 
	foreach my $line (<FILEHANDLE>){
	chomp $line;
	push  @newstudents,"$line";
	next;
	close FILEHANDLE;
	return @newstudents}
	&log ("Inside readfile");
}
sub addrecord {
	$ldap = Net::LDAP->new("$ldapserver") or die "$@";
	$ldap->bind("$rootdn",password=>"$rootpw");
	$mesg = $ldap->add($user,attrs=> 
	['sambaLMPassword'=>'1461E145A27938EDAAD3B435B51404EE',	
	'sambaPrimaryGroupSID'=>"$groupsid",
	'displayName'=>"$gecos",
	'givenName'=>"$firstname",
	'objectClass'=>['top','inetOrgPerson','posixAccount','sambaSamAccount'],
	'cn'=>"$username",
	'userPassword'=>'{SHA}rj6QQrUzqVP1AnjSPSwN+wuRWWM=',
	'sambaLogonTime'=>'0',
	'sambaHomeDrive'=>'P:',
	'uid'=>"$username",
	'mail'=>"$username\@ellsworthschools.org",
	'uidNumber'=>"$lastuid",
	'sambaLogoffTime'=>'2147483647',
	'sambaPwdLastSet'=>'1128449072',
	'loginShell'=>'/bin/bash',
	'sambaAcctFlags'=>'[UX]',
	'sambaProfilePath'=>"\\\\puffin\\profiles\\$username",
	'gidNumber'=>"$year",
	'sambaPwdMustChange'=>'0',
	'sambaNTPassword'=>"4B08AE009324CE883AD7C42735F8CE2C",
	'sambaPwdCanChange'=>'1128449072',
	'gecos'=>"$gecos",
	'sambaSID'=>"$groupsid",
	'description'=>"$gecos",
	'homeDirectory'=>"/home/$year/$username",
	'sambaKickoffTime'=>"2147483647",
	'sn'=>"$lastname",
	'sambaHomePath'=>'\\\\puffin\\homes',
	]
	);
	$mesg = $ldap->unbind;
	 $lastuid = $lastuid +1;
	&log ("inside addrecord $lastuid"); 
}	

1; 
