Handle hostnames with upper-case letters
[webmin.git] / quota / hpux-lib.pl
1 # hpux-lib.pl
2 # Quota functions for HPUX
3
4 # quotas_init()
5 sub quotas_init
6 {
7 return undef;
8 }
9
10 # quotas_supported()
11 # Returns 1 for user quotas, 2 for group quotas or 3 for both
12 sub quotas_supported
13 {
14 return 1;
15 }
16
17 # free_space(filesystem)
18 # Returns an array containing  btotal, bfree, ftotal, ffree
19 sub free_space
20 {
21 local($out);
22 $out = `df -t $_[0]`;
23 $out =~ /(\d+) blocks\s+(\d+) files\n.*\s+(\d+) blocks\s+(\d+) files/;
24 return ($3, $1, $4, $2);
25 }
26
27 # quota_can(&mnttab, &fstab)
28 # Can this filesystem type support quotas?
29 #  0 = No quota support (or not turned on in /etc/fstab)
30 #  1 = User quotas only
31 #  2 = Group quotas only
32 #  3 = User and group quotas
33 sub quota_can
34 {
35 return $_[0]->[2] eq "hfs" || $_[0]->[2] eq "vxfs" ? 1 : 0;
36 }
37
38 # quota_now(&mnttab, &fstab)
39 # Are quotas currently active?
40 #  0 = Not active
41 #  1 = User quotas active
42 #  2 = Group quotas active
43 #  3 = Both active
44 sub quota_now
45 {
46 return $_[0]->[3] =~ /,quota/ || $_[0]->[3] =~ /^quota/ ? 1 : 0;
47 }
48
49 # filesystem_users(filesystem)
50 # Fills the array %user with information about all users with quotas
51 # on this filesystem. This may not be all users on the system..
52 sub filesystem_users
53 {
54 local($rep, @rep, $n, $newfmt);
55 $rep = `$config{'user_repquota_command'} $_[0] 2>&1`;
56 if ($?) { return -1; }
57 @rep = split(/\n/, $rep); @rep = @rep[3..$#rep];
58 `uname -r` =~ /(\d+)\.(\d+)/;
59 if ($1 > 10 || $1 == 10 && $2 >= 20) { $newfmt = 1; }
60 for($n=0; $n<@rep; $n++) {
61         if ($newfmt && $rep[$n] =~ /(\S+)\s+..(.{7})(.{7})(.{7}).{13}(.{7})(.{7})(.{7})/ || !$newfmt && $rep[$n] =~ /(\S+)\s+..(\d+)\s+(\d+)\s+(\d+)\s+.{11}(\d+)\s+(\d+)\s+(\d+)/) {
62                 $user{$n,'user'} = $1;
63                 $user{$n,'ublocks'} = int($2);
64                 $user{$n,'sblocks'} = int($3);
65                 $user{$n,'hblocks'} = int($4);
66                 $user{$n,'ufiles'} = int($5);
67                 $user{$n,'sfiles'} = int($6);
68                 $user{$n,'hfiles'} = int($7);
69                 $user{$n,'user'} =~ s/^#//g;
70                 }
71         }
72 return $n;
73 }
74
75 # edit_quota_file(data, filesys, sblocks, hblocks, sfiles, hfiles)
76 sub edit_quota_file
77 {
78 local($rv, $line);
79 foreach $line (split(/\n/, $_[0])) {
80         if ($line =~ /^fs (\S+) blocks \(soft = (\d+), hard = (\d+)\) inodes \(soft = (\d+), hard = (\d+)\)$/ && $1 eq $_[1]) {
81                 # found line to change
82                 $line = "fs $_[1] blocks (soft = $_[2], hard = $_[3]) inodes (soft = $_[4], hard = $_[5])";
83                 }
84         $rv .= "$line\n";
85         }
86 return $rv;
87 }
88
89 # quotaon(filesystem, mode)
90 # Activate quotas and create quota file for some filesystem. The mode can
91 # be 1 for user only, 2 for group only or 3 for user and group
92 sub quotaon
93 {
94 return if (&is_readonly_mode());
95 local($qf, $out);
96 $qf = "$_[0]/quotas";
97 if (!(-r $qf)) {
98         &open_tempfile(QUOTAFILE, ">$qf", 0, 1);
99         &close_tempfile(QUOTAFILE);
100         &set_ownership_permissions(undef, undef, 0600, $qf);
101         }
102 $out = &backquote_logged("$config{'user_quotaon_command'} $_[0] 2>&1");
103 if ($?) { return $out; }
104 return undef;
105 }
106
107 # quotaoff(filesystem, mode)
108 # Turn off quotas for some filesystem
109 sub quotaoff
110 {
111 return if (&is_readonly_mode());
112 local($out);
113 $out = &backquote_logged("$config{'user_quotaoff_command'} $_[0] 2>&1");
114 if ($?) { return $out; }
115 return undef;
116 }
117
118 # quotacheck(filesystem, mode)
119 # Runs quotacheck on some filesystem
120 sub quotacheck
121 {
122 $out = &backquote_logged("$config{'quotacheck_command'} $_[0] 2>&1");
123 if ($?) { return $out; }
124 return undef;
125 }
126
127 # copy_user_quota(user, [user]+)
128 # Copy the quotas for some user to many others
129 sub copy_user_quota
130 {
131 for($i=1; $i<@_; $i++) {
132         $out = &backquote_logged("$config{'user_copy_command'} ".
133                                 quotemeta($_[0])." ".quotemeta($_[$i])." 2>&1");
134         if ($?) { return $out; }
135         }
136 return undef;
137 }
138
139 # user_filesystems(user)
140 # Fills the array %filesys with details of all filesystems some user has
141 # quotas on
142 sub user_filesystems
143 {
144 local($n, $_);
145 open(QUOTA, "$config{'user_quota_command'} ".quotemeta($_[0])." |");
146 $n=0; while(<QUOTA>) {
147         chop;
148         if (/^(Disk|Filesystem)/) { next; }
149         if (/^(\S+)$/) {
150                 # Bogus wrapped line!
151                 $filesys{$n,'filesys'} = $1;
152                 local $nl = <QUOTA>;
153                 if ($nl =~ /^\s+(\d+)\s+(\d+)\s+(\d+)\s.{0,15}\s(\d+)\s+(\d+)\s+(\d+)/ ||
154                     $nl =~ /^.{13}(.{7})(.{7})(.{7}).{12}(.{7})(.{7})(.{7})/) {
155                         $filesys{$n,'ublocks'} = int($1);
156                         $filesys{$n,'sblocks'} = int($2);
157                         $filesys{$n,'hblocks'} = int($3);
158                         $filesys{$n,'ufiles'} = int($4);
159                         $filesys{$n,'sfiles'} = int($5);
160                         $filesys{$n,'hfiles'} = int($6);
161                         $n++;
162                         }
163                 }
164         elsif (/^(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s.{0,15}\s(\d+)\s+(\d+)\s+(\d+)/ ||
165                /^(.{13})(.{7})(.{7})(.{7}).{12}(.{7})(.{7})(.{7})/) {
166                 $filesys{$n,'filesys'} = $1;
167                 $filesys{$n,'ublocks'} = int($2);
168                 $filesys{$n,'sblocks'} = int($3);
169                 $filesys{$n,'hblocks'} = int($4);
170                 $filesys{$n,'ufiles'} = int($5);
171                 $filesys{$n,'sfiles'} = int($6);
172                 $filesys{$n,'hfiles'} = int($7);
173                 $filesys{$n,'filesys'} =~ s/\s+$//g;
174                 $n++;
175                 }
176         }
177 close(QUOTA);
178 return $n;
179 }
180
181 # get_user_grace(filesystem)
182 # Returns an array containing  btime, bunits, ftime, funits
183 # The units can be 0=sec, 1=min, 2=hour, 3=day, 4=week, 5=month
184 sub get_user_grace
185 {
186 local(@rv);
187 $ENV{'EDITOR'} = $ENV{'VISUAL'} = "cat";
188 open(GRACE, "$config{'user_grace_command'} |");
189 while(<GRACE>) {
190         if (/^fs (\S+) blocks time limit = ([0-9\.]+) (\S+), files time limit = ([0-9\.]+) (\S+)/ && $1 eq $_[0]) {
191                 if ($2 == 0) { push(@rv, 0, 0); }
192                 else { push(@rv, $2, $name_to_unit{$3}); }
193                 if ($4 == 0) { push(@rv, 0, 0); }
194                 else { push(@rv, $4, $name_to_unit{$5}); }
195                 }
196         }
197 close(GRACE);
198 return @rv;
199 }
200
201 # default_grace()
202 # Returns 0 if grace time can be 0, 1 if zero grace means default
203 sub default_grace
204 {
205 return 1;
206 }
207
208 # edit_grace_file(data, filesystem, btime, bunits, ftime, funits)
209 sub edit_grace_file
210 {
211 local($rv, $line);
212 foreach $line (split(/\n/, $_[0])) {
213         if ($line =~ /^fs (\S+) blocks time limit = ([0-9\.]+) (\S+), files time limit = ([0-9\.]+) (\S+)/ && $1 eq $_[1]) {
214                 # replace this line
215                 $line = "fs $_[1] blocks time limit = $_[2] $unit_to_name{$_[3]}, files time limit = $_[4] $unit_to_name{$_[5]}";
216                 }
217         $rv .= "$line\n";
218         }
219 return $rv;
220 }
221
222 # grace_units()
223 # Returns an array of possible units for grace periods
224 sub grace_units
225 {
226 return ($text{'grace_seconds'}, $text{'grace_minutes'}, $text{'grace_hours'},
227         $text{'grace_days'}, $text{'grace_weeks'}, $text{'grace_months'});
228 }
229
230 %name_to_unit = ( "sec", 0, "secs", 0,
231                   "min", 1, "mins", 1,
232                   "hour", 2, "hours", 2,
233                   "day", 3, "days", 3,
234                   "week", 4, "weeks", 4,
235                   "month", 5, "months", 5
236                 );
237 foreach $k (keys %name_to_unit) {
238         $unit_to_name{$name_to_unit{$k}} = $k;
239         }
240
241 1;