File: smbldap-grouplist.pl

package info (click to toggle)
smbldap-tools 0.9.7-1+deb7u1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,124 kB
  • sloc: perl: 5,056; sh: 173; makefile: 96
file content (194 lines) | stat: -rw-r--r-- 4,290 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!@PERL_CMD@

# $Id$

#  This code was developped by Jerome Tournier (jtournier@gmail.com) and
#  contributors (their names can be found in the CONTRIBUTORS file).

#  This was first created by Martin Matuska <mm@FreeBSD.org>

#  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, see <http://www.gnu.org/licenses/>.

# Purpose of smbldap-grouplist : list groups

use strict;
use warnings;
use Getopt::Std;
use smbldap_tools;

# function declaration
sub exist_in_tab;

my %Options;

my $ok = getopts('dtS?', \%Options);
if ( (!$ok) || ($Options{'?'}) || $Options{'h'} ) {
    print "Usage: $0 [dtS?] [user template]\n\n";
    print "-d     Show displayName\n";
    print "-t     Show samba group type\n";
    print "-S     Show samba SID\n";
    print "-?     show the help message\n";
    exit (1);
}

my $binduser;
my $pass;

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

my $search;
if ( $ARGV[0] ) {
    if ( $< != 0 ) {
        die "Only root can show group inormation\n";
    } else {
        $search=$ARGV[0];
    }
} elsif ( $< != 0 ) {
    $search=$binduser;
}


my ($dn,$ldap_master);
# First, connecting to the directory
if ($< != 0) {
    # non-root user
    if (!defined($pass)) {
	$pass = password_read("UNIX password: ");

# JTO: search real basedn: may be different in case ou=bla1,ou=bla2 !
# JTO: faire afficher egalement lock, expire et lastChange
$config{masterDN}="uid=$binduser,$config{usersdn}";
$config{masterPw}="$pass";
$ldap_master=connect_ldap_master();
$dn=$config{masterDN};
if (!is_user_valid($binduser, $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_group {
    my ($entry, %Options) = @_;
    printf "%4s ", $entry->get_value('gidNumber') ;
    printf "|%-20s ", $entry->get_value('cn');
    if ($Options{'d'})
    {
	if (defined $entry->get_value('displayName'))
	{
	    printf "|%-20s", $entry->get_value('displayName');
	} else {
	    print "|-";
	}
    }
    if ($Options{'t'})
    {
	my $group_name;
	if (defined($entry->get_value('sambaGroupType')) && \
	    defined($group_name = &group_name_by_type($entry->get_value('sambaGroupType'))))
	{
	    printf "|%-14s", $group_name;
	} else {
	    print "|-";
	}
    }
    if ($Options{'S'})
    {
	if (defined $entry->get_value('sambaSID'))
	{
	    printf "|%-47s", $entry->get_value('sambaSID');
	} else {
	    print "|-";
	}
    }
    print "|\n";
}

my $attrs="['gid','cn'";
my $banner="gid  |cn                   ";
if ($Options{'d'})
{
    $banner .=  "|displayName         ";
    $attrs  .=  ",'displayName'";
}
if ($Options{'t'})
{
    $banner .=  "|sambaGroupType";
    $attrs  .=  ",'sambaGroupType'";
}
if ($Options{'S'})
{
    $banner .=  "|sambaSID                                       ";
    $attrs  .=  ",'sambaSID'";
}
$attrs.="]";
$banner.="|";
print "$banner\n\n";
my $filter;
$filter = "(&(objectclass=posixGroup)";
my $base = $config{groupsdn};

if ($search) {
    $filter.="(displayName=$search)";
}

$filter.=")";

my  $mesg = $ldap_master->search ( base   => $base,
                                   scope => $config{scope},
                                   filter => $filter,
				   attrs => "$attrs"
				   );
$mesg->code && warn $mesg->error;

foreach my $entry ($mesg->all_entries) {
    print_group($entry,%Options);
}
########################################

=head1 NAME

smbldap-grouplist - list groups

=head1 SYNOPSIS

smbldap-grouplist [-S] [group template]


=head1 DESCRIPTION

-d     Show displayName

-S     Show samba SID entry

-?     show the help message

=head1 EXAMPLE

smbldap-grouplist -dS

smbldap-grouplist "*ourn*"

=cut

#'

# The End