2 # Guenther Deschner <gd@samba.org>
4 # check for multiple LDAP entries
14 print "usage: $0 -h host -b base -D admindn -w password [-l]\n";
15 print "\tperforms checks for multiple sid, uid and gid-entries on your LDAP server\n";
16 print "\t-l adds additional checks against the local /etc/passwd and /etc/group file\n";
20 getopts
('b:h:D:w:l', \
%opts);
22 my $host = $opts{h
} || "localhost";
23 my $suffix = $opts{b
} || die "please set base with -b";
24 my $binddn = $opts{D
} || die "please set basedn with -D";
25 my $bindpw = $opts{w
} || die "please set password with -w";
26 my $check_local_files = $opts{l
} || 0;
28 ########################
32 my (%passwd_h, %group_h);
38 if ($check_local_files) {
39 my @uids = `cut -d ':' -f 3 /etc/passwd`;
40 my @gids = `cut -d ':' -f 3 /etc/group`;
42 foreach my $uid (@uids) {
44 $passwd_h{$uid} = $uid;
47 foreach my $gid (@gids) {
49 $group_h{$gid} = $gid;
57 $ldap = Net
::LDAP
->new($host, version
=> '3');
59 $res = $ldap->bind( $binddn, password
=> $bindpw);
60 $res->code && die "failed to bind: ", $res->error;
64 ###########################
65 # check for double sids #
66 ###########################
68 print "\ntesting for multiple sambaSids\n";
72 filter
=> "(objectclass=sambaSamAccount)");
74 $res->code && die "failed to search: ", $res->error;
76 foreach my $entry ($res->all_entries) {
78 my $sid = $entry->get_value('sambaSid');
80 my $local_res = $ldap->search(
82 filter
=> "(&(objectclass=sambaSamAccount)(sambaSid=$sid))");
84 $local_res->code && die "failed to search: ", $local_res->error;
85 if ($local_res->count > 1) {
86 print "A SambaSamAccount with sambaSid [$sid] must exactly exist once\n";
87 print "You have ", $local_res->count, " entries:\n";
88 foreach my $loc_entry ($local_res->all_entries) {
89 printf "\t%s\n", $loc_entry->dn;
97 print "You have $bad_sids bad sambaSids in your system. You might need to repair them\n";
99 print "No multiple sambaSids found in your system\n";
102 print "-" x
80, "\n";
104 ###########################
105 # check for double groups #
106 ###########################
108 print "\ntesting for multiple gidNumbers\n";
110 $res = $ldap->search(
112 filter
=> "(objectclass=posixGroup)");
114 $res->code && die "failed to search: ", $res->error;
116 foreach my $entry ($res->all_entries) {
118 my $gid = $entry->get_value('gidNumber');
121 my $local_res = $ldap->search(
123 filter
=> "(&(objectclass=posixGroup)(gidNumber=$gid))");
125 $local_res->code && die "failed to search: ", $local_res->error;
126 if ($local_res->count > 1) {
127 print "A PosixGroup with gidNumber [$gid] must exactly exist once\n";
128 print "You have ", $local_res->count, " entries:\n";
129 foreach my $loc_entry ($local_res->all_entries) {
130 printf "\t%s\n", $loc_entry->dn;
136 if ($check_local_files && exists $group_h{$gid}) {
137 print "Warning: There is a group in /etc/group that has gidNumber [$gid] as well\n";
138 print "This entry may conflict with $dn\n";
145 print "You have $bad_gids bad gidNumbers in your system. You might need to repair them\n";
147 print "No multiple gidNumbers found in your system\n";
150 print "-" x
80, "\n";
153 ###########################
154 # check for double users #
155 ###########################
157 print "\ntesting for multiple uidNumbers\n";
159 $res = $ldap->search(
161 filter
=> "(objectclass=posixAccount)");
163 $res->code && die "failed to search: ", $res->error;
166 foreach my $entry ($res->all_entries) {
168 my $uid = $entry->get_value('uidNumber');
171 my $local_res = $ldap->search(
173 filter
=> "(&(objectclass=posixAccount)(uidNumber=$uid))");
175 $local_res->code && die "failed to search: ", $local_res->error;
176 if ($local_res->count > 1) {
177 print "A PosixAccount with uidNumber [$uid] must exactly exist once\n";
178 print "You have ", $local_res->count, " entries:\n";
179 foreach my $loc_entry ($local_res->all_entries) {
180 printf "\t%s\n", $loc_entry->dn;
185 if ($check_local_files && exists $passwd_h{$uid}) {
186 print "Warning: There is a user in /etc/passwd that has uidNumber [$uid] as well\n";
187 print "This entry may conflict with $dn\n";
194 print "You have $bad_uids bad uidNumbers in your system. You might need to repair them\n";
196 print "No multiple uidNumbers found in your system\n";