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