-
Notifications
You must be signed in to change notification settings - Fork 1
/
ugid-find
executable file
·282 lines (236 loc) · 8.68 KB
/
ugid-find
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/perl -w
# Search for files and directories by uid or gid
# based on an index file prepared by ugid-scan.
#
# Markus Kuhn
use strict;
use Storable;
use POSIX qw(strftime);
use FindBin qw($RealBin); # find directory where this file is located ...
use lib $RealBin; # ... and add it to @INC ...
use UGidScan; # so we can load this
our $dbfn = "$RealBin/ugid-list.sdb";
our $usage = <<"EOT";
Search for files by uid or gid.
Usage: $0 [options] command ...
Options:
-f <filename> filename of index file produced by ugid-scan
(default: '$dbfn')
This tool reads the index file into memory and performs queries on it.
It accepts a sequence of commands on the command line. There are
several types of commands. Selection commands perform queries and
deposit lists of pathnames on a stack. Filter commands transform a
list of pathnames on the stack. Output commands print the list of
pathnames on the stack in different forms. Information commands
display metadata about the index.
Selection commands:
uiddir=<range1> select all directories that contain (as an immediate
child node) a file and directory owned by a uid
in the integer range <range1>
giddir=<range2> select all directories that contain (as an immediate
child node) a file and directory belonging to a
gid in the integer range <range2>
uid=<range1> select all files and directories owned by a uid in <range1>
gid=<range2> select all files and directories with gid in <range2>
uid=<range1>:<range2>
select all files or directories owned by a uid in <range1>
that also have a gid in <range2>
gid=<range1>:<range2>
select all files or directories with gid in <range2>
that also have a uid in <range1>
The output of uid=<range1>:<range2> and gid=<range1>:<range2> should
be identical, but the runtime may differ due to different search
strategies. Prefer uid=<range1>:<range2> if you expect the output of
uiddir=<range1> to be small and use gid=<range1>:<range2> if you
expect the output of giddir=<range2> to be small.
A <range> can be a comma-separated list of ids or id-ranges (with
hyphen), or it can be a single numeric id or range prefixed with ^
meaning "not". Example ranges are "101,110-115,10000-", "^3601" and
"^3601-3610".
Note that the tool deliberately does not support any symbolic group
user or group names from the getent or LDAP "passwd" and "group"
tables. This is to avoid accidents by ambiguities in the assignment of
these names.
Filter commands:
dirnames strip filenames from a list of paths
prefixes remove from a list of paths all those for which
a directoy prefix is already in the list
Output commands:
print output all pathnames on stack as LF-terminated strings
print0 output all pathnames on stack as NUL-terminated strings
(for piping into "xargs -0r")
ll output all pathnames with "ls -lnd"
count output the length of the pathname lists on the stack
Query index metadata:
uids list of uids in the index
gids list of gids in the index
info print some information about the scan
mounts display mount options in force during (end of) scan
Usage examples:
\$ ./ugid-find uid=101 count
\$ ./ugid-find gid=^3601:3601 ll
\$ ./ugid-find gid=^3601:3601 print0 | xargs -0r ls -lnd
\$ ./ugid-find gid=^3601:3601 print0 | xargs -0r chgrp -hc 9601
\$ ./ugid-find uiddir=101 prefixes print0 | \
xargs -0r chown -hcR --from=101:60101 1101:1101
EOT
die("Use option -h for help.\n") unless @ARGV;
while (@ARGV && $ARGV[0] =~ /^-/) {
$_ = shift @ARGV;
if ($_ eq '-f') {
$dbfn = shift @ARGV;
} elsif ($_ eq '-h' || $_ eq '--help') {
print $usage;
exit 0;
} else {
die("Unknown option '$_', use option -h for help.\n");
}
}
my %errors;
# Search all files contained in one of the listed directories
# for any that match the given uid and gid range (undef = don't care).
sub search {
my ($uid, $gid, $dirs) = @_;
my @files;
my $ufilter = Filter->new($uid);
my $gfilter = Filter->new($gid);
for my $dir (@{$dirs}) {
my $dh;
unless (opendir($dh, $dir)) {
warn("Can't read directory '$dir': $!\n");
next;
};
chdir($dir) || die("Can't change to directory '$dir': $!\n");
while(readdir $dh) {
next if $_ eq '.' || $_ eq '..';
my (undef,undef,undef,undef,$fuid,$fgid) = lstat($_);
unless (defined $fuid) {
$errors{$dir}++;
next;
}
next unless $ufilter->matches0($fuid);
next unless $gfilter->matches0($fgid);
push @files, "$dir/$_";
}
closedir $dh;
}
return [sort @files];
}
sub uniq {
my %seen;
grep !$seen{$_}++, @_;
}
our $db = retrieve $dbfn;
my @stack = ();
while (@ARGV) {
my $opt = shift @ARGV;
if ($opt =~ /^uiddir=([-\^\d,]+)$/) {
# push onto the stack a list of all directories containing
# files with the given uid.
push @stack, $db->uid_range_dirs($1);
} elsif ($opt =~ /^uid=([-\^\d,]+)(?::([-\^\d,]+))?$/) {
# push onto the stack a list of all files with the given uid
# and optionally filter for gid ranges
my $dirs = $db->uid_range_dirs($1);
push @stack, search($1, $2, $dirs);
} elsif ($opt =~ /^giddir=([-\^\d,]+)$/) {
# push onto the stack a list of all directories containing
# files with the given gid.
push @stack, $db->gid_range_dirs($1);
} elsif ($opt =~ /^gid=(?:([-\^\d,]+):)?([-\^\d,]+)$/) {
# push onto the stack a list of all files with the given gid
# and optionally filter for uid ranges
my $dirs = $db->gid_range_dirs($2);
push @stack, search($1, $2, $dirs);
} elsif ($opt eq 'dirnames') {
# replace all filenames with the corresponding directory names,
# then eliminate duplicates
my @dirnames;
for my $f (@{pop @stack}) {
$f =~ s/\/[^\/]+$// if -l $f || ! -d _;
push @dirnames, $f;
}
push @stack, [sort(uniq(@dirnames))];
} elsif ($opt eq 'prefixes') {
# eliminate all pathnames from a list for which a prefix
# also appears in the list
my @prefixes;
FILE:
for my $f (@{pop @stack}) {
for my $p (@prefixes) {
next FILE if (substr($f, 0, length($p)) eq $p &&
substr($f, length($p), 1) eq '/');
}
push @prefixes, $f;
}
push @stack, [@prefixes];
} elsif ($opt eq 'print') {
# print all paths on the stack as a LF-terminated string
for my $s (@stack) {
for my $r (@{$s}) {
die("Path ‘$r’ contains a line-feed character.\n",
"Instead of ‘print’ use ‘print0’ or ‘ll’, ",
"or just rename the file. Aborting.\n") if $r =~ /\n/;
print $r, "\n";
}
}
} elsif ($opt eq 'print0') {
# print all paths on the stack as a \0-terminated string
for my $s (@stack) {
for my $r (@{$s}) {
print $r, "\0";
}
}
} elsif ($opt eq 'll') {
# show all paths on the stack with "ls -lnd"
open(my $p, '|xargs -r0 ls -lnd');
for my $s (@stack) {
for my $r (@{$s}) {
print $p $r, "\0";
}
}
close $p;
} elsif ($opt eq 'count') {
# output path numbers on the stack
for my $s (@stack) {
print scalar(@{$s}), "\n";
}
} elsif ($opt eq 'uids') {
# output list of uids indexed
print join(',', @{$db->{uids}}), "\n";
} elsif ($opt eq 'gids') {
# output list of gids indexed
print join(',', @{$db->{gids}}), "\n";
} elsif ($opt eq 'info') {
# output metadata about the scan
print("$dbfn:\n");
my ($uid_dirs, $gid_dirs) = $db->count_dirs;
printf("Indexed %d directory entries across %d uids.\n",
$uid_dirs, scalar(@{$db->{uids}}));
printf("Indexed %d directory entries across %d gids.\n",
$gid_dirs, scalar(@{$db->{gids}}));
next unless exists $db->{command};
print("Scan command: ", join(' ', @{$db->{command}}), "\n");
print("Scan time: ",
strftime('%Y-%m-%d %H:%M:%S', localtime $db->{time}[0]), " to ",
strftime('%Y-%m-%d %H:%M:%S', localtime $db->{time}[1]), "\n");
my $elapsed = $db->{time}[1] - $db->{time}[0];
printf("Scan duration: %d:%02d:%02d\n",
$elapsed / 3600, ($elapsed % 3600) / 60, $elapsed % 60);
printf("Scanner: %s:%s@%s\n",
$db->{euid}, $db->{egid}, $db->{hostname});
print($db->{ps}) if defined $db->{ps};
print("Excluded uids: ", $db->{excluded_uids}, "\n")
if defined $db->{excluded_uids};
print("Excluded gids: ", $db->{excluded_gids}, "\n")
if defined $db->{excluded_gids};
} elsif ($opt eq 'mounts') {
print("Mount options during scan:\n", $db->{mounts});
} else {
die("Unknown option '$opt'\n");
}
}
# report errors encountered
for my $dir (sort keys %errors) {
print STDERR "Warning: could not stat $errors{$dir} files in $dir\n";
}