Handle hostnames with upper-case letters
[webmin.git] / quota / unixware-lib.pl
1 # unixware-lib.pl
2 # Quota functions for UnixWare 7
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 "ufs" || $_[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, %hasu, $u);
55 $rep = &backquote_logged("$config{'user_repquota_command'} $_[0] 2>&1");
56 if ($?) { return -1; }
57 setpwent();
58 while(@uinfo = getpwent()) {
59         $hasu{$uinfo[0]}++;
60         }
61 endpwent() if ($gconfig{'os_type'} ne 'hpux');
62 @rep = split(/\n/, $rep); @rep = @rep[3..$#rep];
63 for($n=0; $n<@rep; $n++) {
64         if ($rep[$n] =~ /(\S+)\s+..\s+(\d+)\s+(\d+)\s+(\d+).*(\d+)\s+(\d+)\s+(\d+)/ || $rep[$n] =~ /(\S+)\s+..(.{7})(.{7})(.{7}).{13}(.{7})(.{7})(.{7})/) {
65                 $user{$n,'user'} = $1;
66                 $user{$n,'ublocks'} = int($2);
67                 $user{$n,'sblocks'} = int($3);
68                 $user{$n,'hblocks'} = int($4);
69                 $user{$n,'ufiles'} = int($5);
70                 $user{$n,'sfiles'} = int($6);
71                 $user{$n,'hfiles'} = int($7);
72                 $user{$n,'user'} =~ s/^#//g;
73                 if ($user{$n,'user'} !~ /^\d+$/ && !$hasu{$user{$n,'user'}}) {
74                         # Username was truncated! Try to find him..
75                         foreach $u (keys %hasu) {
76                                 if (substr($u, 0, length($user{$n,'user'})) eq
77                                     $user{$n,'user'}) {
78                                         # found him..
79                                         $user{$n,'user'} = $u;
80                                         last;
81                                         }
82                                 }
83                         }
84                 }
85         }
86 return $n;
87 }
88
89 # edit_quota_file(data, filesys, sblocks, hblocks, sfiles, hfiles)
90 sub edit_quota_file
91 {
92 local($rv, $line);
93 foreach $line (split(/\n/, $_[0])) {
94         if ($line =~ /^fs (\S+) blocks \(soft = (\d+), hard = (\d+)\) inodes \(soft = (\d+), hard = (\d+)\)$/ && $1 eq $_[1]) {
95                 # found line to change
96                 $line = "fs $_[1] blocks (soft = $_[2], hard = $_[3]) inodes (soft = $_[4], hard = $_[5])";
97                 }
98         $rv .= "$line\n";
99         }
100 return $rv;
101 }
102
103 # quotaon(filesystem, mode)
104 # Activate quotas and create quota file for some filesystem. The mode can
105 # be 1 for user only, 2 for group only or 3 for user and group
106 sub quotaon
107 {
108 return if (&is_readonly_mode());
109 local($qf, $out);
110 $qf = "$_[0]/quotas";
111 if (!(-r $qf)) {
112         &open_tempfile(QUOTAFILE, ">$qf", 0, 1);
113         &close_tempfile(QUOTAFILE);
114         &set_ownership_permissions(undef, undef, 0600, $qf);
115         }
116 $out = &backquote_logged("$config{'user_quotaon_command'} $_[0] 2>&1");
117 if ($?) { return $out; }
118 return undef;
119 }
120
121 # quotaoff(filesystem, mode)
122 # Turn off quotas for some filesystem
123 sub quotaoff
124 {
125 return if (&is_readonly_mode());
126 local($out);
127 $out = &backquote_logged("$config{'user_quotaoff_command'} $_[0] 2>&1");
128 if ($?) { return $out; }
129 return undef;
130 }
131
132 # quotacheck(filesystem, mode)
133 # Runs quotacheck on some filesystem
134 sub quotacheck
135 {
136 $out = &backquote_logged("$config{'quotacheck_command'} $_[0] 2>&1");
137 if ($?) { return $out; }
138 return undef;
139 }
140
141 # copy_user_quota(user, [user]+)
142 # Copy the quotas for some user to many others
143 sub copy_user_quota
144 {
145 for($i=1; $i<@_; $i++) {
146         $out = &backquote_logged("$config{'user_copy_command'} ".
147                                 quotemeta($_[0])." ".quotemeta($_[$i])." 2>&1");
148         if ($?) { return $out; }
149         }
150 return undef;
151 }
152
153 # user_filesystems(user)
154 # Fills the array %filesys with details of all filesystems some user has
155 # quotas on
156 sub user_filesystems
157 {
158 local($n, $_);
159 open(QUOTA, "$config{'user_quota_command'} ".quotemeta($_[0])." |");
160 $n=0; while(<QUOTA>) {
161         chop;
162         if (/^(Disk|Filesystem)/) { next; }
163         if (/^(\S+)$/) {
164                 # Bogus wrapped line!
165                 $filesys{$n,'filesys'} = $1;
166                 <QUOTA> =~ /^.{13}(.{7})(.{7})(.{7}).{12}(.{7})(.{7})(.{7})/;
167                 $filesys{$n,'ublocks'} = int($1);
168                 $filesys{$n,'sblocks'} = int($2);
169                 $filesys{$n,'hblocks'} = int($3);
170                 $filesys{$n,'ufiles'} = int($4);
171                 $filesys{$n,'sfiles'} = int($5);
172                 $filesys{$n,'hfiles'} = int($6);
173                 $n++;
174                 }
175         elsif (/^(.{13})(.{7})(.{7})(.{7}).{12}(.{7})(.{7})(.{7})/) {
176                 $filesys{$n,'filesys'} = $1;
177                 $filesys{$n,'ublocks'} = int($2);
178                 $filesys{$n,'sblocks'} = int($3);
179                 $filesys{$n,'hblocks'} = int($4);
180                 $filesys{$n,'ufiles'} = int($5);
181                 $filesys{$n,'sfiles'} = int($6);
182                 $filesys{$n,'hfiles'} = int($7);
183                 $filesys{$n,'filesys'} =~ s/\s+$//g;
184                 $n++;
185                 }
186         }
187 close(QUOTA);
188 return $n;
189 }
190
191 # get_user_grace(filesystem)
192 # Returns an array containing  btime, bunits, ftime, funits
193 # The units can be 0=sec, 1=min, 2=hour, 3=day, 4=week, 5=month
194 sub get_user_grace
195 {
196 local(@rv);
197 $ENV{'EDITOR'} = $ENV{'VISUAL'} = "cat";
198 open(GRACE, "$config{'user_grace_command'} |");
199 while(<GRACE>) {
200         if (/^fs (\S+) blocks time limit = ([0-9\.]+) (\S+), files time limit = ([0-9\.]+) (\S+)/ && $1 eq $_[0]) {
201                 if ($2 == 0) { push(@rv, 0, 0); }
202                 else { push(@rv, $2, $name_to_unit{$3}); }
203                 if ($4 == 0) { push(@rv, 0, 0); }
204                 else { push(@rv, $4, $name_to_unit{$5}); }
205                 }
206         }
207 close(GRACE);
208 return @rv;
209 }
210
211 # default_grace()
212 # Returns 0 if grace time can be 0, 1 if zero grace means default
213 sub default_grace
214 {
215 return 1;
216 }
217
218 # edit_grace_file(data, filesystem, btime, bunits, ftime, funits)
219 sub edit_grace_file
220 {
221 local($rv, $line);
222 foreach $line (split(/\n/, $_[0])) {
223         if ($line =~ /^fs (\S+) blocks time limit = ([0-9\.]+) (\S+), files time limit = ([0-9\.]+) (\S+)/ && $1 eq $_[1]) {
224                 # replace this line
225                 $line = "fs $_[1] blocks time limit = $_[2] $unit_to_name{$_[3]}, files time limit = $_[4] $unit_to_name{$_[5]}";
226                 }
227         $rv .= "$line\n";
228         }
229 return $rv;
230 }
231
232 # grace_units()
233 # Returns an array of possible units for grace periods
234 sub grace_units
235 {
236 return ($text{'grace_seconds'}, $text{'grace_minutes'}, $text{'grace_hours'},
237         $text{'grace_days'}, $text{'grace_weeks'}, $text{'grace_months'});
238 }
239
240 %name_to_unit = ( "sec", 0, "secs", 0,
241                   "min", 1, "mins", 1,
242                   "hour", 2, "hours", 2,
243                   "day", 3, "days", 3,
244                   "week", 4, "weeks", 4,
245                   "month", 5, "months", 5
246                 );
247 foreach $k (keys %name_to_unit) {
248         $unit_to_name{$name_to_unit{$k}} = $k;
249         }
250
251 1;
252