#!/usr/local/bin/perl
#
# $Id: usersearch,v 1.4 2003/05/30 22:43:44 visick Exp $
#
# usersearch - locates a user by uid, username, or custom field
#
# 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

use Term::ReadLine;
require 'usertools.ph';

use strict;
no strict "vars";

$term = new Term::ReadLine;

@fields = ('uid','gecos','uuid', 'homedirectory', 'loginshell', 'uidnumber','gidnumber');

# Check for -h
if ($ARGV[0] =~ /^-h/) {
  exit &usage;
}

$ldap = ldap_connect_anon() || exit(1);

if (@ARGV) {
  foreach (@ARGV) {
    lookup("|(cn=*" . $_ . "*)(cn=" . $_ .")");
    lookup("(uidNumber=" . $_ . ")");
    if ($config{user_search_attribute}){
      for (split(/,/,$config{user_search_attribute})){
        lookup("($attr=*" . $_ . "*)");
      }
    }
  }
} else {
  while (1) {
    $user = $term->readline("Lookup entry for name (blank to end): ");
    chomp $user;
    if ($user) {
      lookup("|(cn=*" . $user . "*)(cn=" . $user .")");
      lookup("(uidNumber=" . $user . ")");
      if ($config{user_search_attribute}){
        for (split(/,/,$config{user_search_attribute})){
          lookup("($attr=*" . $user . "*)");
        }
      }
    } else {
      last;
    }
  }
}

sub lookup {
  $search = shift;
  $help = 0;
  $mesg = $ldap->search( base => $config{userbase}, filter => $search);
  @entry = $mesg->entries;
  if (@entry){
    print "**************************************************\n";
    foreach $entry (@entry) { 
      foreach (@fields) {
        $aref = $entry->get_value($_, asref => 1);
        if ($aref) {
          printf("%20s: ",$_);
          print (join ", ", @$aref);
          print "\n";
        }
      }
      print "--------------------------------------------------\n";
    }
    print "**************************************************\n";
  }
}

sub usage {

  print << "EOSTR";

Usage: $0 [names]

EOSTR

  1;
}

__END__
=head1 NAME

usersearch - locates a user by uid, username, or custom field

=head1 SYNOPSIS

usersearch <uid> 

usersearch <username>
     
usersearch -h

=head1 DESCRIPTION

B<usersearch> retrieves list of users from the LDAP directory specified 
in /etc/usertools.conf or $HOME/.usertoolsrc. You will be prompted 
for a password with which to bind. Users are matched to the search 
parameter. An optional user_search_attribute may be defined in 
your configuration file.

=head1 OPTIONS

B<-h>

Print out usage information.

=head1 EXAMPLES
     
usersearch staff

**************************************************
                 uid: visick
               gecos: Spencer H Visick
                uuid: 00167097
       homedirectory: /afs/eng.utah.edu/home/visick
          loginshell: /bin/tcsh
           uidnumber: 8382
           gidnumber: 5
--------------------------------------------------
**************************************************

=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), userdelete(1), usermodify(1), userrevert(1), 
groupcreate(1), groupmodify(1), groupsearch(1).


=cut

