summaryrefslogtreecommitdiffstats
path: root/examples/misc/check_multiple_LDAP_entries.pl
blob: 00c197ace4fa4c8c3c22274f994f8ad983e24300 (plain)
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
195
196
197
198
199
200
201
#!/usr/bin/perl -w
# Guenther Deschner <gd@samba.org>
#
# check for multiple LDAP entries

use strict;

use Net::LDAP;
use Getopt::Std;

my %opts;

if (!@ARGV) {
	print "usage: $0 -h host -b base -D admindn -w password [-l]\n";
	print "\tperforms checks for multiple sid, uid and gid-entries on your LDAP server\n";
	print "\t-l adds additional checks against the local /etc/passwd and /etc/group file\n";
	exit 1;
}

getopts('b:h:D:w:l', \%opts);

my $host =   $opts{h}	|| "localhost";
my $suffix = $opts{b}	|| die "please set base with -b";
my $binddn = $opts{D}	|| die "please set basedn with -D";
my $bindpw = $opts{w}	|| die "please set password with -w";
my $check_local_files = $opts{l} || 0;

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


my ($ldap, $res);
my (%passwd_h, %group_h);
my $bad_uids = 0;
my $bad_gids = 0;
my $bad_sids = 0;
my $ret = 0;

if ($check_local_files) {
	my @uids = `cut -d ':' -f 3 /etc/passwd`;
	my @gids = `cut -d ':' -f 3 /etc/group`;
 
	foreach my $uid (@uids) {
		chomp($uid);
		$passwd_h{$uid} = $uid;
	}

	foreach my $gid (@gids) {
		chomp($gid);
		$group_h{$gid} = $gid;
	}
}

########
# bind #
########

$ldap = Net::LDAP->new($host, version => '3');

$res = $ldap->bind( $binddn, password => $bindpw);
$res->code && die "failed to bind: ", $res->error;



###########################
# check for double sids   #
###########################

print "\ntesting for multiple sambaSids\n";

$res = $ldap->search( 
	base => $suffix, 
	filter => "(objectclass=sambaSamAccount)");

$res->code && die "failed to search: ", $res->error;

foreach my $entry ($res->all_entries) {

	my $sid = $entry->get_value('sambaSid');

	my $local_res = $ldap->search( 
		base => $suffix, 
		filter => "(&(objectclass=sambaSamAccount)(sambaSid=$sid))");
	
	$local_res->code && die "failed to search: ", $local_res->error;
	if ($local_res->count > 1) {
		print "A SambaSamAccount with sambaSid [$sid] must exactly exist once\n";
		print "You have ", $local_res->count, " entries:\n";
		foreach my $loc_entry ($local_res->all_entries) {
			printf "\t%s\n", $loc_entry->dn;
		}
		++$bad_sids;
	}
}

if ($bad_sids) {
	$ret = -1;
	print "You have $bad_sids bad sambaSids in your system. You might need to repair them\n";
} else {
	print "No multiple sambaSids found in your system\n";
}

print "-" x 80, "\n";

###########################
# check for double groups #
###########################

print "\ntesting for multiple gidNumbers\n";

$res = $ldap->search( 
	base => $suffix, 
	filter => "(objectclass=posixGroup)");

$res->code && die "failed to search: ", $res->error;

foreach my $entry ($res->all_entries) {

	my $gid = $entry->get_value('gidNumber');
	my $dn  = $entry->dn;

	my $local_res = $ldap->search( 
		base => $suffix, 
		filter => "(&(objectclass=posixGroup)(gidNumber=$gid))");
	
	$local_res->code && die "failed to search: ", $local_res->error;
	if ($local_res->count > 1) {
		print "A PosixGroup with gidNumber [$gid] must exactly exist once\n";
		print "You have ", $local_res->count, " entries:\n";
		foreach my $loc_entry ($local_res->all_entries) {
			printf "\t%s\n", $loc_entry->dn;
		}
		++$bad_gids;
		next;
	}

	if ($check_local_files && exists $group_h{$gid}) {
		print "Warning: There is a group in /etc/group that has gidNumber [$gid] as well\n";
		print "This entry may conflict with $dn\n";
		++$bad_gids;
	}
}

if ($bad_gids) {
	$ret = -1;
	print "You have $bad_gids bad gidNumbers in your system. You might need to repair them\n";
} else {
	print "No multiple gidNumbers found in your system\n";
}

print "-" x 80, "\n";


###########################
# check for double users  #
###########################

print "\ntesting for multiple uidNumbers\n";

$res = $ldap->search( 
	base => $suffix, 
	filter => "(objectclass=posixAccount)");

$res->code && die "failed to search: ", $res->error;


foreach my $entry ($res->all_entries) {

	my $uid = $entry->get_value('uidNumber');
	my $dn  = $entry->dn;

	my $local_res = $ldap->search( 
		base => $suffix, 
		filter => "(&(objectclass=posixAccount)(uidNumber=$uid))");
	
	$local_res->code && die "failed to search: ", $local_res->error;
	if ($local_res->count > 1) {
		print "A PosixAccount with uidNumber [$uid] must exactly exist once\n";
		print "You have ", $local_res->count, " entries:\n";
		foreach my $loc_entry ($local_res->all_entries) {
			printf "\t%s\n", $loc_entry->dn;
		}
		++$bad_uids;
		next;
	}
	if ($check_local_files && exists $passwd_h{$uid}) {
		print "Warning: There is a user in /etc/passwd that has uidNumber [$uid] as well\n";
		print "This entry may conflict with $dn\n";
		++$bad_uids;
	}
}

if ($bad_uids) {
	$ret = -1;
	print "You have $bad_uids bad uidNumbers in your system. You might need to repair them\n";
} else {
	print "No multiple uidNumbers found in your system\n";
}

$ldap->unbind;

exit $ret;