Debian Codegraph @

contrib/ftpquota

raw
1 #!/usr/bin/env perl
2 # -------------------------------------------------------------------------
3 # Copyright (C) 2000-2017 TJ Saunders <[email protected]>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 # -------------------------------------------------------------------------
19
20 use strict;
21
22 use Fcntl qw(:flock);
23 use File::Basename qw(basename);
24 use Getopt::Long;
25 use IO::Seekable;
26
27 my $program = basename($0);
28 my $verbose = 0;
29
30 # These formats are highly dependent on the quota record definitions in
31 # mod_quotatab.h. Should those definitions change, these format strings will
32 # need to be adjusted accordingly.
33 my $limit_format = "Z81I1C1I1d3L3";
34 my $tally_format = "Z81I1d3L3";
35
36 # These sizes are determined by the sizes of the quota_limit_t and
37 # quota_tally_t structs (NOT using C's sizeof(), though -- alignment issues)
38 my $limit_reclen = 126;
39 my $tally_reclen = 121;
40
41 # the values for these variables is similarly dependent on the actual
42 # values defined in mod_quotatab.h
43 my $ALL_QUOTA = 10;
44 my $USER_QUOTA = 20;
45 my $GROUP_QUOTA = 30;
46 my $CLASS_QUOTA = 40;
47
48 my $HARD_LIMIT = 1;
49 my $SOFT_LIMIT = 2;
50
51 my $LIMIT_TABLE = 100;
52 my $TALLY_TABLE = 101;
53
54 my $LIMIT_MAGIC = hex(7626);
55 my $TALLY_MAGIC = hex(7644);
56
57 my $default_limit_table = "./ftpquota.limittab";
58 my $default_tally_table = "./ftpquota.tallytab";
59 my $default_name = "";
60 my $default_quota_type = $ALL_QUOTA;
61 my $default_limit_type = $HARD_LIMIT;
62 my $default_per_session = 0;
63 my $default_bytes_in = -1.0;
64 my $default_bytes_out = -1.0;
65 my $default_bytes_xfer = -1.0;
66 my $default_files_in = 0;
67 my $default_files_out = 0;
68 my $default_files_xfer = 0;
69
70 my $byte_units = "byte";
71
72 my %opts = ();
73 GetOptions(\%opts, 'Bu=n', 'bytes-upload=n', 'Bd=n', 'bytes-download=n',
74 'Bt=n', 'bytes-xfer=n', 'Fu=n', 'files-upload=n', 'Fd=n', 'files-download=n',
75 'Ft=n', 'files-xfer', 'L|limit-type=s', 'N|name=s', 'P|per-session',
76 'Q|quota-type=s', 'help', 'table-path=s', 'units=s', 'verbose', 'type=s',
77 'add-record', 'create-table', 'delete-record', 'show-records',
78 'update-record');
79
80 usage() if (defined($opts{'help'}));
81
82 my ($table, $table_type);
83 my %record = ();
84
85 parse_options();
86
87 if (defined($opts{'add-record'})) {
88 open_table();
89 wlock_table();
90 add_record();
91 unlock_table();
92 close_table();
93 exit 0;
94 }
95
96 if (defined($opts{'create-table'})) {
97 open_table();
98 close_table();
99 exit 0;
100 }
101
102 if (defined($opts{'delete-record'})) {
103 open_table();
104 wlock_table();
105 delete_record();
106 unlock_table();
107 close_table();
108 exit 0;
109 }
110
111 if (defined($opts{'show-records'})) {
112 open_table();
113 rlock_table();
114 print_table();
115 unlock_table();
116 close_table();
117 exit 0;
118 }
119
120 if (defined($opts{'update-record'})) {
121 open_table();
122 wlock_table();
123 update_record();
124 unlock_table();
125 close_table();
126 exit 0;
127 }
128
129 # if no operations have been specified, note this and exit
130 print STDOUT "$program: no operations to perform, exiting\n";
131
132 exit;
133
134 # -------------------------------------------------------------------------
135 sub add_record {
136 print STDOUT "$program: adding record\n" if $verbose;
137
138 my $add_quota_type;
139
140 # first, find the matching record. Note that this option requires
141 # both --name and --quota-type
142
143 if (!defined($opts{'Q'})) {
144 print STDOUT "$program: --add-record requires --quota-type option\n";
145 exit(1);
146
147 } else {
148 $add_quota_type = get_quota_type(quota_type => $opts{'Q'});
149 }
150
151 if ($add_quota_type != $ALL_QUOTA) {
152 if (!defined($opts{'N'})) {
153 print STDOUT "$program: --add-record requires --name option\n";
154 exit(1);
155 }
156 }
157
158 # now, see if there is a matching record already in the table
159 if (find_record(name => $opts{'N'}, quota_type => $add_quota_type)) {
160 print STDOUT "$program: unable to add record: matching record already exists\n";
161 exit(1);
162 }
163
164 # move to the end of the table, and write the new record
165 set_table_position(0, SEEK_END);
166
167 my $formatted_record;
168
169 if ($table_type == $LIMIT_TABLE) {
170 $formatted_record = pack($limit_format,
171 $record{'name'}, $record{'quota_type'},
172 $record{'per_session'}, $record{'limit_type'},
173 $record{'bytes_in'}, $record{'bytes_out'}, $record{'bytes_xfer'},
174 $record{'files_in'}, $record{'files_out'}, $record{'files_xfer'});
175
176 } elsif ($table_type == $TALLY_TABLE) {
177
178 $formatted_record = pack($tally_format,
179 $record{'name'}, $record{'quota_type'},
180 $record{'bytes_in'}, $record{'bytes_out'}, $record{'bytes_xfer'},
181 $record{'files_in'}, $record{'files_out'}, $record{'files_xfer'});
182 }
183
184 write_record(record => $formatted_record);
185 }
186
187 # -------------------------------------------------------------------------
188 sub check_table {
189 my $data;
190
191 print STDOUT "$program: checking table '$table'\n" if $verbose;
192
193 sysread TABLE, $data, 4;
194
195 # unpack what was read in
196 my ($magic) = unpack("L", $data);
197
198 if ($table_type == $LIMIT_TABLE) {
199
200 if ($magic != $LIMIT_MAGIC) {
201 print STDOUT "$program: bad header: $magic != $LIMIT_MAGIC\n" if
202 $verbose;
203 die "$program: mismatched table header, exiting\n";
204
205 } else {
206 print STDOUT "$program: table has correct header\n" if $verbose;
207 }
208
209 } elsif ($table_type == $TALLY_TABLE) {
210
211 if ($magic != $TALLY_MAGIC) {
212 print STDOUT "$program: bad header magics: $magic != $TALLY_MAGIC\n" if
213 $verbose;
214 die "$program: mismatched table header, exiting\n";
215
216 } else {
217 print STDOUT "$program: table has correct header\n" if $verbose;
218 }
219 }
220 }
221
222 # -------------------------------------------------------------------------
223 sub close_table {
224 print STDOUT "$program: closing table '$table'\n" if $verbose;
225
226 close(TABLE);
227 }
228
229 # -------------------------------------------------------------------------
230 sub delete_record {
231 print STDOUT "$program: deleting record\n" if $verbose;
232
233 my $delete_quota_type;
234
235 # first, find the matching record. Note that this option requires
236 # both --name and --quota-type
237
238 if (!defined($opts{'Q'})) {
239 print STDOUT "$program: --delete-record requires --quota-type option\n";
240 exit(1);
241
242 } else {
243 $delete_quota_type = get_quota_type(quota_type => $opts{'Q'});
244 }
245
246 if ($delete_quota_type != $ALL_QUOTA) {
247 if (!defined($opts{'N'})) {
248 print STDOUT "$program: --delete-record requires --name option\n";
249 exit(1);
250 }
251 }
252
253 # now, find the matching record in the table
254 unless (find_record(name => $opts{'N'}, quota_type => $delete_quota_type)) {
255 print STDOUT "$program: unable to delete record: no match found\n";
256 exit(1);
257 }
258 rewind_record();
259
260 print STDOUT "$program: modifying table '$table'\n" if $verbose;
261
262 # first, record the current file position
263 my $position = get_table_position();
264
265 # next, advance past this record to be deleted, and read in the
266 # rest of the file
267
268 seek_record(1);
269
270 my ($nrecords, @records) = read_table();
271
272 # Rewind the previous position, and "delete" that record by simply
273 # overwriting it. Note that this approach works for file tables --
274 # tables in other formats, such as SQL, will have a much easier time
275 # deleting a quota record.
276
277 set_table_position($position, SEEK_SET);
278
279 if ($nrecords > 0) {
280 for (my $i = 0; $i < $nrecords; $i++) {
281 write_record(record => $records[$i]);
282 }
283
284 # Truncate the table
285 truncate_table(len => get_table_position() - 4);
286
287 } else {
288
289 # Truncate the table
290 truncate_table(len => $position - 4);
291 }
292
293 # done
294 exit(0);
295 }
296
297 # -------------------------------------------------------------------------
298 sub find_record {
299 my %args = @_;
300 my $nrecords = 0;
301
302 my $search_name = $args{'name'};
303 my $search_quota_type = $args{'quota_type'};
304
305 while (my $record = read_record()) {
306 $nrecords++;
307
308 my ($name, $quota_type, @junk);
309
310 # unpack the binary record structure
311 if ($table_type == $LIMIT_TABLE) {
312 ($name, $quota_type, @junk) = unpack($limit_format, $record);
313
314 } elsif ($table_type == $TALLY_TABLE) {
315 ($name, $quota_type, @junk) = unpack($tally_format, $record);
316 }
317
318 # remove any NULs from $name
319 $name =~ s/(\0*)?//;
320
321 if ($search_quota_type == $ALL_QUOTA and
322 $quota_type == $ALL_QUOTA) {
323 return $record;
324
325 } elsif ($name eq $search_name and
326 $quota_type == $search_quota_type) {
327 return $record;
328 }
329 }
330
331 return undef;
332 }
333
334 # -------------------------------------------------------------------------
335 sub get_display_bytes {
336 my %args = @_;
337
338 my $bytes = $args{'bytes'};
339
340 if ($byte_units eq "byte") {
341 # no calculation necessary
342
343 unless ($bytes eq "unlimited" || $bytes <= 0) {
344 return sprintf "bytes:\t%.2f", $bytes;
345 } else {
346 return sprintf "bytes:\tunlimited";
347 }
348
349 } elsif ($byte_units eq "kilo") {
350 # divide by 1024.0
351 my $adj_bytes = $bytes / 1024.0;
352
353 unless ($bytes eq "unlimited" || $bytes <= 0) {
354 return sprintf "Kb:\t%.2f", $adj_bytes;
355 } else {
356 return sprintf "Kb:\tunlimited";
357 }
358
359 } elsif ($byte_units eq "mega") {
360 # divide by 1024.0 * 1024.0
361 my $adj_bytes = $bytes / (1024.0 * 1024.0);
362
363 unless ($bytes eq "unlimited" || $bytes <= 0) {
364 return sprintf "Mb:\t%.2f", $adj_bytes;
365 } else {
366 return sprintf "Mb:\tunlimited";
367 }
368
369 } elsif ($byte_units eq "giga") {
370 # divide by 1024.0 * 1024.0 * 1024.0
371 my $adj_bytes = $bytes / (1024.0 * 1024.0 * 1024.0);
372
373 unless ($bytes eq "unlimited" || $bytes <= 0) {
374 return sprintf "Gb:\t%.2f", $adj_bytes;
375 } else {
376 return sprintf "Gb:\tunlimited";
377 }
378 }
379 }
380
381 # -------------------------------------------------------------------------
382 sub get_quota_type {
383 my %args = @_;
384
385 my $quota_type = $args{'quota_type'};
386
387 # given the command-line --quote-type option value, return the
388 # corresponding numeric value
389
390 if ($quota_type eq "all") {
391 return $ALL_QUOTA;
392
393 } elsif ($quota_type eq "user") {
394 return $USER_QUOTA;
395
396 } elsif ($quota_type eq "group") {
397 return $GROUP_QUOTA;
398
399 } elsif ($quota_type eq "class") {
400 return $CLASS_QUOTA;
401
402 } else {
403 print STDERR "$program: unknown quota type: $quota_type\n";
404 exit(1);
405 }
406 }
407
408 # -------------------------------------------------------------------------
409 sub get_table_position {
410 return sysseek(TABLE, 0, 1);
411 }
412
413 # -------------------------------------------------------------------------
414 sub open_table {
415
416 # if the table doesn't exist, create it if requested
417 if (defined($opts{'create-table'})) {
418 die "$program: cannot create existing table\n" if (-e $table);
419
420 print STDOUT "$program: creating new table\n" if $verbose;
421
422 open(TABLE, "> $table") or
423 die "$program: unable to create $table: $!\n";
424
425 print STDOUT "$program: writing header for new table\n" if $verbose;
426
427 # write out the identifying header for the table type
428 if ($table_type == $LIMIT_TABLE) {
429 syswrite TABLE, pack("L", $LIMIT_MAGIC);
430
431 } elsif ($table_type == $TALLY_TABLE) {
432 syswrite TABLE, pack("L", $TALLY_MAGIC);
433 }
434
435 } else {
436
437 print STDOUT "$program: opening table '$table'\n" if $verbose;
438
439 if (defined($opts{'show-records'})) {
440 open(TABLE, "< $table") or
441 die "$program: unable to open $table: $!\n";
442
443 } else {
444 open(TABLE, "+< $table") or
445 die "$program: unable to open $table: $!\n";
446 }
447
448 check_table();
449 }
450 }
451
452 # -------------------------------------------------------------------------
453 sub parse_options {
454
455 $verbose = 1 if (defined($opts{'verbose'}));
456
457 if (defined($opts{'type'})) {
458
459 die "$program: unknown table type: $opts{'type'}\n" if
460 ($opts{'type'} ne 'limit' and $opts{'type'} ne 'tally');
461
462 if ($opts{'type'} eq 'limit') {
463 $table_type = $LIMIT_TABLE;
464 $table = $default_limit_table;
465 }
466
467 if ($opts{'type'} eq 'tally') {
468 $table_type = $TALLY_TABLE;
469 $table = $default_tally_table;
470 }
471
472 } else {
473 die "$program: missing required --type parameter\n";
474 }
475
476 $table = $opts{'table-path'} if (defined($opts{'table-path'}));
477
478 if (defined($opts{'N'})) {
479 $record{'name'} = $opts{'N'};
480
481 # make sure the name is not too long
482 die "$program: name is too long, must be less than 80 characters\n"
483 if (length($record{'name'}) >= 80);
484
485 } else {
486 $record{'name'} = $default_name;
487 }
488
489 if (defined($opts{'P'})) {
490 $record{'per_session'} = 1;
491
492 } else {
493 $record{'per_session'} = $default_per_session;
494 }
495
496 if (defined($opts{'Q'})) {
497 if ($opts{'Q'} eq "all") {
498 $record{'quota_type'} = $ALL_QUOTA;
499
500 } elsif ($opts{'Q'} eq "user") {
501 $record{'quota_type'} = $USER_QUOTA;
502
503 # it's an error if there's no -N option present as well
504 unless (defined($opts{'N'})) {
505 print STDOUT "$program: quota type \"user\" requires --name option\n";
506 exit(1);
507 }
508
509 } elsif ($opts{'Q'} eq "group") {
510 $record{'quota_type'} = $GROUP_QUOTA;
511
512 # it's an error if there's no -N option present as well
513 unless (defined($opts{'N'})) {
514 print STDOUT "$program: quota type \"group\" requires --name option\n";
515 exit(1);
516 }
517
518 } elsif ($opts{'Q'} eq "class") {
519 $record{'quota_type'} = $CLASS_QUOTA;
520
521 # it's an error if there's no -N option present as well
522 unless (defined($opts{'N'})) {
523 print STDOUT "$program: quota type \"class\" requires --name option\n";
524 exit(1);
525 }
526
527 } else {
528 print STDERR "$program: unknown quota type: $opts{'Q'}\n";
529 exit(1);
530 }
531
532 } else {
533 $record{'quota_type'} = $ALL_QUOTA;
534 }
535
536 if (defined($opts{'L'})) {
537 if ($opts{'L'} eq "hard") {
538 $record{'limit_type'} = $HARD_LIMIT;
539
540 } elsif ($opts{'L'} eq "soft") {
541 $record{'limit_type'} = $SOFT_LIMIT;
542
543 } else {
544 print STDERR "$program: unknown limit type: $opts{'L'}\n";
545 exit(1);
546 }
547
548 } else {
549 $record{'limit_type'} = $HARD_LIMIT;
550 }
551
552 # process the --units option, if present
553 if (defined($opts{'units'})) {
554
555 if ($opts{'units'} eq "B" or $opts{'units'} eq "byte") {
556 $byte_units = "byte";
557
558 } elsif ($opts{'units'} eq "Kb" or $opts{'units'} eq "kilo") {
559 $byte_units = "kilo";
560
561 } elsif ($opts{'units'} eq "Mb" or $opts{'units'} eq "mega") {
562 $byte_units = "mega";
563
564 } elsif ($opts{'units'} eq "Gb" or $opts{'units'} eq "giga") {
565 $byte_units = "giga";
566
567 } else {
568 print STDERR "$program: unknown unit type: $opts{'units'}\n";
569 exit(1);
570 }
571 }
572
573 if (defined($opts{'Bu'}) || defined($opts{'bytes-upload'})) {
574 if (defined($opts{'Bu'}) && defined($opts{'bytes-upload'})) {
575 print STDOUT "$program: do not use both --Bu and --bytes-upload options\n";
576 exit(1);
577 }
578
579 $record{'bytes_in'} = $opts{'Bu'} if (defined($opts{'Bu'}));
580 $record{'bytes_in'} = $opts{'bytes-upload'} if
581 (defined($opts{'bytes-upload'}));
582
583 if ($record{'bytes_in'} <= 0.0) {
584 print STDERR "$program: notice: upload bytes value '", $record{'bytes_in'}, "' means 'unlimited'\n";
585 }
586
587 if ($record{'bytes_in'} != $default_bytes_in) {
588 if ($byte_units eq "kilo") {
589 $record{'bytes_in'} = $record{'bytes_in'} * 1024.0;
590
591 } elsif ($byte_units eq "mega") {
592 $record{'bytes_in'} = $record{'bytes_in'} * 1024.0 * 1024.0;
593
594 } elsif ($byte_units eq "giga") {
595 $record{'bytes_in'} = $record{'bytes_in'} * 1024.0 * 1024.0 * 1024.0;
596 }
597 }
598
599 } else {
600
601 $record{'bytes_in'} = $default_bytes_in if ($table_type == $LIMIT_TABLE);
602 $record{'bytes_in'} = 0.0 if ($table_type == $TALLY_TABLE);
603 }
604
605 if (defined($opts{'Bd'}) || defined($opts{'bytes-download'})) {
606 if (defined($opts{'Bd'}) && defined($opts{'bytes-download'})) {
607 print STDOUT "$program: do not use both --Bd and --bytes-download options\n";
608 exit(1);
609 }
610
611 $record{'bytes_out'} = $opts{'Bd'} if (defined($opts{'Bd'}));
612 $record{'bytes_out'} = $opts{'bytes-download'} if
613 (defined($opts{'bytes-download'}));
614
615 if ($record{'bytes_out'} <= 0.0) {
616 print STDERR "$program: notice: download bytes value '", $record{'bytes_out'}, "' means 'unlimited'\n";
617 }
618
619 if ($record{'bytes_out'} != $default_bytes_out) {
620 if ($byte_units eq "kilo") {
621 $record{'bytes_out'} = $record{'bytes_out'} * 1024.0;
622
623 } elsif ($byte_units eq "mega") {
624 $record{'bytes_out'} = $record{'bytes_out'} * 1024.0 * 1024.0;
625
626 } elsif ($byte_units eq "giga") {
627 $record{'bytes_out'} = $record{'bytes_out'} * 1024.0 * 1024.0 * 1024.0;
628 }
629 }
630
631 } else {
632
633 $record{'bytes_out'} = $default_bytes_out if ($table_type == $LIMIT_TABLE);
634 $record{'bytes_out'} = 0.0 if ($table_type == $TALLY_TABLE);
635 }
636
637 if (defined($opts{'Bx'}) || defined($opts{'bytes-xfer'})) {
638 if (defined($opts{'Bx'}) && defined($opts{'bytes-xfer'})) {
639 print STDOUT "$program: do not use both --Bx and --bytes-xfer options\n";
640 exit(1);
641 }
642
643 $record{'bytes_xfer'} = $opts{'Bx'} if (defined($opts{'Bx'}));
644 $record{'bytes_xfer'} = $opts{'bytes-xfer'} if
645 (defined($opts{'bytes-xfer'}));
646
647 if ($record{'bytes_xfer'} <= 0.0) {
648 print STDERR "$program: notice: transfer bytes value '", $record{'bytes_xfer'}, "' means 'unlimited'\n";
649 }
650
651 if ($record{'bytes_xfer'} != $default_bytes_xfer) {
652 if ($byte_units eq "kilo") {
653 $record{'bytes_xfer'} = $record{'bytes_xfer'} * 1024.0;
654
655 } elsif ($byte_units eq "mega") {
656 $record{'bytes_xfer'} = $record{'bytes_xfer'} * 1024.0 * 1024.0;
657
658 } elsif ($byte_units eq "giga") {
659 $record{'bytes_xfer'} =
660 $record{'bytes_xfer'} * 1024.0 * 1024.0 * 1024.0;
661 }
662 }
663
664 } else {
665
666 $record{'bytes_xfer'} = $default_bytes_xfer if
667 ($table_type == $LIMIT_TABLE);
668 $record{'bytes_xfer'} = 0.0 if ($table_type == $TALLY_TABLE);
669 }
670
671 if (defined($opts{'Fu'}) || defined($opts{'files-upload'})) {
672 if (defined($opts{'Fu'}) && defined($opts{'files-upload'})) {
673 print STDOUT "$program: do not use both --Fu and --files-upload options\n";
674 exit(1);
675 }
676
677 $record{'files_in'} = $opts{'Fu'} if (defined($opts{'Fu'}));
678 $record{'files_in'} = $opts{'files-upload'} if
679 (defined($opts{'files-upload'}));
680
681 if ($record{'files_in'} <= 0) {
682 print STDERR "$program: notice: upload files value '", $record{'files_in'}, "' means 'unlimited'\n";
683 }
684
685 } else {
686
687 $record{'files_in'} = $default_files_in if ($table_type == $LIMIT_TABLE);
688 $record{'files_in'} = 0 if ($table_type == $TALLY_TABLE);
689 }
690
691 if (defined($opts{'Fd'}) || defined($opts{'files-download'})) {
692 if (defined($opts{'Fd'}) && defined($opts{'files-download'})) {
693 print STDOUT "$program: do not use both --Fd and --files-download options\n";
694 exit(1);
695 }
696
697 $record{'files_out'} = $opts{'Fd'} if (defined($opts{'Fd'}));
698 $record{'files_out'} = $opts{'files-download'} if
699 (defined($opts{'files-download'}));
700
701 if ($record{'files_out'} <= 0) {
702 print STDERR "$program: notice: download files value '", $record{'files_out'}, "' means 'unlimited'\n";
703 }
704
705 } else {
706
707 $record{'files_out'} = $default_files_out if ($table_type == $LIMIT_TABLE);
708 $record{'files_out'} = 0 if ($table_type == $TALLY_TABLE);
709 }
710
711 if (defined($opts{'Fx'}) || defined($opts{'files-xfer'})) {
712 if (defined($opts{'Fx'}) && defined($opts{'files-xfer'})) {
713 print STDOUT "$program: do not use both --Fx and --files-xfer options\n";
714 exit(1);
715 }
716
717 $record{'files_xfer'} = $opts{'Fx'} if (defined($opts{'Fx'}));
718 $record{'files_xfer'} = $opts{'files-xfer'} if
719 (defined($opts{'files-xfer'}));
720
721 if ($record{'files_xfer'} <= 0) {
722 print STDERR "$program: notice: transfer files value '", $record{'files_xfer'}, "' means 'unlimited'\n";
723 }
724
725 } else {
726
727 $record{'files_xfer'} = $default_files_xfer if
728 ($table_type == $LIMIT_TABLE);
729 $record{'files_xfer'} = 0 if ($table_type == $TALLY_TABLE);
730 }
731 }
732
733 # -------------------------------------------------------------------------
734 sub print_record {
735 my %args = @_;
736
737 my $record = $args{'record'};
738
739 my ($name, $quota_type, $per_session, $limit_type, $bytes_in, $bytes_out,
740 $bytes_xfer, $files_in, $files_out, $files_xfer);
741
742 if ($table_type == $LIMIT_TABLE) {
743 ($name, $quota_type, $per_session, $limit_type, $bytes_in, $bytes_out,
744 $bytes_xfer, $files_in, $files_out, $files_xfer) = unpack($limit_format,
745 $record);
746
747 } elsif ($table_type == $TALLY_TABLE) {
748
749 ($name, $quota_type, $bytes_in, $bytes_out, $bytes_xfer,
750 $files_in, $files_out, $files_xfer) = unpack($tally_format, $record);
751 }
752
753
754 # make the symbolic values legible
755 $quota_type = "All" if ($quota_type == $ALL_QUOTA);
756 $quota_type = "User" if ($quota_type == $USER_QUOTA);
757 $quota_type = "Group" if ($quota_type == $GROUP_QUOTA);
758 $quota_type = "Class" if ($quota_type == $CLASS_QUOTA);
759
760 if ($table_type == $LIMIT_TABLE) {
761 $limit_type = "Hard" if ($limit_type == $HARD_LIMIT);
762 $limit_type = "Soft" if ($limit_type == $SOFT_LIMIT);
763
764 if ($per_session eq 1) {
765 $per_session = "True";
766
767 } else {
768 $per_session = "False";
769 }
770 }
771
772 # print "unlimited" if quota is unlimited, but only for limit tables
773 if ($table_type == $LIMIT_TABLE) {
774 $bytes_in = "unlimited" if ($bytes_in <= 0.0);
775 $bytes_out = "unlimited" if ($bytes_out <= 0.0);
776 $bytes_xfer = "unlimited" if ($bytes_xfer <= 0.0);
777
778 $files_in = "unlimited" if ($files_in <= 0);
779 $files_out = "unlimited" if ($files_out <= 0);
780 $files_xfer = "unlimited" if ($files_xfer <= 0);
781 }
782
783 print STDOUT "-------------------------------------------\n";
784 print STDOUT " Name: $name\n";
785 print STDOUT " Quota Type: $quota_type\n";
786
787 if ($table_type == $LIMIT_TABLE) {
788 print STDOUT " Per Session: $per_session\n";
789 print STDOUT " Limit Type: $limit_type\n";
790 }
791
792 print STDOUT " Uploaded ", get_display_bytes('bytes' => $bytes_in), "\n";
793 print STDOUT " Downloaded ", get_display_bytes('bytes' => $bytes_out),
794 "\n";
795 print STDOUT " Transferred ", get_display_bytes('bytes' => $bytes_xfer),
796 "\n";
797
798 print STDOUT " Uploaded files:\t$files_in\n";
799 print STDOUT " Downloaded files:\t$files_out\n";
800 print STDOUT " Transferred files:\t$files_xfer\n";
801 }
802
803 # -------------------------------------------------------------------------
804 sub print_table {
805 my $have_records = 0;
806
807 while (my $record = read_record()) {
808 $have_records = 1;
809 print_record(record => $record);
810 }
811 print STDOUT "$program: (empty table)\n" unless ($have_records);
812
813 # done
814 exit(0);
815 }
816
817 # -------------------------------------------------------------------------
818 sub read_record {
819 my $record;
820 my $bread;
821
822 if ($table_type == $LIMIT_TABLE) {
823 $bread = sysread TABLE, $record, $limit_reclen;
824
825 } elsif ($table_type == $TALLY_TABLE) {
826 $bread = sysread TABLE, $record, $tally_reclen;
827 }
828
829 if ($bread) {
830 return $record;
831
832 } else {
833 return undef;
834 }
835 }
836
837 # -------------------------------------------------------------------------
838 sub read_table {
839 my @records = ();
840 my $nrecords_read = 0;
841
842 # read from TABLE
843 while (my $record = read_record()) {
844 $nrecords_read++;
845 push(@records, $record);
846 }
847
848 return ($nrecords_read, @records);
849 }
850
851 # -------------------------------------------------------------------------
852 sub rewind_record {
853 if ($table_type == $LIMIT_TABLE) {
854 sysseek TABLE, -$limit_reclen, 1;
855
856 } elsif ($table_type == $TALLY_TABLE) {
857 sysseek TABLE, -$tally_reclen, 1;
858 }
859 }
860
861 # -------------------------------------------------------------------------
862 sub rlock_table {
863 print STDOUT "$program: read-locking table '$table'\n" if $verbose;
864
865 flock(TABLE, LOCK_SH);
866 }
867
868 # -------------------------------------------------------------------------
869 sub seek_record {
870 my ($n) = @_;
871
872 if ($table_type == $LIMIT_TABLE) {
873 sysseek TABLE, $limit_reclen, $n;
874
875 } elsif ($table_type == $TALLY_TABLE) {
876 sysseek TABLE, $tally_reclen, $n;
877 }
878 }
879
880 # -------------------------------------------------------------------------
881 sub set_table_position {
882 my ($position, $whence) = @_;
883
884 my $result = sysseek(TABLE, $position, $whence);
885
886 print STDOUT "$program: set table position to $result\n" if $verbose;
887
888 return $result;
889 }
890
891 # -------------------------------------------------------------------------
892 sub truncate_table {
893 my %args = @_;
894
895 my $length = $args{'len'};
896
897 # don't forget about the header (4 bytes)
898 truncate TABLE, $length + 4;
899 }
900
901 # -------------------------------------------------------------------------
902 sub unlock_table {
903 print STDOUT "$program: unlocking table '$table'\n" if $verbose;
904
905 flock(TABLE, LOCK_UN);
906 }
907
908 # -------------------------------------------------------------------------
909 sub update_record {
910 my $current_record;
911 my $search_quota_type;
912
913 # first, find the matching record. Note that this option requires
914 # both --name and --quota-type
915
916 if (!defined($opts{'Q'})) {
917 print STDOUT "$program: --update-record requires --quota-type option\n";
918 exit(1);
919
920 } else {
921 $search_quota_type = get_quota_type(quota_type => $opts{'Q'});
922 }
923
924 if ($search_quota_type != $ALL_QUOTA) {
925 if (!defined($opts{'N'})) {
926 print STDOUT "$program: --update-record requires --name option\n";
927 exit(1);
928 }
929 }
930
931 # now, find the matching record in the table
932 open_table();
933 unless ($current_record = find_record(name => $opts{'N'},
934 quota_type => $search_quota_type)) {
935 print STDOUT "$program: unable to update record: no match found\n";
936 exit(1);
937 }
938 rewind_record();
939
940 print STDOUT "$program: updating table '$table'\n" if $verbose;
941
942 # now, adjust the current record with the updated values in the requested
943 # record
944
945 my ($name, $quota_type, $per_session, $limit_type, @junk);
946
947 if ($table_type == $LIMIT_TABLE) {
948 ($name, $quota_type, $per_session, $limit_type,
949 @junk) = unpack($limit_format, $current_record);
950
951 } elsif ($table_type == $TALLY_TABLE) {
952
953 ($name, $quota_type, @junk) = unpack($tally_format, $current_record);
954 }
955
956 my $formatted_record;
957
958 if ($table_type == $LIMIT_TABLE) {
959 $formatted_record = pack($limit_format, $name, $quota_type,
960 $record{'per_session'}, $record{'limit_type'},
961 $record{'bytes_in'}, $record{'bytes_out'}, $record{'bytes_xfer'},
962 $record{'files_in'}, $record{'files_out'}, $record{'files_xfer'});
963
964 } elsif ($table_type == $TALLY_TABLE) {
965
966 $formatted_record = pack($tally_format, $name, $quota_type,
967 $record{'bytes_in'}, $record{'bytes_out'}, $record{'bytes_xfer'},
968 $record{'files_in'}, $record{'files_out'}, $record{'files_xfer'});
969 }
970
971 write_record(record => $formatted_record);
972
973 # done
974 exit(0);
975 }
976
977 # -------------------------------------------------------------------------
978 sub wlock_table {
979 print STDOUT "$program: write-locking table '$table'\n" if $verbose;
980
981 flock(TABLE, LOCK_EX);
982 }
983
984 # -------------------------------------------------------------------------
985 sub write_record {
986 my %args = @_;
987 my $record = $args{'record'};
988
989 print STDOUT "$program: writing record\n" if $verbose;
990
991 die "$program: error writing table: $!\n" unless
992 syswrite TABLE, $record;
993 }
994
995 # -------------------------------------------------------------------------
996 sub usage {
997
998 print STDOUT <<END_OF_USAGE;
999
1000 usage: $program [options]
1001
1002 The following options describe the type of operation to be performed:
1003
1004 --add-record Create a new record with the specified limits. Any
1005 limits left unspecified with have their default
1006 values. This option requires the --name and
1007 --quota-type options.
1008
1009 --create-table Create the table if not present. Used to initialize
1010 a table. The default limit table path is
1011 "$default_limit_table". The default tally table path is
1012 "$default_tally_table".
1013
1014 --delete-record Deletes a quota record from the table. This option
1015 requires the --name and --quote-type options.
1016
1017 --show-records Prints out all of the quota records in the table in
1018 a legible format.
1019
1020 --update-record Updates a quota record with the specified limits. Any
1021 limits left unspecified will be updated with their
1022 default value. This option requires the --name and
1023 --quota-type options.
1024
1025 The following option describes the type of table on which to operate:
1026
1027 --type Specifies a table type to use. The allowable options
1028 are: "limit" or "tally". This is required.
1029
1030 The following options are used to specify specific quota limits:
1031
1032 --Bu Specifies the limit of the number of bytes that may be
1033 --bytes-upload uploaded. Defaults to -1 (unlimited). Note that any
1034 value less than or equal to zero is treated as
1035 "unlimited".
1036
1037 --Bd Specifies the limit of the number of bytes that may be
1038 --bytes-download downloaded. Defaults to -1 (unlimited). Note that any
1039 value less than or equal to zero is treated as
1040 "unlimited".
1041
1042 --Bx Specifies the limit of the number of bytes that may be
1043 --bytes-xfer transferred. Note that this total includes uploads,
1044 downloads, AND directory listings. Defaults to
1045 -1 (unlimited). Note that any value value less than or
1046 equal to zero is treated as "unlimited".
1047
1048 --Fu Specifies the limit of the number of files that may be
1049 --files-upload uploaded. Defaults to -1 (unlimited). Note that any
1050 value less than or equal to zero is treated as
1051 "unlimited".
1052
1053 --Fd Specifies the limit of the number of files that may be
1054 --files-download downloaded. Defaults to -1 (unlimited). Note that any
1055 value less than or equal to zero is treated as
1056 "unlimited".
1057
1058 --Fx Specifies the limit of the number of files that may be
1059 --files-xfer transferred, including uploads and downloads. Defaults
1060 to -1 (unlimited). Note that any value less than or
1061 equal to zero is treated as "unlimited".
1062
1063 -L Specifies the type of limit, "hard" or "soft", of
1064 --limit-type the bytes limits. If "hard", any uploaded files that
1065 push the bytes used counter past the limit will be
1066 automatically deleted; if "soft", those extra bytes
1067 will be allowed, but future uploads will be denied.
1068 This option only makes sense if --type is "limit".
1069
1070 -N Specifies a name for the quota record. This name
1071 --name will be the user/login name, group name, or class
1072 name, depending on the quota type. This option
1073 is ignored if the quota type specified is "all".
1074
1075 -P
1076 --per-session Specifies that the quota limit is to be applied only
1077 to each session, rather than persisting across
1078 sessions. By default, quotas are persistent.
1079
1080 -Q Specifies a "quota type" for this record, where
1081 --quota-type the type means to which category of FTP users this
1082 quota will apply. The quota type must be one of:
1083 "user", "group", "class", or "all".
1084
1085 The following options are miscellaneous:
1086
1087 --help Displays this message.
1088
1089 --table-path Specifies the path to a quota table file to use.
1090
1091 --units Specifies whether to treats bytes as is, in kilobytes,
1092 megabytes, or gigabytes. Allowable options are:
1093 "B" or "byte", "Kb" or "kilo", "Mb" or "mega",
1094 and "Gb" or "giga". Defaults to "byte".
1095
1096 --verbose Toggles more verbose information about the doings of
1097 the tool as it works.
1098
1099 END_OF_USAGE
1100
1101 exit 0;
1102 }
1103
1104 # -------------------------------------------------------------------------
1105