Debian Codegraph @

bin/prefixadmin

raw
1 #!/usr/bin/perl -w
2
3 ###########################################################################
4 # TODO
5 ###########################################################################
6 # o tbd
7 ###########################################################################
8
9 use strict;
10
11 use App::Options (
12 options => [qw(prefix remote remote_prefix remote_user remote_host op verbose)],
13 option => {
14 prefix => {
15 description => "the directory path to be administered (generally an 'application root' directory)",
16 },
17 remote => {
18 description => "run the command remotely using 'ssh' rather than locally as the logged in user",
19 },
20 remote_host => {
21 description => "(for --remote option) which server(s) to run the prefixadmin on. Comma delimited list. (optionally user\@host:/pr/ef/ix)",
22 },
23 remote_prefix => {
24 description => "directory on remote machine to administer",
25 },
26 remote_user => {
27 description => "the username to run the remote command",
28 },
29 op => {
30 description => "list of operations to perform [fix] (check,fix)",
31 },
32 username => {
33 description => "the user name that a prefix should be shared as",
34 },
35 group => {
36 description => "the group name that a prefix should be shared as",
37 },
38 verbose => {
39 default => 0,
40 description => "level of detail to print",
41 },
42 },
43 );
44
45 $| = 1;
46
47 {
48 my (@op);
49 if ($App::options{op}) {
50 @op = split(/,/,$App::options{op});
51 }
52 elsif ($#ARGV > -1) {
53 @op = @ARGV;
54 }
55 else {
56 @op = ("fix");
57 }
58
59 if ($#op > -1) {
60 my $admin = App::Options::PrefixAdmin->new();
61
62 foreach my $op (@op) {
63 if ($op eq "fix") {
64 $admin->fix(\%App::options);
65 }
66 else {
67 print "Unknown operation [$op]\n";
68 }
69 }
70 }
71 else {
72 print "No operations specified\n";
73 }
74 }
75
76 package App::Options::PrefixAdmin;
77
78 use File::Find;
79 use Date::Format;
80 use Fcntl ':mode';
81
82 sub new {
83 my ($this) = @_;
84 my $class = ref($this) || $this;
85 my $self = {};
86 bless $self, $class;
87 return($self);
88 }
89
90 sub fix {
91 my ($self, $options) = @_;
92 my ($path, $file, $cwd);
93 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks);
94
95 my $verbose = $options->{verbose} || 0;
96 my $prefix = $options->{prefix} || die "prefix not specified";
97 die "$prefix is not a directory" if (! -d $prefix);
98 chdir($prefix) || die "Could not change directory to $prefix";
99
100 $path = ".";
101 ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($path);
102 if ($verbose >= 2) {
103 printf("%3d %8d %10s %2d %5d %5d %6d %15d [%17s] %s\n",
104 $dev, $ino, $self->format_mode($mode), $nlink, $uid, $gid, $rdev, $size, time2str("%Y-%m-%d %H:%M:%S", $mtime), $path);
105 }
106
107 my ($u_name, $u_pass, $u_uid, $u_gid, $u_quota, $u_comment, $u_gcos, $u_dir, $u_shell, $u_expire) = getpwuid($uid);
108 print "Uname: $u_name UID: $u_uid\n" if ($verbose >= 2);
109
110 my ($grp_name, $grp_passwd, $grp_gid, $grp_members) = getgrgid($gid);
111 print "Gname: $grp_name GID: $grp_gid Members: $grp_members\n" if ($verbose >= 2);
112
113 my ($shgrp_name,$shgrp_passwd,$shgrp_gid,$shgrp_members);
114 my $shared_group = $options->{group};
115 if ($shared_group) {
116 ($shgrp_name,$shgrp_passwd,$shgrp_gid,$shgrp_members) = getgrnam($shared_group);
117 print "Shared Gname: $shgrp_name GID: $shgrp_gid Members: $shgrp_members\n" if ($verbose >= 2);
118 }
119 else { # if --group is not given on the command line, use the GID of the top level directory
120 ($shgrp_name,$shgrp_passwd,$shgrp_gid,$shgrp_members) = getgrgid($gid);
121 print "Shared Gname: $shgrp_name GID: $shgrp_gid Members: $shgrp_members\n" if ($verbose >= 2);
122 }
123
124 #print STDERR " searching $prefix\n" if ($verbose >= 2);
125 find(
126 sub {
127 $file = $_;
128 $path = $File::Find::name;
129 $path =~ s!^\.\/!!;
130 $cwd = $File::Find::dir;
131 $cwd =~ s!^\.\/!!;
132 my ($err_msg);
133
134 ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($file);
135
136 if (!defined $mode) {
137 print ">>> $file\n" if ($verbose);
138 }
139 else {
140 printf("%3d %8d %07o:%10s %2d %5d %5d %6d %15d [%17s] %s\n",
141 $dev, $ino, $mode, $self->format_mode($mode), $nlink, $uid, $gid, $rdev, $size, time2str("%Y-%m-%d %H:%M:%S", $mtime), $path) if ($verbose);
142 if ($shgrp_gid) {
143 $err_msg = $self->_share_file($file, $options, $shgrp_gid, $mode, $uid, $gid);
144 }
145 }
146 return(0);
147 },
148 "."
149 );
150 }
151
152 # S_IRWXU S_IRUSR S_IWUSR S_IXUSR
153 # S_IRWXG S_IRGRP S_IWGRP S_IXGRP
154 # S_IRWXO S_IROTH S_IWOTH S_IXOTH
155 #
156 # # Setuid/Setgid/Stickiness/SaveText.
157 # # Note that the exact meaning of these is system dependent.
158 #
159 # S_ISUID S_ISGID S_ISVTX S_ISTXT
160
161 sub format_mode {
162 my ($self, $mode) = @_;
163 my $fmt_mode = ($mode & S_IFREG) ? "-" : (($mode & S_IFDIR) ? "d" : (($mode & S_IFLNK) ? "l" : "?"));
164 $fmt_mode .= ($mode & S_IRUSR) ? "r" : "-";
165 $fmt_mode .= ($mode & S_IWUSR) ? "w" : "-";
166 $fmt_mode .= ($mode & S_IXUSR) ? (($mode & S_ISUID) ? "s" : "x") : (($mode & S_ISUID) ? "S" : "-");
167 $fmt_mode .= ($mode & S_IRGRP) ? "r" : "-";
168 $fmt_mode .= ($mode & S_IWGRP) ? "w" : "-";
169 $fmt_mode .= ($mode & S_IXGRP) ? (($mode & S_ISGID) ? "s" : "x") : (($mode & S_ISGID) ? "S" : "-");
170 $fmt_mode .= ($mode & S_IROTH) ? "r" : "-";
171 $fmt_mode .= ($mode & S_IWOTH) ? "w" : "-";
172 $fmt_mode .= ($mode & S_IXOTH) ? (($mode & S_ISVTX) ? "t" : "x") : (($mode & S_ISVTX) ? "T" : "-");
173 return($fmt_mode);
174 }
175
176 # 1. $cnt = chmod 0755, 'foo', 'bar';
177 # 2. chmod 0755, @executables;
178 # 3. $mode = '0644'; chmod $mode, 'foo'; # !!! sets mode to
179 # 4. # --w----r-T
180 # 5. $mode = '0644'; chmod oct($mode), 'foo'; # this is better
181 # 6. $mode = 0644; chmod $mode, 'foo'; # this is best
182
183 # 1. $cnt = chown $uid, $gid, 'foo', 'bar';
184 # 2. chown $uid, $gid, @filenames;
185
186 sub _share_file {
187 my ($self, $file, $options, $shgrp_gid, $mode, $uid, $gid) = @_;
188
189 my $verbose = $options->{verbose};
190 my $err_msg = "";
191 my ($retval);
192 if ($shgrp_gid) {
193
194 if ($gid != $shgrp_gid) {
195 $retval = chown($uid, $shgrp_gid, $file);
196 if ($verbose) {
197 print ">>> chown($uid, $shgrp_gid, $file) = [$retval]\n";
198 }
199 }
200
201 my $share_mode = $mode & 07777;
202 my $mode_needs_fix = 0;
203
204 if ($mode & S_IFDIR) {
205 if ($mode & S_ISGID) {
206 # do nothing
207 }
208 else {
209 $share_mode |= S_ISGID;
210 $mode_needs_fix = 1;
211 }
212 }
213 else {
214 if ($mode & S_ISGID) {
215 $share_mode ^= S_ISGID;
216 $mode_needs_fix = 1;
217 }
218 else {
219 # do nothing
220 }
221 }
222
223 if ($mode & S_ISUID) {
224 $share_mode ^= S_ISUID;
225 $mode_needs_fix = 1;
226 }
227 if ($mode & S_ISVTX) {
228 $share_mode ^= S_ISVTX;
229 $mode_needs_fix = 1;
230 }
231
232 if ($mode & S_IRUSR) {
233 if ($mode & S_IRGRP) {
234 # do nothing
235 }
236 else {
237 $share_mode |= S_IRGRP;
238 $mode_needs_fix = 1;
239 }
240 }
241 if ($mode & S_IWUSR) {
242 if ($mode & S_IWGRP) {
243 # do nothing
244 }
245 else {
246 $share_mode |= S_IWGRP;
247 $mode_needs_fix = 1;
248 }
249 }
250 if ($mode & S_IXUSR) {
251 if ($mode & S_IXGRP) {
252 # do nothing
253 }
254 else {
255 $share_mode |= S_IXGRP;
256 $mode_needs_fix = 1;
257 }
258 }
259
260 if ($mode_needs_fix) {
261 $retval = chmod($share_mode, $file);
262 if ($verbose) {
263 printf(">>> chmod(%06o, $file) = [$retval]\n", $share_mode);
264 }
265 }
266 }
267 return($err_msg);
268 }
269
270 1;