Handle hostnames with upper-case letters
[webmin.git] / file / chmod.cgi
1 #!/usr/local/bin/perl
2 # chmod.cgi
3 # Change the ownership and permissions on a file
4
5 require './file-lib.pl';
6 $disallowed_buttons{'info'} && &error($text{'ebutton'});
7 &ReadParse();
8 &webmin_log($in{'linkto'} ? "relink" : "chmod", undef, $in{'path'}, \%in);
9 &switch_acl_uid_and_chroot();
10 print "Content-type: text/plain\n\n";
11 !$access{'ro'} && &can_access($in{'path'}) ||
12         &failure(&text('chmod_eaccess', $in{'path'}));
13
14 if (defined($in{'user'})) {
15         $uid = $in{'user'} =~ /^\d+$/ ? $in{'user'} :
16                defined(%user_to_uid) ? $user_to_uid{$in{'user'}} :
17                                        getpwnam($in{'user'});
18         &failure(&text('chmod_euser', $in{'user'})) if (!defined($uid));
19         $gid = $in{'group'} =~ /^\d+$/ ? $in{'group'} :
20                defined(%group_to_gid) ? $group_to_gid{$in{'group'}} :
21                                          getgrnam($in{'group'});
22         &failure(&text('chmod_egroup', $in{'group'})) if (!defined($gid));
23         }
24
25 if ($in{'linkto'}) {
26         # Just changing the link target
27         $follow && &failure($text{'chmod_efollow'});
28         &lock_file($in{'path'});
29         unlink($in{'path'});
30         symlink($in{'linkto'}, $in{'path'}) ||
31                 &failure(&text('chmod_elink', $1));
32         &unlock_file($in{'path'});
33         }
34 elsif ($in{'rec'} == 0) {
35         # Just this file
36         &update($in{'path'}, 0);
37         }
38 elsif ($in{'rec'} == 1) {
39         # This directory and all its files
40         &update($in{'path'}, 0);
41         opendir(DIR, $in{'path'});
42         foreach $f (readdir(DIR)) {
43                 next if ($f eq "." || $f eq "..");
44                 next if (-l $full);
45                 &update("$in{'path'}/$f", 1) if (!-d $full);
46                 }
47         closedir(DIR);
48         }
49 elsif ($in{'rec'} == 2) {
50         # Directory and all subdirectories
51         &update($in{'path'}, 0);
52         &recurse($in{'path'});
53         }
54 print "\n";
55
56 sub recurse
57 {
58 local(@files, $f, $full);
59 opendir(DIR, $_[0]);
60 @files = readdir(DIR);
61 closedir(DIR);
62 foreach $f (@files) {
63         $full = "$_[0]/$f";
64         next if ($f eq "." || $f eq "..");
65         next if (-l $full);
66         &update($full, !-d $full);
67         &recurse($full) if (-d $full);
68         }
69 }
70
71 sub failure
72 {
73 print @_,"\n";
74 exit;
75 }
76  
77 # update(file, perms_only)
78 sub update
79 {
80 local $perms = $in{'perms'};
81 if (defined($uid)) {
82         chown($uid, $gid, $_[0]) || &failure(&text('chmod_echown', $!));
83         }
84 if (defined($perms)) {
85         if ($_[1]) {
86                 @st = stat($_[0]);
87                 $perms = ($perms & 07777) | ($st[2] & 037777770000);
88                 }
89         chmod($perms, $_[0]) || &failure(&text('chmod_echmod', $!));
90         }
91 }
92