[Samba] [new script] smbldap-userlist

tarjei tarjei at nu.no
Fri May 25 16:09:22 GMT 2007


Hi, for those who are using ldap for managing Samba, here's a quick
script I wrote to make it simple to list users or machines on the
commandline. I use it to delete old machine entries that are not in use
anymore.

For usage, read the perldoc in the script. Place the script in the same
directory as you have your other smbldap-tools scripts.

The script is based on smbldap-usershow.

Kind regards,
Tarjei
-------------- next part --------------
#!/usr/bin/perl -w

# Created by P.Wieleba at iem.pw.edu.pl in 2004

use strict;
use Getopt::Std;
use FindBin;
use FindBin qw($RealBin);
use lib "$RealBin/";
use smbldap_tools;
use Date::Format; 

# function declaration
sub exist_in_tab;

my %Options;

my $ok = getopts('dmug?', \%Options);
if ( (!$ok) || ($Options{'?'}) ) {
  print "Usage: $0 []\n";
  exit (1);
}

my $user;
my $pass;
if ( $< != 0 ) {
  my $current_user = getpwuid($<);
  if ($current_user and $ARGV[0] and $current_user ne $ARGV[0] ) {
    die "Only root can change other users inormation\n";
  }
} else {
  if ( $ARGV[0] ) {
    $user = $ARGV[0];
  }
  $pass = 1;
}

if (!defined($user)) {
  $user = getpwuid($<);
}

my ($dn,$ldap_master);
# First, connecting to the directory
if ($< != 0) {
  # non-root user
  if (!defined($pass)) {
    # prompt for password
    print "UNIX password: ";
    system "stty -echo" if (-t STDIN);
    chomp($pass=<STDIN>);
    system "stty echo" if (-t STDIN);
    print "\n";

    $config{masterDN}="uid=$user,$config{usersdn}";
    $config{masterPw}="$pass";
    $ldap_master=connect_ldap_master();
    $dn=$config{masterDN};
    if (!is_user_valid($user, $dn, $pass)) {
      print "Authentication failure\n";
      exit (10);
    }
  }
} else {
  # root user
  $ldap_master=connect_ldap_master();
  # test existence of user in LDAP
  my $dn_line;
}

sub print_user {
    my ($entry, %Options) = @_;
    printf "%4s  ", $entry->get_value('uidNumber') ;
    printf "%-20s ", $entry->get_value('uid');
    printf "%-10s ", $entry->get_value('gecos') if ($Options{'g'});
    printf "%-12s ", time2str("%D %H:%m", $entry->get_value('sambaPwdLastSet')) if ($Options{'d'});
    print "\n";
}
my $filter;

if ($Options{'m'}) {
    $filter = "(&(objectclass=posixAccount)(sambaAcctFlags=[W          ]))";
} elsif ($Options{'u'}) {
    $filter = "(&(objectclass=posixAccount)(sambaAcctFlags=[U          ]))";
    
} else {
    $filter = "(&(objectclass=posixAccount))";
}
my  $mesg = $ldap_master->search ( base   => $config{suffix},
                                   scope => $config{scope},
                                   filter => $filter 
                                    );
$mesg->code && die $mesg->error;
foreach my $entry ($mesg->all_entries) {
    print_user($entry,%Options);
}

########################################

=head1 NAME

smbldap-listusers list users or machines with some info

=head1 SYNOPSIS

smbldap-listusers [-m] [-g] [-p]


=head1 DESCRIPTION
-g     Show gecos entry

-d     Show when the user last changed his or her password.

-m     Only list machines.

-u     Only list users

-?     show the help message


=cut

#'

# The End



More information about the samba mailing list