#!/usr/local/bin/perl
#
# $Id: userrevert,v 1.2 2002/11/07 20:17:14 visick Exp $
#
# userrevert - assigns a user the default shell and random password
#
# Copyright (C) 2002 Steven Barrus
# Copyright (C) 2002 Dana Dahlstrom
# Copyright (C) 2002 Robert Ricci
# Copyright (C) 2002 Spencer Visick
#
# See the AUTHORS file for contact info
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA



exit &usage if (!@ARGV or $ARGV[0] =~ /^-h/);

require 'usertools.ph';

$uid = $ARGV[0];

@fields = ( 'uid', 'uidnumber', 'cn', 'gecos',);
if ($config{user_attribute}){
  for (split(/,/,$config{user_attribute})){
    push(@fields,$_)
  }
}


$ldap = ldap_connect() || exit(1);
main();

sub main{

  if ($uid =~ /\d/) { 
    $search = "(uidNumber=" . $uid . ")";
  }else {
    $search = "(uid=" . $uid . ")";
  }

  $pass = gen_pass();     
  $entry = chentry();        

  $pass = enc_passwd($pass);  
  $mesg = $entry->replace( loginshell => $shell, 
                           userPassword => $pass );
  $mesg = $entry->update($ldap) ;
     die "An error occured. The error was:\n", $mesg->error if $mesg->code;
}

sub chentry {
##Checks information and prints unencrypted password
  
$mesg = $ldap->search( base => $config{userbase}, filter => $search);
my $entry = $mesg->entry() or die "ERROR: Unable to find LDAP entry\n";

if ($config{default_shell} eq "ask"){
  $shell = get_shell();
}else {
  $shell = $config{default_shell};
}


  print "**************************************************\n";
  foreach (@fields) {
    $aref = $entry->get_value($_);
    if ($aref) {
      printf ("%20s: ",$_);
      print (join ", ", $aref);
      print "\n";
    }
  }
  printf ("%20s: ", "loginshell");
  print (join ", ", $shell);
  print "\n";
  printf ("%20s: ", "New Password");
  print (join ", ", $pass);
  print "\n";
   
  print "**************************************************\n";

  $helper = 0;  #sux
  while($helper == 0){
    print "Does this look okay? [(y)es,(n)o] ";
    $response = <STDIN>;
    if ($response =~ /y/) {
      return $entry;
    } elsif ($response =~ /n/) {
      exit();
    }else {
      $helper = 0;
    }
  }
}

sub get_shell{
  $sh = "";
  print "Which shell whould you like to use? ";
  chomp ($sh = <STDIN>);
  return $sh;
}

sub usage{
  print << "EOSTR";
Usage: $0 USER...
Chage USER shell to $shell and generate a random password.

USER may be a login name or numeric user ID.

You will be prompted for a password with which to bind; if you succeed
in binding, you will given output to approve. 
EOSTR

  1;
}

__END__
=head1 NAME

userrevert - assigns a user the default shell and random password

=head1 SYNOPSIS

userrevert USER 
     
userrevert -h

=head1 DESCRIPTION

B<userrevert> will change a user's shell to the default_shell set
in /etc/usertools.conf or $HOME/.usertoolsrc. You will be prompted 
for a password with which to bind; if you succeed in binding, you 
will given output to approve.

=head1 OPTIONS

B<-h>

Print out usage information.

=head1 EXAMPLES
     
userrevert visick

Password: 
**************************************************
                 uid: visick
           uidnumber: 1079
                  cn: Spencer Visick
               gecos: Spencer Visick
          loginshell: /usr/bin/firstlogin
        New Password: yf7jtk4s
**************************************************
Does this look okay? [(y)es,(n)o] 


=head1 BUGS

lots probably, sorry. Contact AUTHORS if you find any.

=head1 AUTHORS

The usertools were written at the CADE by various opers. See the AUTHORS file 
for details.

=head1 SEE ALSO

usercreate(1), usermodify(1), user(1), usersearch(1).
groupcreate(1), groupmodify(1), groupsearch(1),


=cut 

