Debian Codegraph @

contrib/mod_quotatab.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_quotatab -- a module for managing FTP byte/file quotas via
3 * centralized tables
4 * Copyright (c) 2001-2025 TJ Saunders
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19 *
20 * As a special exemption, TJ Saunders gives permission to link this program
21 * with OpenSSL, and distribute the resulting executable, without including
22 * the source code for OpenSSL in the source distribution.
23 *
24 * This is mod_quotatab, contrib software for proftpd 1.2 and above.
25 * For more information contact TJ Saunders <[email protected]>. It is based
26 * the ideas in Eric Estabrook's mod_quota, available from
27 * ftp://pooh.urbanrage.com/pub/c/. This module, however, has been written
28 * from scratch to implement quotas in a different way.
29 */
30
31 #include "mod_quotatab.h"
32
33 typedef struct regtab_obj {
34 struct regtab_obj *prev, *next;
35
36 /* Table source type name */
37 const char *regtab_name;
38
39 /* Initialization function for this type of table source */
40 quota_table_t *(*regtab_open)(pool *, quota_tabtype_t, const char *);
41
42 /* Flags for this type of table: QUOTATAB_LIMIT_SRC, QUOTATAB_TALLY_SRC.
43 * Used to restrict some sources to only certain table types (ie
44 * LDAP to limit tables).
45 */
46 unsigned int regtab_srcs;
47
48 } quota_regtab_t;
49
50 module quotatab_module;
51
52 /* Quota objects for the current session */
53 static quota_type_t sess_quota_type;
54
55 quota_deltas_t quotatab_deltas;
56 static quota_table_t *limit_tab = NULL;
57 static quota_limit_t sess_limit;
58
59 static quota_table_t *tally_tab = NULL;
60 static quota_tally_t sess_tally;
61
62 /* Memory pools for this module */
63 static pool *quotatab_pool = NULL;
64 static pool *quotatab_backend_pool = NULL;
65
66 /* List of registered quotatab sources */
67 static quota_regtab_t *quotatab_backends = NULL;
68 static unsigned int quotatab_nbackends = 0;
69
70 /* Logging data */
71 static int quota_logfd = -1;
72 static char *quota_logname = NULL;
73
74 static unsigned char allow_site_quota = TRUE;
75 static unsigned char use_dirs = FALSE;
76 static unsigned char use_quotas = FALSE;
77 static unsigned char have_quota_entry = FALSE;
78 static unsigned char have_quota_limit_table = FALSE;
79 static unsigned char have_quota_tally_table = FALSE;
80
81 static unsigned long have_quota_update = 0;
82 #define QUOTA_HAVE_READ_UPDATE 10000
83 #define QUOTA_HAVE_WRITE_UPDATE 20000
84
85 #if defined(PR_USE_REGEX)
86 static pr_regex_t *quota_exclude_pre = NULL;
87 static const char *quota_exclude_filter = NULL;
88 #endif
89
90 static quota_units_t byte_units = BYTE;
91
92 /* For transmitting number of bytes/files from PRE_CMD to POST_CMD handlers
93 * (for use by APPE, DELE, MKD, RMD, RNTO, STOR, STOU, XMKD, and XRMD
94 * commands)
95 */
96 static off_t quotatab_disk_nbytes;
97 static unsigned int quotatab_disk_nfiles;
98
99 /* For handling deletes of files which not belong to us. */
100 static struct stat quotatab_dele_st;
101 static int quotatab_have_dele_st = FALSE;
102
103 /* For locking (e.g. during tally creation). */
104 static int quota_lockfd = -1;
105
106 #define QUOTA_MAX_LOCK_ATTEMPTS 10
107
108 /* Used to indicate whether a transfer was aborted via the ABORT command.
109 * This is needed, in conjunction with checking the SF_ABORT/SF_POST_ABORT
110 * session flags, since it is possible for clients to abort transfers without
111 * using the OOB signal byte.
112 */
113 static unsigned char have_aborted_transfer = FALSE;
114
115 /* It is the case where sometimes a command may be denied by a PRE_CMD
116 * handler of this module, in which case an appropriate error response is
117 * added to the response chain. When the matching POST_CMD_ERR handler
118 * then runs, it will add another response. Sometimes, however, the
119 * POST_CMD_ERR handlers may be run for other reasons, and the response
120 * added is not a duplicate. Use this flag to signal when a response has
121 * already been added.
122 */
123 static unsigned char have_err_response = FALSE;
124
125 /* convenience macros */
126 #define DISPLAY_BYTES_IN(x) \
127 quota_display_bytes((x)->tmp_pool, \
128 sess_tally.bytes_in_used, sess_limit.bytes_in_avail, IN)
129
130 #define DISPLAY_BYTES_OUT(x) \
131 quota_display_bytes((x)->tmp_pool, \
132 sess_tally.bytes_out_used, sess_limit.bytes_out_avail, OUT)
133
134 #define DISPLAY_BYTES_XFER(x) \
135 quota_display_bytes((x)->tmp_pool, \
136 sess_tally.bytes_xfer_used, sess_limit.bytes_xfer_avail, XFER)
137
138 #define DISPLAY_FILES_IN(x) \
139 quota_display_files((x)->tmp_pool, \
140 sess_tally.files_in_used, sess_limit.files_in_avail, IN)
141
142 #define DISPLAY_FILES_OUT(x) \
143 quota_display_files((x)->tmp_pool, \
144 sess_tally.files_out_used, sess_limit.files_out_avail, OUT)
145
146 #define DISPLAY_FILES_XFER(x) \
147 quota_display_files((x)->tmp_pool, \
148 sess_tally.files_xfer_used, sess_limit.files_xfer_avail, XFER)
149
150 #define QUOTATAB_TALLY_READ \
151 if (!sess_limit.quota_per_session) { \
152 if (quotatab_read(&sess_tally) < 0) { \
153 quotatab_log("error: unable to read tally: %s", strerror(errno)); \
154 } \
155 }
156
157 #define QUOTATAB_TALLY_WRITE(bi, bo, bx, fi, fo, fx) \
158 { \
159 if (quotatab_write(&sess_tally, (bi), (bo), (bx), (fi), (fo), (fx)) < 0) { \
160 quotatab_log("error: unable to write tally: %s", strerror(errno)); \
161 } \
162 have_quota_update = 0; \
163 }
164
165 static unsigned long quotatab_opts = 0UL;
166 #define QUOTA_OPT_SCAN_ON_LOGIN 0x0001
167
168 #define QUOTA_SCAN_FL_VERBOSE 0x0001
169
170 /* necessary prototypes */
171 MODRET quotatab_pre_stor(cmd_rec *);
172 MODRET quotatab_post_stor(cmd_rec *);
173 MODRET quotatab_post_stor_err(cmd_rec *);
174 static int quotatab_sess_init(void);
175
176 static int quotatab_rlock(quota_table_t *);
177 static int quotatab_runlock(quota_table_t *);
178 static int quotatab_wlock(quota_table_t *);
179 static int quotatab_wunlock(quota_table_t *);
180
181 /* Support routines
182 */
183
184 /* Returns the most appropriate errno value for indicating a "quota exceeded"
185 * state, or -1 if there is no such appropriate errno.
186 */
187 static int get_quota_exceeded_errno(int default_errno, char **errstr) {
188 int res;
189
190 #if defined(EDQUOT)
191 res = EDQUOT;
192
193 #elif defined(EFBIG)
194 res = EFBIG;
195
196 #elif defined(ENOSPC)
197 res = ENOSPC;
198
199 #else
200 res = default_errno;
201 #endif
202
203 if (errstr != NULL) {
204 *errstr = strerror(res);
205 }
206
207 return res;
208 }
209
210 static const char *get_quota_type_text(quota_type_t quota_type) {
211 const char *text;
212
213 switch (quota_type) {
214 case USER_QUOTA:
215 text = "user";
216 break;
217
218 case GROUP_QUOTA:
219 text = "group";
220 break;
221
222 case CLASS_QUOTA:
223 text = "class";
224 break;
225
226 default:
227 text = "all";
228 }
229
230 return text;
231 }
232
233 /* Quota units routines */
234
235 static char *quota_display_bytes(pool *p, double bytes_used,
236 double bytes_avail, quota_xfer_t xfer_type) {
237 double adj_used = 0.0, adj_avail = 0.0;
238 char *display = NULL, *xferstr = NULL;
239 unsigned int display_len = 80;
240
241 display = pcalloc(p, display_len);
242
243 switch (xfer_type) {
244 case IN:
245 xferstr = _("upload");
246 break;
247
248 case OUT:
249 xferstr = _("download");
250 break;
251
252 case XFER:
253 xferstr = _("transfer");
254 break;
255
256 default:
257 break;
258 }
259
260 switch (byte_units) {
261
262 case BYTE:
263 /* no manipulation needed */
264 pr_snprintf(display, display_len - 1, _("%.2f of %.2f %s %s"), bytes_used,
265 bytes_avail, xferstr, bytes_avail > 1.0 ? _("bytes") : _("byte"));
266 break;
267
268 case KILO:
269 /* divide by 1024.0 */
270 adj_used = (bytes_used / 1024.0);
271 adj_avail = (bytes_avail / 1024.0);
272 pr_snprintf(display, display_len - 1, _("%.2f of %.2f %s Kb"), adj_used,
273 adj_avail, xferstr);
274 break;
275
276 case MEGA:
277 /* divide by 1024.0 * 1024.0 */
278 adj_used = (bytes_used / (1024.0 * 1024.0));
279 adj_avail = (bytes_avail / (1024.0 * 1024.0));
280 pr_snprintf(display, display_len -1 , _("%.2f of %.2f %s Mb"), adj_used,
281 adj_avail, xferstr);
282 break;
283
284 case GIGA:
285 /* divide by 1024.0 * 1024.0 * 1024.0 */
286 adj_used = (bytes_used / (1024.0 * 1024.0 * 1024.0));
287 adj_avail = (bytes_avail / (1024.0 * 1024.0 * 1024.0));
288 pr_snprintf(display, display_len - 1, _("%.2f of %.2f %s Gb"), adj_used,
289 adj_avail, xferstr);
290 break;
291
292 /* Always have a fallthrough case, even if it _should_ never be reached. */
293 default:
294 quotatab_log("warning: unknown QuotaDisplayUnits");
295 break;
296 }
297
298 return display;
299 }
300
301 static char *quota_display_files(pool *p, unsigned int files_used,
302 unsigned int files_avail, quota_xfer_t xfer_type) {
303 char *display = NULL, *xferstr = NULL;
304 unsigned int display_len = 80;
305
306 display = pcalloc(p, display_len);
307
308 switch (xfer_type) {
309 case IN:
310 xferstr = _("upload");
311 break;
312
313 case OUT:
314 xferstr = _("download");
315 break;
316
317 case XFER:
318 xferstr = _("transfer");
319 break;
320
321 default:
322 break;
323 }
324
325 pr_snprintf(display, display_len - 1, _("%u of %u %s %s"), files_used,
326 files_avail, xferstr, files_avail > 1.0 ? _("files") : _("file"));
327 return display;
328 }
329
330 static char *quota_display_site_bytes(pool *p, double bytes_used,
331 double bytes_avail, quota_xfer_t xfer_type) {
332 double adj_used = 0.0, adj_avail = 0.0;
333 char *display = NULL;
334 unsigned int display_len = 80;
335
336 display = pcalloc(p, display_len);
337
338 switch (byte_units) {
339 case BYTE:
340 /* no calculation needed */
341
342 if (bytes_avail > 0.0) {
343 pr_snprintf(display, display_len - 1, _("bytes:\t%.2f/%.2f"),
344 bytes_used, bytes_avail);
345
346 } else {
347 pr_snprintf(display, display_len - 1, _("bytes:\tunlimited"));
348 }
349 break;
350
351 case KILO:
352 /* divide by 1024.0 */
353 adj_used = (bytes_used / 1024.0);
354 adj_avail = (bytes_avail / 1024.0);
355
356 if (adj_avail > 0.0) {
357 pr_snprintf(display, display_len - 1, _("Kb:\t%s%.2f/%.2f"),
358 xfer_type != IN ? "" : "\t", adj_used, adj_avail);
359
360 } else {
361 pr_snprintf(display, display_len - 1, _("Kb:\tunlimited"));
362 }
363 break;
364
365 case MEGA:
366 /* divide by 1024.0 * 1024.0 */
367 adj_used = (bytes_used / (1024.0 * 1024.0));
368 adj_avail = (bytes_avail / (1024.0 * 1024.0));
369
370 if (adj_avail > 0.0) {
371 pr_snprintf(display, display_len - 1, _("Mb:\t%s%.2f/%.2f"),
372 xfer_type != IN ? "" : "\t", adj_used, adj_avail);
373
374 } else {
375 pr_snprintf(display, display_len - 1, _("Mb:\tunlimited"));
376 }
377 break;
378
379 case GIGA:
380 /* divide by 1024.0 * 1024.0 * 1024.0 */
381 adj_used = (bytes_used / (1024.0 * 1024.0 * 1024.0));
382 adj_avail = (bytes_avail / (1024.0 * 1024.0 * 1024.0));
383
384 if (adj_avail > 0.0) {
385 pr_snprintf(display, display_len - 1, _("Gb:\t%s%.2f/%.2f"),
386 xfer_type != IN ? "" : "\t", adj_used, adj_avail);
387
388 } else {
389 pr_snprintf(display, display_len - 1, _("Gb:\tunlimited"));
390 }
391 break;
392
393 default:
394 quotatab_log("warning: unknown QuotaDisplayUnits");
395 break;
396 }
397
398 return display;
399 }
400
401 static char *quota_display_site_files(pool *p, unsigned int files_used,
402 unsigned int files_avail, quota_xfer_t xfer_type) {
403 char *display = NULL;
404 unsigned int display_len = 80;
405
406 display = pcalloc(p, display_len);
407
408 if (files_avail != 0) {
409 pr_snprintf(display, display_len - 1, _("files:\t%u/%u"), files_used,
410 files_avail);
411
412 } else {
413 pr_snprintf(display, display_len - 1, _("files:\tunlimited"));
414 }
415
416 return display;
417 }
418
419 /* Quota logging routines */
420 static int quotatab_closelog(void) {
421 /* sanity check */
422 if (quota_logfd >= 0) {
423 (void) close(quota_logfd);
424 }
425
426 quota_logfd = -1;
427 quota_logname = NULL;
428
429 return 0;
430 }
431
432 int quotatab_log(const char *fmt, ...) {
433 va_list msg;
434 int res;
435
436 /* sanity check */
437 if (!quota_logname)
438 return 0;
439
440 va_start(msg, fmt);
441 res = pr_log_vwritefile(quota_logfd, MOD_QUOTATAB_VERSION, fmt, msg);
442 va_end(msg);
443
444 return res;
445 }
446
447 int quotatab_openlog(void) {
448 int res = 0, xerrno;
449
450 quota_logname = get_param_ptr(main_server->conf, "QuotaLog", FALSE);
451 if (quota_logname == NULL) {
452 return 0;
453 }
454
455 /* check for "none" */
456 if (strcasecmp(quota_logname, "none") == 0) {
457 quota_logname = NULL;
458 return 0;
459 }
460
461 pr_signals_block();
462 PRIVS_ROOT
463 res = pr_log_openfile(quota_logname, &quota_logfd, PR_LOG_SYSTEM_MODE);
464 xerrno = errno;
465 PRIVS_RELINQUISH
466 pr_signals_unblock();
467
468 switch (res) {
469 case -1:
470 pr_log_pri(LOG_NOTICE, MOD_QUOTATAB_VERSION
471 ": unable to open QuotaLog '%s': %s", quota_logname, strerror(xerrno));
472 break;
473
474 case PR_LOG_WRITABLE_DIR:
475 pr_log_pri(LOG_NOTICE, MOD_QUOTATAB_VERSION
476 ": unable to open QuotaLog '%s': %s", quota_logname,
477 "World-writable directory");
478 break;
479
480 case PR_LOG_SYMLINK:
481 pr_log_pri(LOG_NOTICE, MOD_QUOTATAB_VERSION
482 ": unable to open QuotaLog '%s': %s", quota_logname, "Symbolic link");
483 break;
484 }
485
486 return res;
487 }
488
489 static int quotatab_close(quota_tabtype_t tab_type) {
490 int res = 0;
491
492 if (tab_type == TYPE_TALLY) {
493 if (tally_tab != NULL) {
494 res = tally_tab->tab_close(tally_tab);
495 tally_tab = NULL;
496 }
497
498 } else if (tab_type == TYPE_LIMIT) {
499 if (limit_tab != NULL) {
500 res = limit_tab->tab_close(limit_tab);
501 limit_tab = NULL;
502 }
503 }
504
505 return res;
506 }
507
508 static int quotatab_verify(quota_tabtype_t tab_type) {
509
510 /* Request the table source object to verify itself */
511 if (tab_type == TYPE_TALLY) {
512 if (tally_tab->tab_verify(tally_tab)) {
513 return TRUE;
514
515 } else {
516 quotatab_log("error: unable to use QuotaTallyTable: bad table header");
517 }
518
519 } else if (tab_type == TYPE_LIMIT) {
520 if (limit_tab->tab_verify(limit_tab)) {
521 return TRUE;
522
523 } else {
524 quotatab_log("error: unable to use QuotaLimitTable: bad table header");
525 }
526 }
527
528 return FALSE;
529 }
530
531 static int quotatab_create(quota_tally_t *tally) {
532
533 /* Creation of an entry only ever occurs in the tally table -- the limit
534 * table is considered read-only.
535 */
536
537 /* Obtain a writer lock for the entry in question. */
538 if (quotatab_wlock(tally_tab) < 0)
539 return FALSE;
540
541 if (tally_tab->tab_create(tally_tab, tally) < 0) {
542 quotatab_wunlock(tally_tab);
543 return FALSE;
544 }
545
546 /* Release the lock */
547 if (quotatab_wunlock(tally_tab) < 0)
548 return FALSE;
549
550 return TRUE;
551 }
552
553 static int quotatab_create_tally(void) {
554 int res = TRUE;
555
556 memset(sess_tally.name, '\0', sizeof(sess_tally.name));
557 pr_snprintf(sess_tally.name, sizeof(sess_tally.name), "%s",
558 sess_limit.name);
559 sess_tally.name[sizeof(sess_tally.name)-1] = '\0';
560
561 sess_tally.quota_type = sess_limit.quota_type;
562
563 /* Initial tally values. */
564 sess_tally.bytes_in_used = 0.0F;
565 sess_tally.bytes_out_used = 0.0F;
566 sess_tally.bytes_xfer_used = 0.0F;
567 sess_tally.files_in_used = 0U;
568 sess_tally.files_out_used = 0U;
569 sess_tally.files_xfer_used = 0U;
570
571 quotatab_log("creating new tally entry to match limit entry");
572
573 res = quotatab_create(&sess_tally);
574 if (res == FALSE) {
575 quotatab_log("error: unable to create tally entry: %s",
576 strerror(errno));
577 }
578
579 return res;
580 }
581
582 static quota_regtab_t *quotatab_get_backend(const char *backend,
583 unsigned int srcs) {
584 quota_regtab_t *regtab;
585
586 for (regtab = quotatab_backends; regtab; regtab = regtab->next) {
587 if ((regtab->regtab_srcs & srcs) &&
588 strcmp(regtab->regtab_name, backend) == 0) {
589 return regtab;
590 }
591 }
592
593 errno = ENOENT;
594 return NULL;
595 }
596
597 /* Returns TRUE if the given path is to be ignored, FALSE otherwise. */
598 static int quotatab_ignore_path(pool *p, const char *path) {
599 char *abs_path;
600
601 if (path == NULL) {
602 return FALSE;
603 }
604
605 #if defined(PR_USE_REGEX)
606 if (quota_exclude_pre == NULL) {
607 return FALSE;
608 }
609
610 /* Always apply the filter to the absolute path. */
611 abs_path = dir_abs_path(p, path, TRUE);
612 if (abs_path == NULL) {
613 quotatab_log("unable to resolve absolute path for '%s': %s", path,
614 strerror(errno));
615 abs_path = (char *) path;
616 }
617
618 if (pr_regexp_exec(quota_exclude_pre, abs_path, 0, NULL, 0, 0, 0) == 0) {
619 return TRUE;
620 }
621
622 return FALSE;
623 #else
624 return FALSE;
625 #endif
626 }
627
628 static int quotatab_scan_dir(pool *p, const char *path, uid_t uid,
629 gid_t gid, int flags, double *nbytes, unsigned int *nfiles) {
630 struct stat st;
631 DIR *dirh;
632 struct dirent *dent;
633
634 if (!nbytes ||
635 !nfiles) {
636 errno = EINVAL;
637 return -1;
638 }
639
640 if (quotatab_ignore_path(p, path)) {
641 quotatab_log("path '%s' matches QuotaExcludeFilter '%s', ignoring",
642 path, quota_exclude_filter);
643 return 0;
644 }
645
646 if (pr_fsio_lstat(path, &st) < 0) {
647 return -1;
648 }
649
650 if (!S_ISDIR(st.st_mode)) {
651 errno = EINVAL;
652 return -1;
653 }
654
655 dirh = pr_fsio_opendir(path);
656 if (dirh == NULL) {
657 return -1;
658 }
659
660 if (use_dirs) {
661 if (uid != (uid_t) -1 ||
662 gid != (gid_t) -1) {
663
664 if (uid != (uid_t) -1 &&
665 st.st_uid == uid) {
666 *nbytes += st.st_size;
667 *nfiles += 1;
668
669 } else if (gid != (gid_t) -1 &&
670 st.st_gid == gid) {
671 *nbytes += st.st_size;
672 *nfiles += 1;
673 }
674
675 } else {
676 *nbytes += st.st_size;
677 *nfiles += 1;
678 }
679 }
680
681 while ((dent = pr_fsio_readdir(dirh)) != NULL) {
682 char *file;
683
684 pr_signals_handle();
685
686 if (strcmp(dent->d_name, ".") == 0 ||
687 strcmp(dent->d_name, "..") == 0) {
688 continue;
689 }
690
691 file = pdircat(p, path, dent->d_name, NULL);
692
693 memset(&st, 0, sizeof(st));
694 if (pr_fsio_lstat(file, &st) < 0) {
695 quotatab_log("unable to lstat '%s': %s", file, strerror(errno));
696 continue;
697 }
698
699 if (S_ISREG(st.st_mode) ||
700 S_ISLNK(st.st_mode)) {
701
702 if (uid != (uid_t) -1 ||
703 gid != (gid_t) -1) {
704 if (uid != (uid_t) -1 &&
705 st.st_uid == uid) {
706 *nbytes += st.st_size;
707 *nfiles += 1;
708
709 } else if (gid != (gid_t) -1 &&
710 st.st_gid == gid) {
711 *nbytes += st.st_size;
712 *nfiles += 1;
713 }
714
715 } else {
716 *nbytes += st.st_size;
717 *nfiles += 1;
718 }
719
720 } else if (S_ISDIR(st.st_mode)) {
721 pool *sub_pool;
722
723 sub_pool = make_sub_pool(p);
724
725 if (quotatab_scan_dir(sub_pool, file, uid, gid, flags, nbytes,
726 nfiles) < 0) {
727 quotatab_log("error scanning '%s': %s", file, strerror(errno));
728 }
729
730 destroy_pool(sub_pool);
731
732 } else {
733 if (flags & QUOTA_SCAN_FL_VERBOSE) {
734 quotatab_log("file '%s' is not a file, symlink, or directory; skipping",
735 file);
736 }
737 }
738 }
739
740 pr_fsio_closedir(dirh);
741 return 0;
742 }
743
744 static int quotatab_open(quota_tabtype_t tab_type) {
745
746 if (tab_type == TYPE_TALLY) {
747 register config_rec *c = NULL;
748 register quota_regtab_t *regtab = NULL;
749
750 c = find_config(main_server->conf, CONF_PARAM, "QuotaTallyTable", FALSE);
751 if (!c) {
752 quotatab_log("notice: no QuotaTallyTable configured");
753 return -1;
754 }
755
756 regtab = quotatab_get_backend(c->argv[0], QUOTATAB_TALLY_SRC);
757 if (regtab) {
758 tally_tab = regtab->regtab_open(quotatab_pool, TYPE_TALLY, c->argv[1]);
759 if (!tally_tab)
760 return -1;
761
762 } else {
763 quotatab_log("error: unsupported tally table type: '%s'",
764 (const char *) c->argv[0]);
765 return -1;
766 }
767
768 } else if (tab_type == TYPE_LIMIT) {
769 register config_rec *c = NULL;
770 register quota_regtab_t *regtab = NULL;
771
772 c = find_config(main_server->conf, CONF_PARAM, "QuotaLimitTable", FALSE);
773 if (!c) {
774 quotatab_log("notice: no QuotaLimitTable configured");
775 return -1;
776 }
777
778 /* Look up the table source open routine by name, and invoke it */
779 regtab = quotatab_get_backend(c->argv[0], QUOTATAB_LIMIT_SRC);
780 if (regtab) {
781 limit_tab = regtab->regtab_open(quotatab_pool, TYPE_LIMIT, c->argv[1]);
782 if (!limit_tab)
783 return -1;
784
785 } else {
786 quotatab_log("error: unsupported limit table type: '%s'",
787 (const char *) c->argv[0]);
788 return -1;
789 }
790 }
791
792 return 0;
793 }
794
795 /* Reads via this function are only ever done on tally tables. Limit tables
796 * are read via the quotatab_lookup() function.
797 */
798 int quotatab_read(quota_tally_t *tally) {
799 int bread = 0;
800
801 /* Make sure the tally table can support reads. */
802 if (!tally_tab || !tally_tab->tab_read) {
803 errno = EPERM;
804 return -1;
805 }
806
807 /* Obtain a reader lock for the entry in question. */
808 if (quotatab_rlock(tally_tab) < 0) {
809 quotatab_log("error: unable to obtain read lock: %s", strerror(errno));
810 return -1;
811 }
812
813 /* Read from the current point in the stream enough information to populate
814 * the quota structs.
815 */
816 bread = tally_tab->tab_read(tally_tab, tally);
817 if (bread < 0) {
818 quotatab_runlock(tally_tab);
819 return -1;
820 }
821
822 /* Release the lock */
823 if (quotatab_runlock(tally_tab) < 0) {
824 quotatab_log("error: unable to release read lock: %s", strerror(errno));
825 return -1;
826 }
827
828 return bread;
829 }
830
831 /* This function is used by mod_quotatab backends, to register their
832 * individual backend table function pointers with the main mod_quotatab
833 * module.
834 */
835 int quotatab_register_backend(const char *backend,
836 quota_table_t *(*srcopen)(pool *, quota_tabtype_t, const char *),
837 unsigned int srcs) {
838 quota_regtab_t *regtab;
839
840 if (!backend || !srcopen) {
841 errno = EINVAL;
842 return -1;
843 }
844
845 if (!quotatab_backend_pool) {
846 quotatab_backend_pool = make_sub_pool(permanent_pool);
847 pr_pool_tag(quotatab_backend_pool, MOD_QUOTATAB_VERSION ": Backend Pool");
848 }
849
850 /* Check to see if this backend has already been registered. */
851 regtab = quotatab_get_backend(backend, srcs);
852 if (regtab) {
853 errno = EEXIST;
854 return -1;
855 }
856
857 regtab = pcalloc(quotatab_backend_pool, sizeof(quota_regtab_t));
858 regtab->regtab_name = pstrdup(quotatab_backend_pool, backend);
859 regtab->regtab_open = srcopen;
860 regtab->regtab_srcs = srcs;
861
862 /* Add this object to the list. */
863 if (quotatab_backends) {
864 quotatab_backends->prev = regtab;
865 regtab->next = quotatab_backends;
866 }
867
868 quotatab_backends = regtab;
869 quotatab_nbackends++;
870
871 return 0;
872 }
873
874 /* Used by mod_quotatab backends to unregister their backend function pointers
875 * from the main mod_quotatab module.
876 */
877 int quotatab_unregister_backend(const char *backend, unsigned int srcs) {
878 quota_regtab_t *regtab;
879
880 if (!backend) {
881 errno = EINVAL;
882 return -1;
883 }
884
885 /* Check to see if this backend has been registered. */
886 regtab = quotatab_get_backend(backend, srcs);
887 if (!regtab) {
888 errno = ENOENT;
889 return -1;
890 }
891
892 #if !defined(PR_SHARED_MODULE)
893 /* If there is only one registered backend, it cannot be removed.
894 */
895 if (quotatab_nbackends == 1) {
896 errno = EPERM;
897 return -1;
898 }
899 #endif
900
901 /* Remove this backend from the linked list. */
902 if (regtab->prev)
903 regtab->prev->next = regtab->next;
904 else
905 /* This backend is the start of the quotatab_backends list (prev is NULL),
906 * so we need to update the list head pointer as well.
907 */
908 quotatab_backends = regtab->next;
909
910 if (regtab->next)
911 regtab->next->prev = regtab->prev;
912
913 regtab->prev = regtab->next = NULL;
914
915 quotatab_nbackends--;
916
917 /* NOTE: a counter should be kept of the number of unregistrations,
918 * as the memory for a registration is not freed on unregistration.
919 */
920
921 return 0;
922 }
923
924 unsigned char quotatab_lookup_default(quota_tabtype_t tab_type, void *ptr,
925 const char *name, quota_type_t quota_type) {
926 config_rec *c;
927
928 c = find_config(main_server->conf, CONF_PARAM, "QuotaDefault", FALSE);
929 while (c != NULL) {
930 char *type_str;
931 quota_limit_t *limit;
932
933 pr_signals_handle();
934
935 type_str = c->argv[0];
936
937 /* What quota type is being looked up, and what kind does this QuotaDefault
938 * provide?
939 */
940 switch (quota_type) {
941 case USER_QUOTA:
942 if (strncasecmp(type_str, "user", 5) != 0) {
943 c = find_config_next(c, c->next, CONF_PARAM, "QuotaDefault", FALSE);
944 continue;
945 }
946 break;
947
948 case GROUP_QUOTA:
949 if (strncasecmp(type_str, "group", 6) != 0) {
950 c = find_config_next(c, c->next, CONF_PARAM, "QuotaDefault", FALSE);
951 continue;
952 }
953 break;
954
955 case CLASS_QUOTA:
956 if (strncasecmp(type_str, "class", 6) != 0) {
957 c = find_config_next(c, c->next, CONF_PARAM, "QuotaDefault", FALSE);
958 continue;
959 }
960 break;
961
962 case ALL_QUOTA:
963 if (strncasecmp(type_str, "all", 4) != 0) {
964 c = find_config_next(c, c->next, CONF_PARAM, "QuotaDefault", FALSE);
965 continue;
966 }
967 break;
968
969 default:
970 c = find_config_next(c, c->next, CONF_PARAM, "QuotaDefault", FALSE);
971 continue;
972 }
973
974 limit = ptr;
975
976 /* Retrieve the limit record (8 values):
977 *
978 * per_session
979 * limit_type
980 * bytes_{in,out,xfer}_avail
981 * files_{in,out,xfer}_avail
982 */
983
984 sstrncpy(limit->name, name, sizeof(limit->name)-1);
985 limit->quota_type = quota_type;
986
987 limit->quota_per_session = pr_str_is_boolean(c->argv[1]);
988
989 if (strncasecmp(c->argv[2], "soft", 5) == 0) {
990 limit->quota_limit_type = SOFT_LIMIT;
991
992 } else if (strncasecmp(c->argv[2], "hard", 5) == 0) {
993 limit->quota_limit_type = HARD_LIMIT;
994 }
995
996 limit->bytes_in_avail = atof(c->argv[3]);
997 limit->bytes_out_avail = atof(c->argv[4]);
998 limit->bytes_xfer_avail = atof(c->argv[5]);
999 limit->files_in_avail = atoi(c->argv[6]);
1000 limit->files_out_avail = atoi(c->argv[7]);
1001 limit->files_xfer_avail = atoi(c->argv[8]);
1002
1003 quotatab_log("using default %s limit from QuotaDefault directive",
1004 type_str);
1005 return TRUE;
1006 }
1007
1008 return FALSE;
1009 }
1010
1011 /* Note: this function will only find the first occurrence of the given
1012 * name and type in the table. This means that if there is a malformed
1013 * quota table, with duplicate name/type pairs, the duplicates will be
1014 * ignored.
1015 */
1016 unsigned char quotatab_lookup(quota_tabtype_t tab_type, void *ptr,
1017 const char *name, quota_type_t quota_type) {
1018
1019 if (tab_type == TYPE_TALLY) {
1020
1021 /* Make sure the requested table can do lookups. */
1022 if (tally_tab == NULL ||
1023 tally_tab->tab_lookup == NULL) {
1024 errno = EPERM;
1025 return FALSE;
1026 }
1027
1028 return tally_tab->tab_lookup(tally_tab, ptr, name, quota_type);
1029
1030 } else if (tab_type == TYPE_LIMIT) {
1031 int res;
1032
1033 /* Make sure the requested table can do lookups. */
1034 if (limit_tab == NULL ||
1035 limit_tab->tab_lookup == NULL) {
1036 errno = EPERM;
1037 res = FALSE;
1038
1039 } else {
1040 res = limit_tab->tab_lookup(limit_tab, ptr, name, quota_type);
1041 }
1042
1043 return res;
1044 }
1045
1046 errno = ENOENT;
1047 return FALSE;
1048 }
1049
1050 static int quotatab_mutex_lock(int lock_type) {
1051 const char *lock_desc;
1052 struct flock lock;
1053 unsigned int nattempts = 1;
1054
1055 if (quota_lockfd < 0)
1056 return 0;
1057
1058 lock.l_type = lock_type;
1059 lock.l_whence = SEEK_SET;
1060 lock.l_start = 0;
1061 lock.l_len = 0;
1062
1063 lock_desc = (lock.l_type == F_WRLCK ? "write-lock" : "unlock");
1064
1065 pr_trace_msg("lock", 9, "attempting to %s QuotaLock fd %d", lock_desc,
1066 quota_lockfd);
1067
1068 while (fcntl(quota_lockfd, F_SETLK, &lock) < 0) {
1069 int xerrno = errno;
1070
1071 if (xerrno == EINTR) {
1072 pr_signals_handle();
1073 continue;
1074 }
1075
1076 pr_trace_msg("lock", 3, "%s of QuotaLock fd %d failed: %s",
1077 lock_desc, quota_lockfd, strerror(xerrno));
1078
1079 if (xerrno == EACCES) {
1080 struct flock locker;
1081
1082 /* Get the PID of the process blocking this lock. */
1083 if (fcntl(quota_lockfd, F_GETLK, &locker) == 0) {
1084 pr_trace_msg("lock", 3, "process ID %lu has blocking %s on "
1085 "QuotaLock fd %d", (unsigned long) locker.l_pid,
1086 (locker.l_type == F_WRLCK ? "write-lock" :
1087 locker.l_type == F_RDLCK ? "read-lock" : "unlock"),
1088 quota_lockfd);
1089 }
1090 }
1091
1092 if (xerrno == EAGAIN ||
1093 xerrno == EACCES) {
1094 /* Treat this as an interrupted call, call pr_signals_handle() (which
1095 * will delay for a few msecs because of EINTR), and try again.
1096 * After QUOTA_MAX_LOCK_ATTEMPTS attempts, give up altogether.
1097 */
1098
1099 nattempts++;
1100 if (nattempts <= QUOTA_MAX_LOCK_ATTEMPTS) {
1101 errno = EINTR;
1102
1103 pr_signals_handle();
1104
1105 errno = 0;
1106 continue;
1107 }
1108
1109 quotatab_log("unable to acquire %s lock on QuotaLock for user '%s': %s",
1110 lock_desc, session.user, strerror(xerrno));
1111 errno = xerrno;
1112 return -1;
1113 }
1114 }
1115
1116 pr_trace_msg("lock", 9, "%s of QuotaLock fd %d succeeded", lock_desc,
1117 quota_lockfd);
1118 return 0;
1119 }
1120
1121 static int quotatab_rlock(quota_table_t *tab) {
1122
1123 if (tab->rlock_count == 0 &&
1124 tab->wlock_count == 0) {
1125 unsigned int nattempts = 1;
1126
1127 tab->tab_lockfd = quota_lockfd;
1128
1129 pr_trace_msg("lock", 9, "attempting to read-lock QuotaLock fd %d",
1130 quota_lockfd);
1131
1132 while (tab->tab_rlock(tab) < 0) {
1133 int xerrno = errno;
1134
1135 if (xerrno == EINTR) {
1136 pr_signals_handle();
1137 continue;
1138 }
1139
1140 if (xerrno == EACCES) {
1141 struct flock locker;
1142
1143 /* Get the PID of the process blocking this lock. */
1144 if (fcntl(quota_lockfd, F_GETLK, &locker) == 0) {
1145 pr_trace_msg("lock", 3, "process ID %lu has blocking %s on "
1146 "QuotaLock fd %d", (unsigned long) locker.l_pid,
1147 (locker.l_type == F_WRLCK ? "write-lock" :
1148 locker.l_type == F_RDLCK ? "read-lock" : "unlock"),
1149 quota_lockfd);
1150 }
1151 }
1152
1153 if (xerrno == EAGAIN ||
1154 xerrno == EACCES) {
1155 /* Treat this as an interrupted call, call pr_signals_handle() (which
1156 * will delay for a few msecs because of EINTR), and try again.
1157 * After QUOTA_MAX_LOCK_ATTEMPTS attempts, give up altogether.
1158 */
1159
1160 nattempts++;
1161 if (nattempts <= QUOTA_MAX_LOCK_ATTEMPTS) {
1162 errno = EINTR;
1163
1164 pr_signals_handle();
1165
1166 errno = 0;
1167 continue;
1168 }
1169 }
1170
1171 quotatab_log("unable to acquire read lock on QuotaLock for user '%s': %s",
1172 session.user, strerror(xerrno));
1173 errno = xerrno;
1174 return -1;
1175 }
1176 }
1177
1178 tab->rlock_count++;
1179 return 0;
1180 }
1181
1182 static int quotatab_runlock(quota_table_t *tab) {
1183 int res = 0;
1184
1185 if (tab->rlock_count == 1 &&
1186 tab->wlock_count == 0) {
1187 tab->tab_lockfd = quota_lockfd;
1188
1189 while (tab->tab_unlock(tab) < 0) {
1190 int xerrno = errno;
1191
1192 if (xerrno == EINTR) {
1193 pr_signals_handle();
1194 continue;
1195 }
1196
1197 res = -1;
1198 break;
1199 }
1200 }
1201
1202 if (res == 0 &&
1203 tab->rlock_count > 0) {
1204 tab->rlock_count--;
1205 }
1206
1207 return res;
1208 }
1209
1210 static int quotatab_wlock(quota_table_t *tab) {
1211
1212 if (tab->wlock_count == 0) {
1213 unsigned int nattempts = 1;
1214
1215 tab->tab_lockfd = quota_lockfd;
1216
1217 pr_trace_msg("lock", 9, "attempting to write-lock QuotaLock fd %d",
1218 quota_lockfd);
1219
1220 while (tab->tab_wlock(tab) < 0) {
1221 int xerrno = errno;
1222
1223 if (xerrno == EINTR) {
1224 pr_signals_handle();
1225 continue;
1226 }
1227
1228 if (xerrno == EACCES) {
1229 struct flock locker;
1230
1231 /* Get the PID of the process blocking this lock. */
1232 if (fcntl(quota_lockfd, F_GETLK, &locker) == 0) {
1233 pr_trace_msg("lock", 3, "process ID %lu has blocking %s on "
1234 "QuotaLock fd %d", (unsigned long) locker.l_pid,
1235 (locker.l_type == F_WRLCK ? "write-lock" :
1236 locker.l_type == F_RDLCK ? "read-lock" : "unlock"),
1237 quota_lockfd);
1238 }
1239 }
1240
1241 if (xerrno == EAGAIN ||
1242 xerrno == EACCES) {
1243 /* Treat this as an interrupted call, call pr_signals_handle() (which
1244 * will delay for a few msecs because of EINTR), and try again.
1245 * After QUOTA_MAX_LOCK_ATTEMPTS attempts, give up altogether.
1246 */
1247
1248 nattempts++;
1249 if (nattempts <= QUOTA_MAX_LOCK_ATTEMPTS) {
1250 errno = EINTR;
1251
1252 pr_signals_handle();
1253
1254 errno = 0;
1255 continue;
1256 }
1257 }
1258
1259 quotatab_log("unable to acquire write lock on QuotaLock for "
1260 "user '%s': %s", session.user, strerror(xerrno));
1261 errno = xerrno;
1262 return -1;
1263 }
1264 }
1265
1266 tab->wlock_count++;
1267 return 0;
1268 }
1269
1270 static int quotatab_wunlock(quota_table_t *tab) {
1271 int res = 0;
1272
1273 /* A write-lock may need to be "downgraded" into a read-lock, depending
1274 * on the other lock attempts that have been made.
1275 */
1276 if (tab->wlock_count == 1) {
1277 tab->tab_lockfd = quota_lockfd;
1278
1279 if (tab->rlock_count == 0) {
1280 /* The write-lock is the only lock left on the table; do a full unlock. */
1281 while (tab->tab_unlock(tab) < 0) {
1282 int xerrno = errno;
1283
1284 if (xerrno == EINTR) {
1285 pr_signals_handle();
1286 continue;
1287 }
1288
1289 res = -1;
1290 break;
1291 }
1292
1293 } else {
1294 /* We have some read-locks on the table; downgrade the write-lock to
1295 * a read-lock. The call to the tab_rlock() callback does NOT go
1296 * through the quotatab_rlock() function, which means it does not affect
1297 * the table lock counters; this is by design.
1298 */
1299 res = tab->tab_rlock(tab);
1300 }
1301 }
1302
1303 if (res == 0 &&
1304 tab->wlock_count > 0) {
1305 tab->wlock_count--;
1306 }
1307
1308 return res;
1309 }
1310
1311 int quotatab_write(quota_tally_t *tally,
1312 double bytes_in_inc, double bytes_out_inc, double bytes_xfer_inc,
1313 int files_in_inc, int files_out_inc, int files_xfer_inc) {
1314
1315 /* Make sure the tally table can support writes. */
1316 if (!tally_tab || !tally_tab->tab_write) {
1317 errno = EPERM;
1318 return -1;
1319 }
1320
1321 /* Obtain a writer lock for the entry in question */
1322 if (quotatab_wlock(tally_tab) < 0) {
1323 quotatab_log("error: unable to obtain write lock: %s", strerror(errno));
1324 return -1;
1325 }
1326
1327 /* Make sure the deltas are cleared. */
1328 memset(&quotatab_deltas, '\0', sizeof(quotatab_deltas));
1329
1330 /* Read in the tally (to catch any possible updates by other processes). */
1331 QUOTATAB_TALLY_READ
1332
1333 /* Only update the tally if the value is not "unlimited". */
1334 if (sess_limit.bytes_in_avail > 0.0) {
1335 sess_tally.bytes_in_used += bytes_in_inc;
1336
1337 /* Prevent underflows. */
1338 if (sess_tally.bytes_in_used < 0.0)
1339 sess_tally.bytes_in_used = 0.0;
1340
1341 quotatab_deltas.bytes_in_delta = bytes_in_inc;
1342 }
1343
1344 /* Only update the tally if the value is not "unlimited". */
1345 if (sess_limit.bytes_out_avail > 0.0) {
1346 sess_tally.bytes_out_used += bytes_out_inc;
1347
1348 /* Prevent underflows. */
1349 if (sess_tally.bytes_out_used < 0.0)
1350 sess_tally.bytes_out_used = 0.0;
1351
1352 quotatab_deltas.bytes_out_delta = bytes_out_inc;
1353 }
1354
1355 /* Only update the tally if the value is not "unlimited". */
1356 if (sess_limit.bytes_xfer_avail > 0.0) {
1357 sess_tally.bytes_xfer_used += bytes_xfer_inc;
1358
1359 /* Prevent underflows. */
1360 if (sess_tally.bytes_xfer_used < 0.0)
1361 sess_tally.bytes_xfer_used = 0.0;
1362
1363 quotatab_deltas.bytes_xfer_delta = bytes_xfer_inc;
1364 }
1365
1366 /* Only update the tally if the value is not "unlimited". */
1367 if (sess_limit.files_in_avail != 0) {
1368
1369 /* Prevent underflows. As this is an unsigned data type, the
1370 * underflow check is not as straightforward as checking for a value
1371 * less than zero.
1372 */
1373 if (!(sess_tally.files_in_used == 0 && files_in_inc < 0))
1374 sess_tally.files_in_used += files_in_inc;
1375
1376 quotatab_deltas.files_in_delta = files_in_inc;
1377 }
1378
1379 /* Only update the tally if the value is not "unlimited". */
1380 if (sess_limit.files_out_avail != 0) {
1381
1382 /* Prevent underflows. As this is an unsigned data type, the
1383 * underflow check is not as straightforward as checking for a value
1384 * less than zero.
1385 */
1386 if (!(sess_tally.files_out_used == 0 && files_out_inc < 0))
1387 sess_tally.files_out_used += files_out_inc;
1388
1389 quotatab_deltas.files_out_delta = files_out_inc;
1390 }
1391
1392 /* Only update the tally if the value is not "unlimited". */
1393 if (sess_limit.files_xfer_avail != 0) {
1394
1395 /* Prevent underflows. As this is an unsigned data type, the
1396 * underflow check is not as straightforward as checking for a value
1397 * less than zero.
1398 */
1399 if (!(sess_tally.files_xfer_used == 0 && files_xfer_inc < 0))
1400 sess_tally.files_xfer_used += files_xfer_inc;
1401
1402 quotatab_deltas.files_xfer_delta = files_xfer_inc;
1403 }
1404
1405 /* No need to write out to the stream if per-session quotas are in effect. */
1406 if (sess_limit.quota_per_session) {
1407 memset(&quotatab_deltas, '\0', sizeof(quotatab_deltas));
1408 quotatab_wunlock(tally_tab);
1409 return 0;
1410 }
1411
1412 if (tally_tab->tab_write(tally_tab, tally) < 0) {
1413 quotatab_log("error: unable to update tally entry: %s", strerror(errno));
1414 quotatab_wunlock(tally_tab);
1415 memset(&quotatab_deltas, '\0', sizeof(quotatab_deltas));
1416 return -1;
1417 }
1418
1419 /* Release the lock */
1420 if (quotatab_wunlock(tally_tab) < 0) {
1421 quotatab_log("error: unable to release write lock: %s", strerror(errno));
1422 memset(&quotatab_deltas, '\0', sizeof(quotatab_deltas));
1423 return -1;
1424 }
1425
1426 memset(&quotatab_deltas, '\0', sizeof(quotatab_deltas));
1427 return 0;
1428 }
1429
1430 /* FSIO handlers
1431 */
1432
1433 static off_t copied_bytes = 0;
1434
1435 static ssize_t quotatab_fsio_pwrite(pr_fh_t *fh, int fd, const void *buf,
1436 size_t bufsz, off_t offset) {
1437 ssize_t res;
1438 off_t total_bytes;
1439
1440 res = pwrite(fd, buf, bufsz, offset);
1441 if (res < 0) {
1442 return res;
1443 }
1444
1445 if (have_quota_update == 0) {
1446 return res;
1447 }
1448
1449 /* Check to see if we've exceeded our upload limit. mod_xfer will
1450 * have called pr_data_xfer(), which will have updated
1451 * session.xfer.total_bytes, before calling pr_fsio_write(), so
1452 * we do not have to worry about updated/changing session.xfer.total_bytes
1453 * ourselves.
1454 *
1455 * Note that there is a race condition here: it is possible for the same
1456 * user to be writing to the same file in chunks from multiple
1457 * simultaneous connections.
1458 */
1459
1460 /* If the client is copying a file (versus uploading a file), then we need
1461 * to track the "total bytes" differently.
1462 */
1463 if (session.curr_cmd_id == PR_CMD_SITE_ID &&
1464 (session.curr_cmd_rec->argc >= 2 &&
1465 (strcasecmp(session.curr_cmd_rec->argv[1], "CPTO") == 0 ||
1466 strcasecmp(session.curr_cmd_rec->argv[1], "COPY") == 0))) {
1467 copied_bytes += res;
1468 total_bytes = copied_bytes;
1469
1470 } else {
1471 total_bytes = session.xfer.total_bytes;
1472 }
1473
1474 if (sess_limit.bytes_in_avail > 0.0 &&
1475 sess_tally.bytes_in_used + total_bytes > sess_limit.bytes_in_avail) {
1476 int xerrno;
1477 char *errstr = NULL;
1478
1479 xerrno = get_quota_exceeded_errno(EIO, &errstr);
1480 quotatab_log("quotatab write(): limit exceeded, returning %s", errstr);
1481
1482 errno = xerrno;
1483 return -1;
1484 }
1485
1486 if (sess_limit.bytes_xfer_avail > 0.0 &&
1487 sess_tally.bytes_xfer_used + total_bytes > sess_limit.bytes_xfer_avail) {
1488 int xerrno;
1489 char *errstr = NULL;
1490
1491 xerrno = get_quota_exceeded_errno(EIO, &errstr);
1492 quotatab_log("quotatab write(): transfer limit exceeded, returning %s",
1493 errstr);
1494
1495 errno = xerrno;
1496 return -1;
1497 }
1498
1499 return res;
1500 }
1501
1502 static int quotatab_fsio_write(pr_fh_t *fh, int fd, const char *buf,
1503 size_t bufsz) {
1504 int res;
1505 off_t total_bytes;
1506
1507 res = write(fd, buf, bufsz);
1508 if (res < 0) {
1509 return res;
1510 }
1511
1512 if (have_quota_update == 0) {
1513 return res;
1514 }
1515
1516 /* Check to see if we've exceeded our upload limit. mod_xfer will
1517 * have called pr_data_xfer(), which will have updated
1518 * session.xfer.total_bytes, before calling pr_fsio_write(), so
1519 * we do not have to worry about updated/changing session.xfer.total_bytes
1520 * ourselves.
1521 *
1522 * Note that there is a race condition here: it is possible for the same
1523 * user to be writing to the same file in chunks from multiple
1524 * simultaneous connections.
1525 */
1526
1527 /* If the client is copying a file (versus uploading a file), then we need
1528 * to track the "total bytes" differently.
1529 */
1530 if (session.curr_cmd_id == PR_CMD_SITE_ID &&
1531 (session.curr_cmd_rec->argc >= 2 &&
1532 (strncasecmp(session.curr_cmd_rec->argv[1], "CPTO", 5) == 0 ||
1533 strncasecmp(session.curr_cmd_rec->argv[1], "COPY", 5) == 0))) {
1534 copied_bytes += res;
1535 total_bytes = copied_bytes;
1536
1537 } else {
1538 total_bytes = session.xfer.total_bytes;
1539 }
1540
1541 if (sess_limit.bytes_in_avail > 0.0 &&
1542 sess_tally.bytes_in_used + total_bytes > sess_limit.bytes_in_avail) {
1543 int xerrno;
1544 char *errstr = NULL;
1545
1546 xerrno = get_quota_exceeded_errno(EIO, &errstr);
1547 quotatab_log("quotatab write(): limit exceeded, returning %s", errstr);
1548
1549 errno = xerrno;
1550 return -1;
1551 }
1552
1553 if (sess_limit.bytes_xfer_avail > 0.0 &&
1554 sess_tally.bytes_xfer_used + total_bytes > sess_limit.bytes_xfer_avail) {
1555 int xerrno;
1556 char *errstr = NULL;
1557
1558 xerrno = get_quota_exceeded_errno(EIO, &errstr);
1559 quotatab_log("quotatab write(): transfer limit exceeded, returning %s",
1560 errstr);
1561
1562 errno = xerrno;
1563 return -1;
1564 }
1565
1566 return res;
1567 }
1568
1569 /* Configuration handlers
1570 */
1571
1572 /* usage: QuotaDefault quota-type per-session limit-type \
1573 * bytes-avail-in bytes-avail-out bytes-avail-xfer \
1574 * files-avail-in files-avail-out files-avail-xfer
1575 */
1576 MODRET set_quotadefault(cmd_rec *cmd) {
1577 config_rec *c = NULL;
1578
1579 CHECK_ARGS(cmd, 9);
1580 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1581
1582 c = add_config_param(cmd->argv[0], 9, NULL, NULL, NULL, NULL, NULL, NULL,
1583 NULL, NULL, NULL);
1584
1585 /* The quota-type parameter MUST be one of "user", "group", "class", or
1586 * "all".
1587 */
1588 if (strncasecmp(cmd->argv[1], "user", 5) != 0 &&
1589 strncasecmp(cmd->argv[1], "group", 6) != 0 &&
1590 strncasecmp(cmd->argv[1], "class", 6) != 0 &&
1591 strncasecmp(cmd->argv[1], "all", 4) != 0) {
1592 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown quota type '",
1593 (char *) cmd->argv[1], "' configured", NULL));
1594 }
1595
1596 c->argv[0] = pstrdup(c->pool, cmd->argv[1]);
1597
1598 /* per-session */
1599 if (pr_str_is_boolean(cmd->argv[2]) < 0) {
1600 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1601 "expected Boolean per-session parameter: ", (char *) cmd->argv[2], NULL));
1602 }
1603
1604 c->argv[1] = pstrdup(c->pool, cmd->argv[2]);
1605
1606 /* limit-type */
1607 if (strncasecmp(cmd->argv[3], "soft", 5) != 0 &&
1608 strncasecmp(cmd->argv[3], "hard", 5) != 0) {
1609 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1610 "expected 'soft' or 'hard' limit-type parameter: ",
1611 (char *) cmd->argv[3], NULL));
1612 }
1613
1614 c->argv[2] = pstrdup(c->pool, cmd->argv[3]);
1615
1616 /* bytes-avail-in, bytes-avail-out, bytes-avail-xfer */
1617 c->argv[3] = pstrdup(c->pool, cmd->argv[4]);
1618 c->argv[4] = pstrdup(c->pool, cmd->argv[5]);
1619 c->argv[5] = pstrdup(c->pool, cmd->argv[6]);
1620
1621 /* files-avail-in, files-avail-out, files-avail-xfer */
1622 c->argv[6] = pstrdup(c->pool, cmd->argv[7]);
1623 c->argv[7] = pstrdup(c->pool, cmd->argv[8]);
1624 c->argv[8] = pstrdup(c->pool, cmd->argv[9]);
1625
1626 if (pr_module_exists("mod_ifsession.c")) {
1627 /* These are needed in case this directive is used with mod_ifsession
1628 * configuration.
1629 */
1630 c->flags |= CF_MULTI;
1631 }
1632
1633 return PR_HANDLED(cmd);
1634 }
1635
1636 /* usage: QuotaDirectoryTally <on|off> */
1637 MODRET set_quotadirtally(cmd_rec *cmd) {
1638 int dir_tally = -1;
1639 config_rec *c = NULL;
1640
1641 CHECK_ARGS(cmd, 1);
1642 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1643
1644 dir_tally = get_boolean(cmd, 1);
1645 if (dir_tally == -1) {
1646 CONF_ERROR(cmd, "expected Boolean parameter");
1647 }
1648
1649 c = add_config_param(cmd->argv[0], 1, NULL);
1650 c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
1651 *((unsigned char *) c->argv[0]) = (unsigned char) dir_tally;
1652
1653 return PR_HANDLED(cmd);
1654 }
1655
1656 /* usage: QuotaDisplayUnits <b|Kb|Mb|Gb> */
1657 MODRET set_quotadisplayunits(cmd_rec *cmd) {
1658 quota_units_t units = BYTE;
1659 config_rec *c = NULL;
1660
1661 CHECK_ARGS(cmd, 1);
1662 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1663
1664 if (strcasecmp(cmd->argv[1], "b") == 0) {
1665 /* No need to assign to the units variable, as it defaults to
1666 * displaying bytes
1667 */
1668 ;
1669
1670 } else if (strcasecmp(cmd->argv[1], "Kb") == 0) {
1671 units = KILO;
1672
1673 } else if (strcasecmp(cmd->argv[1], "Mb") == 0) {
1674 units = MEGA;
1675
1676 } else if (strcasecmp(cmd->argv[1], "Gb") == 0) {
1677 units = GIGA;
1678
1679 } else {
1680 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown display units: ",
1681 (char *) cmd->argv[1], NULL));
1682 }
1683
1684 c = add_config_param(cmd->argv[0], 1, NULL);
1685 c->argv[0] = palloc(c->pool, sizeof(quota_units_t));
1686 *((quota_units_t *) c->argv[0]) = units;
1687
1688 return PR_HANDLED(cmd);
1689 }
1690
1691 /* usage: QuotaEngine <on|off> */
1692 MODRET set_quotaengine(cmd_rec *cmd) {
1693 int engine = -1;
1694 config_rec *c = NULL;
1695
1696 CHECK_ARGS(cmd, 1);
1697 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1698
1699 engine = get_boolean(cmd, 1);
1700 if (engine == -1) {
1701 CONF_ERROR(cmd, "expected Boolean parameter");
1702 }
1703
1704 c = add_config_param(cmd->argv[0], 1, NULL);
1705 c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
1706 *((unsigned char *) c->argv[0]) = (unsigned char) engine;
1707
1708 return PR_HANDLED(cmd);
1709 }
1710
1711 /* usage: QuotaExcludeFilter regex|"none" */
1712 MODRET set_quotaexcludefilter(cmd_rec *cmd) {
1713 #if defined(PR_USE_REGEX)
1714 pr_regex_t *pre = NULL;
1715 config_rec *c;
1716 int res;
1717
1718 CHECK_ARGS(cmd, 1);
1719 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1720
1721 if (strcasecmp(cmd->argv[1], "none") == 0) {
1722 add_config_param(cmd->argv[0], 0);
1723 return PR_HANDLED(cmd);
1724 }
1725
1726 pre = pr_regexp_alloc(&quotatab_module);
1727
1728 res = pr_regexp_compile(pre, cmd->argv[1], REG_EXTENDED|REG_NOSUB);
1729 if (res != 0) {
1730 char errstr[256] = {'\0'};
1731
1732 pr_regexp_error(res, pre, errstr, sizeof(errstr));
1733 pr_regexp_free(NULL, pre);
1734
1735 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "'", (char *) cmd->argv[1],
1736 "' failed regex compilation: ", errstr, NULL));
1737 }
1738
1739 c = add_config_param(cmd->argv[0], 2, NULL, NULL);
1740 c->argv[0] = pstrdup(c->pool, cmd->argv[1]);
1741 c->argv[1] = (void *) pre;
1742 return PR_HANDLED(cmd);
1743
1744 #else
1745 CONF_ERROR(cmd, "The QuotaExcludeFilter directive cannot be used on this "
1746 "system, as you do not have POSIX compliant regex support");
1747 #endif
1748 }
1749
1750 /* usage: QuotaLock file */
1751 MODRET set_quotalock(cmd_rec *cmd) {
1752 char *path;
1753
1754 CHECK_ARGS(cmd, 1);
1755 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1756
1757 path = cmd->argv[1];
1758
1759 /* Check for non-absolute paths */
1760 if (*path != '/') {
1761 CONF_ERROR(cmd, "absolute path required");
1762 }
1763
1764 add_config_param_str(cmd->argv[0], 1, path);
1765 return PR_HANDLED(cmd);
1766 }
1767
1768 /* usage: QuotaLog path|"none" */
1769 MODRET set_quotalog(cmd_rec *cmd) {
1770 CHECK_ARGS(cmd, 1);
1771 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1772
1773 add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
1774
1775 return PR_HANDLED(cmd);
1776 }
1777
1778 /* usage: QuotaOptions opt1 opt2 ... */
1779 MODRET set_quotaoptions(cmd_rec *cmd) {
1780 config_rec *c;
1781 register unsigned int i;
1782 unsigned long opts = 0UL;
1783
1784 if (cmd->argc-1 == 0) {
1785 CONF_ERROR(cmd, "wrong number of parameters");
1786 }
1787
1788 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1789
1790 c = add_config_param(cmd->argv[0], 1, NULL);
1791
1792 for (i = 1; i < cmd->argc; i++) {
1793 if (strcmp(cmd->argv[i], "ScanOnLogin") == 0) {
1794 opts |= QUOTA_OPT_SCAN_ON_LOGIN;
1795
1796 } else {
1797 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown QuotaOption: '",
1798 cmd->argv[i], "'", NULL));
1799 }
1800 }
1801
1802 c->argv[0] = pcalloc(c->pool, sizeof(unsigned long));
1803 *((unsigned long *) c->argv[0]) = opts;
1804
1805 if (pr_module_exists("mod_ifsession.c")) {
1806 /* These are needed in case this directive is used with mod_ifsession
1807 * configuration.
1808 */
1809 c->flags |= CF_MULTI;
1810 }
1811
1812 return PR_HANDLED(cmd);
1813 }
1814
1815 /* usage: QuotaShowQuotas <on|off> */
1816 MODRET set_quotashowquotas(cmd_rec *cmd) {
1817 int show_quotas = -1;
1818 config_rec *c = NULL;
1819
1820 CHECK_ARGS(cmd, 1);
1821 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1822
1823 show_quotas = get_boolean(cmd, 1);
1824 if (show_quotas == -1) {
1825 CONF_ERROR(cmd, "expected Boolean parameter");
1826 }
1827
1828 c = add_config_param(cmd->argv[0], 1, NULL);
1829 c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
1830 *((unsigned char *) c->argv[0]) = (unsigned char) show_quotas;
1831
1832 return PR_HANDLED(cmd);
1833 }
1834
1835 /* usage: Quota{Limit,Tally}Table <source-type:source-info> */
1836 MODRET set_quotatable(cmd_rec *cmd) {
1837 char *tmp = NULL;
1838 unsigned int tabflag = 0;
1839 #if !defined(PR_SHARED_MODULE)
1840 register quota_regtab_t *regtab = NULL;
1841 #endif /* PR_SHARED_MODULE */
1842
1843 CHECK_ARGS(cmd, 1);
1844 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1845
1846 /* Separate the parameter into the separate pieces. The parameter is
1847 * given as one string to enhance its similarity to URL syntax.
1848 */
1849 tmp = strchr(cmd->argv[1], ':');
1850 if (tmp == NULL) {
1851 CONF_ERROR(cmd, "badly formatted parameter");
1852 }
1853
1854 *tmp++ = '\0';
1855
1856 /* Verify that the requested source type has been registered, and supports
1857 * the table type (limit or tally).
1858 */
1859 if (strcasecmp(cmd->argv[0], "QuotaLimitTable") == 0) {
1860 tabflag = QUOTATAB_LIMIT_SRC;
1861
1862 } else if (strcasecmp(cmd->argv[0], "QuotaTallyTable") == 0) {
1863 tabflag = QUOTATAB_TALLY_SRC;
1864 }
1865
1866 #if !defined(PR_SHARED_MODULE)
1867 regtab = quotatab_get_backend(cmd->argv[1], tabflag);
1868 if (!regtab) {
1869 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unsupported table source type: '",
1870 cmd->argv[1], "'", NULL));
1871 }
1872 #endif /* PR_SHARED_MODULE */
1873
1874 add_config_param_str(cmd->argv[0], 2, cmd->argv[1], tmp);
1875 return PR_HANDLED(cmd);
1876 }
1877
1878 /* Variable handlers
1879 */
1880
1881 static const char *quota_get_bytes_str(void *data, size_t datasz) {
1882 const char *res = NULL;
1883 double adj = 0.0, bytes = *((double *) data);
1884
1885 switch (byte_units) {
1886 case BYTE:
1887 /* no calculation needed */
1888 if (bytes > 0.0) {
1889 char buf[PR_TUNABLE_BUFFER_SIZE];
1890 memset(buf, '\0', sizeof(buf));
1891 pr_snprintf(buf, sizeof(buf), "%.2f", bytes);
1892 res = pstrdup(session.pool, buf);
1893
1894 } else {
1895 res = pstrdup(session.pool, "(unlimited)");
1896 }
1897 break;
1898
1899 case KILO:
1900 /* Divide by 1024.0 */
1901 adj = (bytes / 1024.0);
1902
1903 if (adj > 0.0) {
1904 char buf[PR_TUNABLE_BUFFER_SIZE];
1905 memset(buf, '\0', sizeof(buf));
1906 pr_snprintf(buf, sizeof(buf), "%.2f KB", adj);
1907 res = pstrdup(session.pool, buf);
1908
1909 } else {
1910 res = pstrdup(session.pool, "(unlimited)");
1911 }
1912 break;
1913
1914 case MEGA:
1915 /* Divide by 1024.0 * 1024.0 */
1916 adj = (bytes / (1024.0 * 1024.0));
1917
1918 if (adj > 0.0) {
1919 char buf[PR_TUNABLE_BUFFER_SIZE];
1920 memset(buf, '\0', sizeof(buf));
1921 pr_snprintf(buf, sizeof(buf), "%.2f MB", adj);
1922 res = pstrdup(session.pool, buf);
1923
1924 } else {
1925 res = pstrdup(session.pool, "(unlimited)");
1926 }
1927 break;
1928
1929 case GIGA:
1930 /* Divide by 1024.0 * 1024.0 * 1024.0 */
1931 adj = (bytes / (1024.0 * 1024.0 * 1024.0));
1932
1933 if (adj > 0.0) {
1934 char buf[PR_TUNABLE_BUFFER_SIZE];
1935 memset(buf, '\0', sizeof(buf));
1936 pr_snprintf(buf, sizeof(buf), "%.2f GB", adj);
1937 res = pstrdup(session.pool, buf);
1938
1939 } else {
1940 res = pstrdup(session.pool, "(unlimited)");
1941 }
1942 break;
1943
1944 default:
1945 quotatab_log("warning: unknown QuotaDisplayUnits");
1946 break;
1947 }
1948
1949 return res;
1950 }
1951
1952 static const char *quota_get_files_str(void *data, size_t datasz) {
1953 const char *res;
1954 unsigned int files = *((unsigned int *) data);
1955
1956 if (files != 0) {
1957 char buf[PR_TUNABLE_BUFFER_SIZE];
1958 memset(buf, '\0', sizeof(buf));
1959 pr_snprintf(buf, sizeof(buf), "%u", files);
1960 res = pstrdup(session.pool, buf);
1961
1962 } else {
1963 res = pstrdup(session.pool, "(unlimited)");
1964 }
1965
1966 return res;
1967 }
1968
1969 /* Command handlers
1970 */
1971
1972 MODRET quotatab_post_abor(cmd_rec *cmd) {
1973 have_aborted_transfer = TRUE;
1974 return PR_DECLINED(cmd);
1975 }
1976
1977 MODRET quotatab_pre_appe(cmd_rec *cmd) {
1978 struct stat st;
1979
1980 have_aborted_transfer = FALSE;
1981 have_err_response = FALSE;
1982
1983 /* Sanity check */
1984 if (!use_quotas) {
1985 return PR_DECLINED(cmd);
1986 }
1987
1988 if (quotatab_ignore_path(cmd->tmp_pool, cmd->arg)) {
1989 have_quota_update = 0;
1990
1991 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
1992 (char *) cmd->argv[0], cmd->arg, quota_exclude_filter);
1993 return PR_DECLINED(cmd);
1994 }
1995
1996 /* Refresh the tally */
1997 QUOTATAB_TALLY_READ
1998
1999 /* Check quotas to see if bytes upload or total quota has been reached.
2000 * Block command if so.
2001 */
2002 if (sess_limit.bytes_in_avail > 0.0 &&
2003 sess_tally.bytes_in_used >= sess_limit.bytes_in_avail) {
2004
2005 /* Report the exceeding of the threshold. */
2006 quotatab_log("%s denied: %s quota exceeded: used %s",
2007 get_quota_type_text(sess_quota_type), (char *) cmd->argv[0],
2008 DISPLAY_BYTES_IN(cmd));
2009 pr_response_add_err(R_552, _("%s denied: quota exceeded: used %s"),
2010 (char *) cmd->argv[0], DISPLAY_BYTES_IN(cmd));
2011 have_err_response = TRUE;
2012
2013 /* Set an appropriate errno value. */
2014 errno = get_quota_exceeded_errno(EPERM, NULL);
2015
2016 return PR_ERROR(cmd);
2017
2018 } else if (sess_limit.bytes_xfer_avail > 0.0 &&
2019 sess_tally.bytes_xfer_used >= sess_limit.bytes_xfer_avail) {
2020
2021 /* Report the exceeding of the threshold. */
2022 quotatab_log("%s denied: %s quota exceeded: used %s",
2023 get_quota_type_text(sess_quota_type), (char *) cmd->argv[0],
2024 DISPLAY_BYTES_XFER(cmd));
2025 pr_response_add_err(R_552, _("%s denied: quota exceeded: used %s"),
2026 (char *) cmd->argv[0], DISPLAY_BYTES_XFER(cmd));
2027 have_err_response = TRUE;
2028
2029 /* Set an appropriate errno value. */
2030 errno = get_quota_exceeded_errno(EPERM, NULL);
2031
2032 return PR_ERROR(cmd);
2033 }
2034
2035 /* Briefly cache the size (in bytes) of the file being appended to, so that
2036 * if successful, the byte counts can be adjusted correctly.
2037 */
2038 pr_fs_clear_cache2(cmd->arg);
2039 if (pr_fsio_lstat(cmd->arg, &st) < 0) {
2040 quotatab_disk_nbytes = 0;
2041
2042 } else {
2043 quotatab_disk_nbytes = st.st_size;
2044 }
2045
2046 have_quota_update = QUOTA_HAVE_WRITE_UPDATE;
2047 return PR_DECLINED(cmd);
2048 }
2049
2050 MODRET quotatab_post_appe(cmd_rec *cmd) {
2051 struct stat st;
2052 off_t append_bytes = session.xfer.total_bytes;
2053
2054 /* sanity check */
2055 if (!use_quotas) {
2056 have_quota_update = 0;
2057 return PR_DECLINED(cmd);
2058 }
2059
2060 if (quotatab_ignore_path(cmd->tmp_pool, cmd->arg)) {
2061 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
2062 (char *) cmd->argv[0], cmd->arg, quota_exclude_filter);
2063 have_quota_update = 0;
2064 return PR_DECLINED(cmd);
2065 }
2066
2067 /* Check on the size of the appended-to file again, and use the difference
2068 * in file size as the increment. Make sure that no caching effects
2069 * mess with the stat.
2070 */
2071 pr_fs_clear_cache2(cmd->arg);
2072 if (pr_fsio_lstat(cmd->arg, &st) >= 0) {
2073 append_bytes = st.st_size - quotatab_disk_nbytes;
2074
2075 } else {
2076 if (errno == ENOENT) {
2077 append_bytes = 0;
2078
2079 } else {
2080 quotatab_log("%s: error checking '%s': %s", (char *) cmd->argv[0],
2081 cmd->arg, strerror(errno));
2082 }
2083 }
2084
2085 /* Write out an updated quota entry. */
2086 QUOTATAB_TALLY_WRITE(append_bytes, 0, session.xfer.total_bytes, 0, 0, 0)
2087
2088 /* Check the bytes quotas to see if any have been reached. Report this
2089 * to the user if so.
2090 */
2091 if (sess_limit.bytes_in_avail > 0.0 &&
2092 sess_tally.bytes_in_used >= sess_limit.bytes_in_avail) {
2093
2094 if (!have_err_response) {
2095 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
2096 DISPLAY_BYTES_IN(cmd));
2097 pr_response_add(R_DUP, _("%s: notice: quota reached: used %s"),
2098 (char *) cmd->argv[0], DISPLAY_BYTES_IN(cmd));
2099 }
2100
2101 if (sess_tally.bytes_in_used > sess_limit.bytes_in_avail &&
2102 sess_limit.quota_limit_type == HARD_LIMIT) {
2103 int res;
2104
2105 res = pr_fsio_unlink(cmd->arg);
2106 if (res < 0 &&
2107 errno == EISDIR &&
2108 use_dirs == TRUE) {
2109 res = pr_fsio_rmdir(cmd->arg);
2110 }
2111
2112 if (res < 0) {
2113 quotatab_log("notice: unable to unlink '%s': %s", cmd->arg,
2114 strerror(errno));
2115
2116 } else {
2117 QUOTATAB_TALLY_WRITE(-append_bytes, 0, -session.xfer.total_bytes,
2118 -1, 0, -1);
2119
2120 /* Report the removal of the file. */
2121 quotatab_log("%s: quota reached: '%s' removed", (char *) cmd->argv[0],
2122 cmd->arg);
2123 pr_response_add(R_DUP, _("%s: notice: quota reached: '%s' removed"),
2124 (char *) cmd->argv[0], cmd->arg);
2125 }
2126 }
2127
2128 } else if (sess_limit.bytes_xfer_avail > 0.0 &&
2129 sess_tally.bytes_xfer_used >= sess_limit.bytes_xfer_avail) {
2130
2131 if (!have_err_response) {
2132 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
2133 DISPLAY_BYTES_XFER(cmd));
2134 pr_response_add(R_DUP, _("%s: notice: quota reached: used %s"),
2135 (char *) cmd->argv[0], DISPLAY_BYTES_XFER(cmd));
2136 }
2137
2138 if (sess_tally.bytes_xfer_used > sess_limit.bytes_xfer_avail &&
2139 sess_limit.quota_limit_type == HARD_LIMIT) {
2140 if (pr_fsio_unlink(cmd->arg) < 0) {
2141 quotatab_log("notice: unable to unlink '%s': %s", cmd->arg,
2142 strerror(errno));
2143
2144 } else {
2145 QUOTATAB_TALLY_WRITE(-append_bytes, 0, -session.xfer.total_bytes,
2146 -1, 0, -1);
2147
2148 /* Report the removal of the file. */
2149 quotatab_log("%s: quota reached: '%s' removed", (char *) cmd->argv[0], cmd->arg);
2150 pr_response_add(R_DUP, _("%s: notice: quota reached: '%s' removed"),
2151 (char *) cmd->argv[0], cmd->arg);
2152 }
2153 }
2154 }
2155
2156 have_quota_update = 0;
2157 return PR_DECLINED(cmd);
2158 }
2159
2160 MODRET quotatab_post_appe_err(cmd_rec *cmd) {
2161 struct stat st;
2162 off_t append_bytes = session.xfer.total_bytes;
2163
2164 /* sanity check */
2165 if (!use_quotas) {
2166 have_quota_update = 0;
2167 return PR_DECLINED(cmd);
2168 }
2169
2170 if (quotatab_ignore_path(cmd->tmp_pool, cmd->arg)) {
2171 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
2172 (char *) cmd->argv[0], cmd->arg, quota_exclude_filter);
2173 have_quota_update = 0;
2174 return PR_DECLINED(cmd);
2175 }
2176
2177 /* Check on the size of the appended-to file again, and use the difference
2178 * in file size as the increment. Make sure that no caching effects
2179 * mess with the stat.
2180 */
2181 pr_fs_clear_cache2(cmd->arg);
2182 if (pr_fsio_lstat(cmd->arg, &st) >= 0) {
2183 append_bytes = st.st_size - quotatab_disk_nbytes;
2184
2185 } else {
2186 if (errno == ENOENT) {
2187 append_bytes = 0;
2188
2189 } else {
2190 quotatab_log("%s: error checking '%s': %s", (char *) cmd->argv[0],
2191 cmd->arg, strerror(errno));
2192 }
2193 }
2194
2195 /* Write out an updated quota entry */
2196 QUOTATAB_TALLY_WRITE(append_bytes, 0, session.xfer.total_bytes, 0, 0, 0)
2197
2198 /* Check the bytes quotas to see if any have been reached. Report this
2199 * to the user if so.
2200 */
2201 if (sess_limit.bytes_in_avail > 0.0 &&
2202 sess_tally.bytes_in_used >= sess_limit.bytes_in_avail) {
2203
2204 if (!have_err_response) {
2205 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
2206 DISPLAY_BYTES_IN(cmd));
2207 pr_response_add_err(R_DUP, _("%s: notice: quota reached: used %s"),
2208 (char *) cmd->argv[0], DISPLAY_BYTES_IN(cmd));
2209 }
2210
2211 if (sess_tally.bytes_in_used > sess_limit.bytes_in_avail &&
2212 sess_limit.quota_limit_type == HARD_LIMIT) {
2213 int res;
2214
2215 res = pr_fsio_unlink(cmd->arg);
2216 if (res < 0 &&
2217 errno == EISDIR &&
2218 use_dirs == TRUE) {
2219 res = pr_fsio_rmdir(cmd->arg);
2220 }
2221
2222 if (res < 0) {
2223 quotatab_log("notice: unable to unlink '%s': %s", cmd->arg,
2224 strerror(errno));
2225
2226 } else {
2227 QUOTATAB_TALLY_WRITE(-append_bytes, 0, -session.xfer.total_bytes,
2228 -1, 0, -1);
2229
2230 /* Report the removal of the file. */
2231 quotatab_log("%s: quota reached: '%s' removed", (char *) cmd->argv[0],
2232 cmd->arg);
2233 pr_response_add(R_DUP, _("%s: notice: quota reached: '%s' removed"),
2234 (char *) cmd->argv[0], cmd->arg);
2235 }
2236 }
2237
2238 } else if (sess_limit.bytes_xfer_avail > 0.0 &&
2239 sess_tally.bytes_xfer_used >= sess_limit.bytes_xfer_avail) {
2240
2241 if (!have_err_response) {
2242 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
2243 DISPLAY_BYTES_XFER(cmd));
2244 pr_response_add_err(R_DUP, _("%s: notice: quota reached: used %s"),
2245 (char *) cmd->argv[0], DISPLAY_BYTES_XFER(cmd));
2246 }
2247
2248 if (sess_tally.bytes_xfer_used > sess_limit.bytes_xfer_avail &&
2249 sess_limit.quota_limit_type == HARD_LIMIT) {
2250 int res;
2251
2252 res = pr_fsio_unlink(cmd->arg);
2253 if (res < 0 &&
2254 errno == EISDIR &&
2255 use_dirs == TRUE) {
2256 res = pr_fsio_rmdir(cmd->arg);
2257 }
2258
2259 if (res < 0) {
2260 quotatab_log("notice: unable to unlink '%s': %s", cmd->arg,
2261 strerror(errno));
2262
2263 } else {
2264 QUOTATAB_TALLY_WRITE(-append_bytes, 0, -session.xfer.total_bytes,
2265 -1, 0, -1);
2266
2267 /* Report the removal of the file. */
2268 quotatab_log("%s: quota reached: '%s' removed", (char *) cmd->argv[0],
2269 cmd->arg);
2270 pr_response_add(R_DUP, _("%s: notice: quota reached: '%s' removed"),
2271 (char *) cmd->argv[0], cmd->arg);
2272 }
2273 }
2274 }
2275
2276 have_quota_update = 0;
2277 return PR_DECLINED(cmd);
2278 }
2279
2280 MODRET quotatab_pre_copy(cmd_rec *cmd) {
2281 struct stat st;
2282
2283 copied_bytes = 0;
2284 have_aborted_transfer = FALSE;
2285 have_err_response = FALSE;
2286
2287 /* Sanity check */
2288 if (!use_quotas) {
2289 return PR_DECLINED(cmd);
2290 }
2291
2292 if (quotatab_ignore_path(cmd->tmp_pool, cmd->argv[1])) {
2293 have_quota_update = 0;
2294
2295 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
2296 (char *) cmd->argv[0], (char *) cmd->argv[1], quota_exclude_filter);
2297 return PR_DECLINED(cmd);
2298 }
2299
2300 /* Refresh the tally */
2301 QUOTATAB_TALLY_READ
2302
2303 /* Check quotas to see if bytes upload or total quota has been reached.
2304 * Block command if so.
2305 */
2306 if (sess_limit.bytes_in_avail > 0.0 &&
2307 sess_tally.bytes_in_used >= sess_limit.bytes_in_avail) {
2308
2309 /* Report the exceeding of the threshold. */
2310 quotatab_log("%s denied: %s quota exceeded: used %s",
2311 get_quota_type_text(sess_quota_type), (char *) cmd->argv[0],
2312 DISPLAY_BYTES_IN(cmd));
2313 pr_response_add_err(R_552, _("%s denied: quota exceeded: used %s"),
2314 (char *) cmd->argv[0], DISPLAY_BYTES_IN(cmd));
2315 have_err_response = TRUE;
2316
2317 /* Set an appropriate errno value. */
2318 errno = get_quota_exceeded_errno(EPERM, NULL);
2319
2320 return PR_ERROR(cmd);
2321
2322 } else if (sess_limit.bytes_xfer_avail > 0.0 &&
2323 sess_tally.bytes_xfer_used >= sess_limit.bytes_xfer_avail) {
2324
2325 /* Report the exceeding of the threshold. */
2326 quotatab_log("%s denied: %s quota exceeded: used %s",
2327 get_quota_type_text(sess_quota_type), (char *) cmd->argv[0],
2328 DISPLAY_BYTES_XFER(cmd));
2329 pr_response_add_err(R_552, _("%s denied: quota exceeded: used %s"),
2330 (char *) cmd->argv[0], DISPLAY_BYTES_XFER(cmd));
2331 have_err_response = TRUE;
2332
2333 /* Set an appropriate errno value. */
2334 errno = get_quota_exceeded_errno(EPERM, NULL);
2335
2336 return PR_ERROR(cmd);
2337 }
2338
2339 /* Briefly cache the size (in bytes) of the file being overwritten, so that
2340 * if successful, the byte counts can be adjusted correctly.
2341 */
2342 pr_fs_clear_cache2(cmd->argv[2]);
2343 if (pr_fsio_stat(cmd->argv[2], &st) < 0) {
2344 quotatab_disk_nbytes = 0;
2345
2346 if (errno == ENOENT) {
2347 quotatab_disk_nfiles = 1;
2348
2349 } else {
2350 quotatab_disk_nfiles = 0;
2351 }
2352
2353 } else {
2354
2355 if (!S_ISDIR(st.st_mode) ||
2356 (S_ISDIR(st.st_mode) && use_dirs == TRUE)) {
2357 quotatab_disk_nbytes = st.st_size;
2358 quotatab_disk_nfiles = 0;
2359 }
2360 }
2361
2362 if (quotatab_disk_nfiles == 1) {
2363 /* Check quotas to see if files upload or total quota has been reached,
2364 * but only if the destination file did not already exist. Block the
2365 * command if the file quotas were exceeded.
2366 */
2367 if (sess_limit.files_in_avail != 0 &&
2368 sess_tally.files_in_used >= sess_limit.files_in_avail) {
2369
2370 /* Report the exceeding of the threshold. */
2371 quotatab_log("%s denied: %s quota exceeded: used %s",
2372 get_quota_type_text(sess_quota_type), (char *) cmd->argv[0],
2373 DISPLAY_FILES_IN(cmd));
2374 pr_response_add_err(R_552, _("%s denied: quota exceeded: used %s"),
2375 (char *) cmd->argv[0], DISPLAY_FILES_IN(cmd));
2376 have_err_response = TRUE;
2377
2378 /* Set an appropriate errno value. */
2379 errno = get_quota_exceeded_errno(EPERM, NULL);
2380
2381 return PR_ERROR(cmd);
2382
2383 } else if (sess_limit.files_xfer_avail != 0 &&
2384 sess_tally.files_xfer_used >= sess_limit.files_xfer_avail) {
2385
2386 /* Report the exceeding of the threshold. */
2387 quotatab_log("%s denied: %s quota exceeded: used %s",
2388 get_quota_type_text(sess_quota_type), (char *) cmd->argv[0],
2389 DISPLAY_FILES_XFER(cmd));
2390 pr_response_add_err(R_552, _("%s denied: quota exceeded: used %s"),
2391 (char *) cmd->argv[0], DISPLAY_FILES_XFER(cmd));
2392 have_err_response = TRUE;
2393
2394 /* Set an appropriate errno value. */
2395 errno = get_quota_exceeded_errno(EPERM, NULL);
2396
2397 return PR_ERROR(cmd);
2398 }
2399 }
2400
2401 have_quota_update = QUOTA_HAVE_WRITE_UPDATE;
2402 return PR_DECLINED(cmd);
2403 }
2404
2405 MODRET quotatab_post_copy(cmd_rec *cmd) {
2406 struct stat st;
2407 off_t copy_bytes = 0;
2408 int dst_truncated = FALSE;
2409
2410 copied_bytes = 0;
2411
2412 /* Sanity check */
2413 if (!use_quotas) {
2414 have_quota_update = 0;
2415 return PR_DECLINED(cmd);
2416 }
2417
2418 if (quotatab_ignore_path(cmd->tmp_pool, cmd->argv[2])) {
2419 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
2420 (char *) cmd->argv[0], (char *) cmd->argv[2], quota_exclude_filter);
2421 have_quota_update = 0;
2422 return PR_DECLINED(cmd);
2423 }
2424
2425 pr_fs_clear_cache2(cmd->argv[2]);
2426 if (pr_fsio_stat(cmd->argv[2], &st) == 0) {
2427 if (quotatab_disk_nfiles == 0) {
2428
2429 if (!S_ISDIR(st.st_mode) ||
2430 (S_ISDIR(st.st_mode) && use_dirs == TRUE)) {
2431 /* If the destination file already existed, the number of bytes
2432 * copied is the current size less its previous size. Unless its
2433 * current size is smaller than its previous size...
2434 */
2435
2436 if (st.st_size >= quotatab_disk_nbytes) {
2437 copy_bytes = st.st_size - quotatab_disk_nbytes;
2438
2439 } else {
2440 copy_bytes = quotatab_disk_nbytes - st.st_size;
2441 dst_truncated = TRUE;
2442 }
2443 }
2444
2445 } else {
2446 if (!S_ISDIR(st.st_mode) ||
2447 (S_ISDIR(st.st_mode) && use_dirs == TRUE)) {
2448 /* ... otherwise, its the entire size of the destination file. */
2449 copy_bytes = st.st_size;
2450 }
2451 }
2452 }
2453
2454 /* Write out an updated quota entry. If the destination file was truncated
2455 * (i.e. made smaller) because of this copy, then we need to write
2456 * negative byte values, not positive.
2457 */
2458 if (!dst_truncated) {
2459 QUOTATAB_TALLY_WRITE(copy_bytes, 0, copy_bytes,
2460 quotatab_disk_nfiles, 0, quotatab_disk_nfiles)
2461
2462 } else {
2463 QUOTATAB_TALLY_WRITE(-copy_bytes, 0, -copy_bytes,
2464 quotatab_disk_nfiles, 0, quotatab_disk_nfiles)
2465 }
2466
2467 /* Check quotas to see if bytes upload or total quota has been reached.
2468 * Report this to user if so. If it is a hard bytes limit, delete the
2469 * uploaded file.
2470 */
2471 if (sess_limit.bytes_in_avail > 0.0 &&
2472 sess_tally.bytes_in_used >= sess_limit.bytes_in_avail) {
2473
2474 if (!have_err_response) {
2475 /* Report the reaching of the threshold. */
2476 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
2477 DISPLAY_BYTES_IN(cmd));
2478 pr_response_add(R_DUP, _("%s: notice: quota reached: used %s"),
2479 (char *) cmd->argv[0], DISPLAY_BYTES_IN(cmd));
2480 }
2481
2482 if (sess_tally.bytes_in_used > sess_limit.bytes_in_avail &&
2483 sess_limit.quota_limit_type == HARD_LIMIT) {
2484 int res;
2485
2486 res = pr_fsio_unlink(cmd->argv[2]);
2487 if (res < 0 &&
2488 errno == EISDIR &&
2489 use_dirs == TRUE) {
2490 res = pr_fsio_rmdir(cmd->argv[2]);
2491 }
2492
2493 if (res < 0) {
2494 quotatab_log("notice: unable to unlink '%s': %s", (char *) cmd->argv[2],
2495 strerror(errno));
2496
2497 } else {
2498 QUOTATAB_TALLY_WRITE(-copy_bytes, 0, -copy_bytes,
2499 -quotatab_disk_nfiles, 0, -quotatab_disk_nfiles);
2500
2501 /* Report the removal of the file. */
2502 quotatab_log("%s: quota reached: '%s' removed", (char *) cmd->argv[0],
2503 (char *) cmd->argv[2]);
2504 pr_response_add(R_DUP, _("%s: notice: quota reached: '%s' removed"),
2505 (char *) cmd->argv[0], (char *) cmd->argv[2]);
2506 }
2507 }
2508
2509 } else if (sess_limit.bytes_xfer_avail > 0.0 &&
2510 sess_tally.bytes_xfer_used >= sess_limit.bytes_xfer_avail) {
2511
2512 if (!have_err_response) {
2513 /* Report the reaching of the threshold. */
2514 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
2515 DISPLAY_BYTES_XFER(cmd));
2516 pr_response_add(R_DUP, _("%s: notice: quota reached: used %s"),
2517 (char *) cmd->argv[0], DISPLAY_BYTES_XFER(cmd));
2518 }
2519
2520 if (sess_tally.bytes_xfer_used > sess_limit.bytes_xfer_avail &&
2521 sess_limit.quota_limit_type == HARD_LIMIT) {
2522 int res;
2523
2524 res = pr_fsio_unlink(cmd->argv[2]);
2525 if (res < 0 &&
2526 errno == EISDIR &&
2527 use_dirs == TRUE) {
2528 res = pr_fsio_rmdir(cmd->argv[2]);
2529 }
2530
2531 if (res < 0) {
2532 quotatab_log("notice: unable to unlink '%s': %s", (char *) cmd->argv[2],
2533 strerror(errno));
2534
2535 } else {
2536 QUOTATAB_TALLY_WRITE(-copy_bytes, 0, -copy_bytes,
2537 -quotatab_disk_nfiles, 0, -quotatab_disk_nfiles);
2538
2539 /* Report the removal of the file. */
2540 quotatab_log("%s: quota reached: '%s' removed", (char *) cmd->argv[0],
2541 (char *) cmd->argv[2]);
2542 pr_response_add(R_DUP, _("%s: notice: quota reached: '%s' removed"),
2543 (char *) cmd->argv[0], (char *) cmd->argv[2]);
2544 }
2545 }
2546 }
2547
2548 if (quotatab_disk_nfiles == 1) {
2549 /* Check quotas to see if files upload or total quota has been reached,
2550 * but only if the destination file did not already exist. Report this
2551 * to user if the file quotas were exceeded.
2552 */
2553 if (sess_limit.files_in_avail != 0 &&
2554 sess_tally.files_in_used >= sess_limit.files_in_avail) {
2555
2556 if (!have_err_response) {
2557 /* Report the reaching of the threshold. */
2558 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
2559 DISPLAY_FILES_IN(cmd));
2560 pr_response_add(R_DUP, _("%s: notice: quota reached: used %s"),
2561 (char *) cmd->argv[0], DISPLAY_FILES_IN(cmd));
2562 }
2563
2564 } else if (sess_limit.files_xfer_avail != 0 &&
2565 sess_tally.files_xfer_used >= sess_limit.files_xfer_avail) {
2566
2567 if (!have_err_response) {
2568 /* Report the reaching of the threshold. */
2569 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
2570 DISPLAY_FILES_XFER(cmd));
2571 pr_response_add(R_DUP, _("%s: notice: quota reached: used %s"),
2572 (char *) cmd->argv[0], DISPLAY_FILES_XFER(cmd));
2573 }
2574 }
2575 }
2576
2577 /* Clear the cached bytes/files. */
2578 quotatab_disk_nbytes = 0;
2579 quotatab_disk_nfiles = 0;
2580
2581 have_quota_update = 0;
2582 return PR_DECLINED(cmd);
2583 }
2584
2585 MODRET quotatab_post_copy_err(cmd_rec *cmd) {
2586 copied_bytes = 0;
2587
2588 /* Sanity check */
2589 if (!use_quotas) {
2590 have_quota_update = 0;
2591 return PR_DECLINED(cmd);
2592 }
2593
2594 /* Clear the cached bytes/files. */
2595 quotatab_disk_nbytes = 0;
2596 quotatab_disk_nfiles = 0;
2597
2598 have_quota_update = 0;
2599 return PR_DECLINED(cmd);
2600 }
2601
2602 MODRET quotatab_pre_dele(cmd_rec *cmd) {
2603 char *path;
2604
2605 /* sanity check */
2606 if (!use_quotas)
2607 return PR_DECLINED(cmd);
2608
2609 /* The real DELE handler makes sure that, if a symlink is being deleted,
2610 * only the symlink (and not the pointed-to file) is deleted. We need
2611 * to do the same here, so that the bytes are correct.
2612 */
2613 path = dir_canonical_path(cmd->tmp_pool,
2614 pr_fs_decode_path(cmd->tmp_pool, cmd->arg));
2615
2616 quotatab_have_dele_st = FALSE;
2617
2618 if (path) {
2619 if (quotatab_ignore_path(cmd->tmp_pool, cmd->arg)) {
2620 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
2621 (char *) cmd->argv[0], cmd->arg, quota_exclude_filter);
2622 return PR_DECLINED(cmd);
2623 }
2624
2625 /* Briefly cache the size (in bytes) of the file to be deleted, so that
2626 * if successful, the byte counts can be adjusted correctly.
2627 */
2628 pr_fs_clear_cache2(path);
2629 if (pr_fsio_lstat(path, &quotatab_dele_st) < 0) {
2630 quotatab_disk_nbytes = 0;
2631
2632 } else {
2633 quotatab_disk_nbytes = quotatab_dele_st.st_size;
2634 quotatab_have_dele_st = TRUE;
2635 have_quota_update = QUOTA_HAVE_WRITE_UPDATE;
2636 }
2637
2638 } else {
2639 quotatab_disk_nbytes = 0;
2640 }
2641
2642 return PR_DECLINED(cmd);
2643 }
2644
2645 MODRET quotatab_post_dele(cmd_rec *cmd) {
2646
2647 /* sanity check */
2648 if (use_quotas == FALSE) {
2649 return PR_DECLINED(cmd);
2650 }
2651
2652 if (quotatab_ignore_path(cmd->tmp_pool, cmd->arg)) {
2653 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
2654 (char *) cmd->argv[0], cmd->arg, quota_exclude_filter);
2655 return PR_DECLINED(cmd);
2656 }
2657
2658 if (quotatab_have_dele_st) {
2659
2660 /* Check the ownership of the deleted file against the currently
2661 * logged-in user/group. If the file belongs to us, we can update
2662 * our tally as normal. Otherwise, we try to give the credit for the
2663 * deleted bytes to the owner of the file.
2664 */
2665
2666 if (quotatab_dele_st.st_uid == session.uid) {
2667 /* Write out an updated quota entry. */
2668 QUOTATAB_TALLY_WRITE(-quotatab_disk_nbytes, 0, 0, -1, 0, 0)
2669
2670 } else {
2671 const char *group_owner, *user_owner, *path;
2672 quota_limit_t dele_limit;
2673 quota_tally_t dele_tally;
2674
2675 path = dir_canonical_path(cmd->tmp_pool,
2676 pr_fs_decode_path(cmd->tmp_pool, cmd->arg));
2677
2678 /* 1. Get the user name for the st.st_uid.
2679 * 2. Check for a USER_QUOTA limit for this user.
2680 * 3. If have limit, check for USER_QUOTA tally.
2681 * 4. If have tally, update _that_ tally.
2682 *
2683 * Do the same check for the st.st_gid, too.
2684 */
2685
2686 user_owner = pr_auth_uid2name(cmd->tmp_pool, quotatab_dele_st.st_uid);
2687 group_owner = pr_auth_gid2name(cmd->tmp_pool, quotatab_dele_st.st_gid);
2688
2689 quotatab_log("deleted file '%s' belongs to user '%s' (UID %s), "
2690 "not the current user '%s' (UID %s); attempting to credit user '%s' "
2691 "for the deleted bytes", path, user_owner,
2692 pr_uid2str(cmd->tmp_pool, quotatab_dele_st.st_uid), session.user,
2693 pr_uid2str(cmd->tmp_pool, session.uid), user_owner);
2694
2695 quotatab_mutex_lock(F_WRLCK);
2696
2697 if (quotatab_lookup(TYPE_LIMIT, &dele_limit, user_owner, USER_QUOTA)) {
2698 quotatab_log("found limit entry for user '%s'", user_owner);
2699
2700 if (quotatab_lookup(TYPE_TALLY, &dele_tally, user_owner, USER_QUOTA)) {
2701 quotatab_log("found tally entry for user '%s'", user_owner);
2702 quotatab_mutex_lock(F_UNLCK);
2703
2704 if (quotatab_write(&dele_tally, -quotatab_disk_nbytes, 0, 0, -1,
2705 0, 0) < 0) {
2706 quotatab_log("error: unable to write tally: %s", strerror(errno));
2707
2708 } else {
2709 quotatab_log("credited user '%s' for the deleted file/bytes",
2710 user_owner);
2711 }
2712
2713 have_quota_update = 0;
2714
2715 } else {
2716 quotatab_log("no tally entry found for user '%s'", user_owner);
2717 quotatab_mutex_lock(F_UNLCK);
2718
2719 /* Credit the current user for these bytes anyway; that's the
2720 * default behavior anyway.
2721 */
2722 QUOTATAB_TALLY_WRITE(-quotatab_disk_nbytes, 0, 0, -1, 0, 0)
2723 }
2724
2725 } else {
2726 quotatab_log("no limit entry found for user '%s'", user_owner);
2727
2728 /* Look for a quota for the group owner, too. */
2729 if (quotatab_lookup(TYPE_LIMIT, &dele_limit, group_owner,
2730 GROUP_QUOTA)) {
2731 quotatab_log("found limit entry for group '%s'", group_owner);
2732
2733 if (quotatab_lookup(TYPE_TALLY, &dele_tally, group_owner,
2734 GROUP_QUOTA)) {
2735 quotatab_log("found tally entry for group '%s'", group_owner);
2736 quotatab_mutex_lock(F_UNLCK);
2737
2738 if (quotatab_write(&dele_tally, -quotatab_disk_nbytes, 0, 0, -1,
2739 0, 0) < 0) {
2740 quotatab_log("error: unable to write tally: %s", strerror(errno));
2741
2742 } else {
2743 quotatab_log("credited group '%s' for the deleted file/bytes",
2744 group_owner);
2745 }
2746
2747 have_quota_update = 0;
2748
2749 } else {
2750 quotatab_log("no tally entry found for group '%s'", group_owner);
2751 quotatab_mutex_lock(F_UNLCK);
2752
2753 /* Credit the current user for these bytes anyway; that's the
2754 * default behavior anyway.
2755 */
2756 QUOTATAB_TALLY_WRITE(-quotatab_disk_nbytes, 0, 0, -1, 0, 0)
2757 }
2758
2759 } else {
2760 quotatab_mutex_lock(F_UNLCK);
2761
2762 /* Credit the current user for these bytes anyway; that's the
2763 * default behavior anyway.
2764 */
2765 QUOTATAB_TALLY_WRITE(-quotatab_disk_nbytes, 0, 0, -1, 0, 0)
2766 }
2767 }
2768 }
2769 }
2770
2771 /* NOTE: if use_dirs is TRUE, also take into consideration the decreased
2772 * disk usage caused by any decrease in the size of the containing directory.
2773 */
2774
2775 /* Clear the cached bytes. */
2776 quotatab_disk_nbytes = 0;
2777
2778 return PR_DECLINED(cmd);
2779 }
2780
2781 MODRET quotatab_post_dele_err(cmd_rec *cmd) {
2782
2783 /* sanity check */
2784 if (use_quotas == FALSE) {
2785 return PR_DECLINED(cmd);
2786 }
2787
2788 /* Clear the cached bytes. */
2789 quotatab_disk_nbytes = 0;
2790
2791 /* Clear the update flag as well. */
2792 have_quota_update = 0;
2793
2794 return PR_DECLINED(cmd);
2795 }
2796
2797 MODRET quotatab_pre_mkd(cmd_rec *cmd) {
2798
2799 /* Sanity check. */
2800 if (!use_dirs)
2801 return PR_DECLINED(cmd);
2802
2803 /* Use quotatab_pre_stor() for most of the work. */
2804 return quotatab_pre_stor(cmd);
2805 }
2806
2807 MODRET quotatab_post_mkd(cmd_rec *cmd) {
2808
2809 /* Sanity check. */
2810 if (!use_dirs)
2811 return PR_DECLINED(cmd);
2812
2813 /* Use quotatab_post_stor() for most of the work. */
2814 return quotatab_post_stor(cmd);
2815 }
2816
2817 MODRET quotatab_post_mkd_err(cmd_rec *cmd) {
2818
2819 /* Sanity check. */
2820 if (!use_dirs)
2821 return PR_DECLINED(cmd);
2822
2823 /* Use quotatab_post_stor_err() for most of the work. */
2824 return quotatab_post_stor_err(cmd);
2825 }
2826
2827 MODRET quotatab_post_pass(cmd_rec *cmd) {
2828 unsigned char have_limit_entry = FALSE;
2829 have_quota_entry = FALSE;
2830
2831 /* Be done now if there is no quota table */
2832 if (!use_quotas ||
2833 !have_quota_limit_table ||
2834 !have_quota_tally_table) {
2835 use_quotas = FALSE;
2836 quotatab_log("turning QuotaEngine off");
2837 return PR_DECLINED(cmd);
2838 }
2839
2840 quotatab_mutex_lock(F_WRLCK);
2841
2842 /* Check for a limit and a tally entry for this user. */
2843 if (quotatab_lookup(TYPE_LIMIT, &sess_limit, session.user, USER_QUOTA)) {
2844 quotatab_log("found limit entry for user '%s'", session.user);
2845 have_limit_entry = TRUE;
2846
2847 if (quotatab_lookup(TYPE_TALLY, &sess_tally, session.user, USER_QUOTA)) {
2848 quotatab_log("found tally entry for user '%s'", session.user);
2849 have_quota_entry = TRUE;
2850
2851 } else {
2852 if (quotatab_create_tally()) {
2853 quotatab_log("created tally entry for user '%s'", session.user);
2854 have_quota_entry = TRUE;
2855 }
2856 }
2857
2858 quotatab_mutex_lock(F_UNLCK);
2859
2860 if (have_quota_entry) {
2861 if ((quotatab_opts & QUOTA_OPT_SCAN_ON_LOGIN) &&
2862 (sess_limit.bytes_in_avail > 0 ||
2863 sess_limit.files_in_avail > 0)) {
2864 double byte_count = 0;
2865 unsigned int file_count = 0;
2866 time_t then;
2867
2868 quotatab_log("ScanOnLogin enabled, scanning current directory '%s' "
2869 "for files owned by user '%s'", pr_fs_getcwd(), session.user);
2870
2871 time(&then);
2872 if (quotatab_scan_dir(cmd->tmp_pool, pr_fs_getcwd(), session.uid, -1,
2873 0, &byte_count, &file_count) < 0) {
2874 quotatab_log("unable to scan '%s': %s", pr_fs_getcwd(),
2875 strerror(errno));
2876
2877 } else {
2878 double bytes_diff = (double)
2879 (byte_count - sess_tally.bytes_in_used);
2880 int files_diff = file_count - sess_tally.files_in_used;
2881
2882 quotatab_log("found %0.2lf bytes in %u %s for user '%s' "
2883 "in %lu secs", byte_count, file_count,
2884 file_count != 1 ? "files" : "file", session.user,
2885 (unsigned long) time(NULL) - then);
2886
2887 quotatab_log("updating tally (%0.2lf bytes, %d %s difference)",
2888 bytes_diff, files_diff, files_diff != 1 ? "files" : "file");
2889
2890 /* Write out an updated quota entry */
2891 QUOTATAB_TALLY_WRITE(bytes_diff, 0, 0, files_diff, 0, 0);
2892 }
2893 }
2894 }
2895 }
2896
2897 /* Check for a limit and a tally entry for these groups. */
2898 if (!have_limit_entry) {
2899 const char *group_name = session.group;
2900 gid_t group_id = session.gid;
2901
2902 if (quotatab_lookup(TYPE_LIMIT, &sess_limit, group_name, GROUP_QUOTA)) {
2903 quotatab_log("found limit entry for group '%s'", group_name);
2904 have_limit_entry = TRUE;
2905
2906 } else {
2907 if (session.groups) {
2908 register unsigned int i = 0;
2909
2910 char **group_names = session.groups->elts;
2911 gid_t *group_ids = session.gids->elts;
2912
2913 /* Scan the list of supplemental group memberships for this user. */
2914 for (i = 0; i < session.groups->nelts; i++) {
2915 group_name = group_names[i];
2916 group_id = group_ids[i];
2917
2918 if (quotatab_lookup(TYPE_LIMIT, &sess_limit, group_name,
2919 GROUP_QUOTA)) {
2920 quotatab_log("found limit entry for group '%s'", group_name);
2921 have_limit_entry = TRUE;
2922 break;
2923 }
2924 }
2925 }
2926 }
2927
2928 if (have_limit_entry) {
2929 if (quotatab_lookup(TYPE_TALLY, &sess_tally, group_name, GROUP_QUOTA)) {
2930 quotatab_log("found tally entry for group '%s'", group_name);
2931 have_quota_entry = TRUE;
2932
2933 } else {
2934 if (quotatab_create_tally()) {
2935 quotatab_log("created tally entry for group '%s'", group_name);
2936 have_quota_entry = TRUE;
2937 }
2938 }
2939
2940 quotatab_mutex_lock(F_UNLCK);
2941
2942 if (have_quota_entry) {
2943 if ((quotatab_opts & QUOTA_OPT_SCAN_ON_LOGIN) &&
2944 (sess_limit.bytes_in_avail > 0 ||
2945 sess_limit.files_in_avail > 0)) {
2946 double byte_count = 0;
2947 unsigned int file_count = 0;
2948 time_t then;
2949
2950 quotatab_log("ScanOnLogin enabled, scanning current directory '%s' "
2951 "for files owned by group '%s'", pr_fs_getcwd(), group_name);
2952
2953 time(&then);
2954 if (quotatab_scan_dir(cmd->tmp_pool, pr_fs_getcwd(), -1, group_id,
2955 0, &byte_count, &file_count) < 0) {
2956 quotatab_log("unable to scan '%s': %s", pr_fs_getcwd(),
2957 strerror(errno));
2958
2959 } else {
2960 double bytes_diff = byte_count - sess_tally.bytes_in_used;
2961 int files_diff = file_count - sess_tally.files_in_used;
2962
2963 quotatab_log("found %0.2lf bytes in %u %s for group '%s' "
2964 "in %lu secs", byte_count, file_count,
2965 file_count != 1 ? "files" : "file", group_name,
2966 (unsigned long) time(NULL) - then);
2967
2968 quotatab_log("updating tally (%0.2lf bytes, %d %s difference)",
2969 bytes_diff, files_diff, files_diff != 1 ? "files" : "file");
2970
2971 /* Write out an updated quota entry */
2972 QUOTATAB_TALLY_WRITE(bytes_diff, 0, 0, files_diff, 0, 0);
2973 }
2974 }
2975 }
2976 }
2977 }
2978
2979 if (!have_limit_entry) {
2980 if (quotatab_lookup_default(TYPE_LIMIT, &sess_limit, session.user,
2981 USER_QUOTA)) {
2982 quotatab_log("found limit entry for user '%s'", session.user);
2983 have_limit_entry = TRUE;
2984
2985 if (quotatab_lookup(TYPE_TALLY, &sess_tally, session.user, USER_QUOTA)) {
2986 quotatab_log("found tally entry for user '%s'", session.user);
2987 have_quota_entry = TRUE;
2988
2989 } else {
2990 if (quotatab_create_tally()) {
2991 quotatab_log("created tally entry for user '%s'", session.user);
2992 have_quota_entry = TRUE;
2993 }
2994 }
2995
2996 quotatab_mutex_lock(F_UNLCK);
2997
2998 if (have_quota_entry) {
2999 if ((quotatab_opts & QUOTA_OPT_SCAN_ON_LOGIN) &&
3000 (sess_limit.bytes_in_avail > 0 ||
3001 sess_limit.files_in_avail > 0)) {
3002 double byte_count = 0;
3003 unsigned int file_count = 0;
3004 time_t then;
3005
3006 quotatab_log("ScanOnLogin enabled, scanning current directory '%s' "
3007 "for files owned by user '%s'", pr_fs_getcwd(), session.user);
3008
3009 time(&then);
3010 if (quotatab_scan_dir(cmd->tmp_pool, pr_fs_getcwd(), session.uid, -1,
3011 0, &byte_count, &file_count) < 0) {
3012 quotatab_log("unable to scan '%s': %s", pr_fs_getcwd(),
3013 strerror(errno));
3014
3015 } else {
3016 double bytes_diff = (double)
3017 (byte_count - sess_tally.bytes_in_used);
3018 int files_diff = file_count - sess_tally.files_in_used;
3019
3020 quotatab_log("found %0.2lf bytes in %u %s for user '%s' "
3021 "in %lu secs", byte_count, file_count,
3022 file_count != 1 ? "files" : "file", session.user,
3023 (unsigned long) time(NULL) - then);
3024
3025 quotatab_log("updating tally (%0.2lf bytes, %d %s difference)",
3026 bytes_diff, files_diff, files_diff != 1 ? "files" : "file");
3027
3028 /* Write out an updated quota entry */
3029 QUOTATAB_TALLY_WRITE(bytes_diff, 0, 0, files_diff, 0, 0);
3030 }
3031 }
3032 }
3033 }
3034 }
3035
3036 if (!have_limit_entry) {
3037 const char *group_name = session.group;
3038 gid_t group_id = session.gid;
3039
3040 if (quotatab_lookup_default(TYPE_LIMIT, &sess_limit, group_name,
3041 GROUP_QUOTA)) {
3042 quotatab_log("found limit entry for group '%s'", group_name);
3043 have_limit_entry = TRUE;
3044
3045 } else {
3046 if (session.groups) {
3047 register unsigned int i = 0;
3048
3049 char **group_names = session.groups->elts;
3050 gid_t *group_ids = session.gids->elts;
3051
3052 /* Scan the list of supplemental group memberships for this user. */
3053 for (i = 0; i < session.groups->nelts; i++) {
3054 group_name = group_names[i];
3055 group_id = group_ids[i];
3056
3057 if (quotatab_lookup_default(TYPE_LIMIT, &sess_limit, group_name,
3058 GROUP_QUOTA)) {
3059 quotatab_log("found limit entry for group '%s'", group_name);
3060 have_limit_entry = TRUE;
3061 break;
3062 }
3063 }
3064 }
3065 }
3066
3067 if (have_limit_entry) {
3068 if (quotatab_lookup(TYPE_TALLY, &sess_tally, group_name, GROUP_QUOTA)) {
3069 quotatab_log("found tally entry for group '%s'", group_name);
3070 have_quota_entry = TRUE;
3071
3072 } else {
3073 if (quotatab_create_tally()) {
3074 quotatab_log("created tally entry for group '%s'", group_name);
3075 have_quota_entry = TRUE;
3076 }
3077 }
3078
3079 quotatab_mutex_lock(F_UNLCK);
3080
3081 if (have_quota_entry) {
3082 if ((quotatab_opts & QUOTA_OPT_SCAN_ON_LOGIN) &&
3083 (sess_limit.bytes_in_avail > 0 ||
3084 sess_limit.files_in_avail > 0)) {
3085 double byte_count = 0;
3086 unsigned int file_count = 0;
3087 time_t then;
3088
3089 quotatab_log("ScanOnLogin enabled, scanning current directory '%s' "
3090 "for files owned by group '%s'", pr_fs_getcwd(), group_name);
3091
3092 time(&then);
3093 if (quotatab_scan_dir(cmd->tmp_pool, pr_fs_getcwd(), -1, group_id,
3094 0, &byte_count, &file_count) < 0) {
3095 quotatab_log("unable to scan '%s': %s", pr_fs_getcwd(),
3096 strerror(errno));
3097
3098 } else {
3099 double bytes_diff = byte_count - sess_tally.bytes_in_used;
3100 int files_diff = file_count - sess_tally.files_in_used;
3101
3102 quotatab_log("found %0.2lf bytes in %u %s for group '%s' "
3103 "in %lu secs", byte_count, file_count,
3104 file_count != 1 ? "files" : "file", group_name,
3105 (unsigned long) time(NULL) - then);
3106
3107 quotatab_log("updating tally (%0.2lf bytes, %d %s difference)",
3108 bytes_diff, files_diff, files_diff != 1 ? "files" : "file");
3109
3110 /* Write out an updated quota entry */
3111 QUOTATAB_TALLY_WRITE(bytes_diff, 0, 0, files_diff, 0, 0);
3112 }
3113 }
3114 }
3115 }
3116 }
3117
3118 /* Check for a limit and a tally entry for this class. */
3119 if (!have_limit_entry &&
3120 session.conn_class != NULL) {
3121 if (quotatab_lookup(TYPE_LIMIT, &sess_limit, session.conn_class->cls_name,
3122 CLASS_QUOTA)) {
3123 quotatab_log("found limit entry for class '%s'",
3124 session.conn_class->cls_name);
3125 have_limit_entry = TRUE;
3126
3127 if (quotatab_lookup(TYPE_TALLY, &sess_tally, session.conn_class->cls_name,
3128 CLASS_QUOTA)) {
3129 quotatab_log("found tally entry for class '%s'",
3130 session.conn_class->cls_name);
3131 have_quota_entry = TRUE;
3132
3133 } else {
3134 if (quotatab_create_tally()) {
3135 quotatab_log("created tally entry for class '%s'",
3136 session.conn_class->cls_name);
3137 have_quota_entry = TRUE;
3138 }
3139 }
3140
3141 quotatab_mutex_lock(F_UNLCK);
3142
3143 if (have_quota_entry) {
3144 if ((quotatab_opts & QUOTA_OPT_SCAN_ON_LOGIN) &&
3145 (sess_limit.bytes_in_avail > 0 ||
3146 sess_limit.files_in_avail > 0)) {
3147 double byte_count = 0;
3148 unsigned int file_count = 0;
3149 time_t then;
3150
3151 quotatab_log("ScanOnLogin enabled, scanning current directory '%s' "
3152 "for files owned by class '%s'", pr_fs_getcwd(),
3153 session.conn_class->cls_name);
3154
3155 time(&then);
3156 if (quotatab_scan_dir(cmd->tmp_pool, pr_fs_getcwd(), -1, -1, 0,
3157 &byte_count, &file_count) < 0) {
3158 quotatab_log("unable to scan '%s': %s", pr_fs_getcwd(),
3159 strerror(errno));
3160
3161 } else {
3162 double bytes_diff = (double)
3163 (byte_count - sess_tally.bytes_in_used);
3164 int files_diff = file_count - sess_tally.files_in_used;
3165
3166 quotatab_log("found %0.2lf bytes in %u %s for class '%s' "
3167 "in %lu secs", byte_count, file_count,
3168 file_count != 1 ? "files" : "file", session.conn_class->cls_name,
3169 (unsigned long) time(NULL) - then);
3170
3171 quotatab_log("updating tally (%0.2lf bytes, %d %s difference)",
3172 bytes_diff, files_diff, files_diff != 1 ? "files" : "file");
3173
3174 /* Write out an updated quota entry */
3175 QUOTATAB_TALLY_WRITE(bytes_diff, 0, 0, files_diff, 0, 0);
3176 }
3177 }
3178 }
3179 }
3180 }
3181
3182 /* Check for a limit and a tally entry for everyone. */
3183 if (!have_limit_entry) {
3184 if (quotatab_lookup(TYPE_LIMIT, &sess_limit, NULL, ALL_QUOTA)) {
3185 quotatab_log("found limit entry for all");
3186 have_limit_entry = TRUE;
3187
3188 if (quotatab_lookup(TYPE_TALLY, &sess_tally, NULL, ALL_QUOTA)) {
3189 quotatab_log("found tally entry for all");
3190 have_quota_entry = TRUE;
3191
3192 } else {
3193 if (quotatab_create_tally()) {
3194 quotatab_log("created tally entry for all");
3195 have_quota_entry = TRUE;
3196 }
3197 }
3198
3199 quotatab_mutex_lock(F_UNLCK);
3200
3201 if (have_quota_entry) {
3202 if ((quotatab_opts & QUOTA_OPT_SCAN_ON_LOGIN) &&
3203 (sess_limit.bytes_in_avail > 0 ||
3204 sess_limit.files_in_avail > 0)) {
3205 double byte_count = 0;
3206 unsigned int file_count = 0;
3207 time_t then;
3208
3209 quotatab_log("ScanOnLogin enabled, scanning current directory '%s' "
3210 "for files owned by all", pr_fs_getcwd());
3211
3212 time(&then);
3213 if (quotatab_scan_dir(cmd->tmp_pool, pr_fs_getcwd(), -1, -1, 0,
3214 &byte_count, &file_count) < 0) {
3215 quotatab_log("unable to scan '%s': %s", pr_fs_getcwd(),
3216 strerror(errno));
3217
3218 } else {
3219 double bytes_diff = (double)
3220 (byte_count - sess_tally.bytes_in_used);
3221 int files_diff = file_count - sess_tally.files_in_used;
3222
3223 quotatab_log("found %0.2lf bytes in %u %s for all "
3224 "in %lu secs", byte_count, file_count,
3225 file_count != 1 ? "files" : "file",
3226 (unsigned long) time(NULL) - then);
3227
3228 quotatab_log("updating tally (%0.2lf bytes, %d %s difference)",
3229 bytes_diff, files_diff, files_diff != 1 ? "files" : "file");
3230
3231 /* Write out an updated quota entry */
3232 QUOTATAB_TALLY_WRITE(bytes_diff, 0, 0, files_diff, 0, 0);
3233 }
3234 }
3235 }
3236 }
3237 }
3238
3239 /* Note: We _could_ close the limit table here. However, we keep it
3240 * open anyway, in order to look up limits for _other_ users (e.g. when
3241 * handling deleted files).
3242 */
3243
3244 if (have_quota_entry) {
3245
3246 /* Assume that quotatab_lookup() does the reading in of the necessary
3247 * data from the quota source table as well.
3248 */
3249
3250 /* If per-session quotas are in effect, zero the tally record now */
3251 if (sess_limit.quota_per_session) {
3252 sess_tally.bytes_in_used = 0.0F;
3253 sess_tally.bytes_out_used = 0.0F;
3254 sess_tally.bytes_xfer_used = 0.0F;
3255 sess_tally.files_in_used = 0U;
3256 sess_tally.files_out_used = 0U;
3257 sess_tally.files_xfer_used = 0U;
3258
3259 quotatab_log("per session setting in effect: updates will not be "
3260 "tracked in the QuotaTallyTable");
3261 }
3262
3263 /* If the limit for this user is a hard limit, install our own FS handlers,
3264 * which provide custom read() and write() functions. We will use them to
3265 * return an error when reading/writing a file causes a limit to be reached.
3266 */
3267 if (sess_limit.quota_limit_type == HARD_LIMIT) {
3268 pr_fs_t *fs = NULL;
3269
3270 fs = pr_register_fs(session.pool, "quotatab", "/");
3271 if (fs != NULL) {
3272 quotatab_log("quotatab fs registered");
3273
3274 /* Use all of the current FS handlers, EXCEPT for its write(2)
3275 * callback. That operation is the one we want to handle, in order
3276 * to force a write failure if a quota is exceeded.
3277 */
3278
3279 /* TODO: How to wrap the existing FS write/pwrite callbacks?
3280 *
3281 * For Issue #1764, this is not a problem due to the fact that
3282 * mod_vroot does not override the system FS write callbacks.
3283 */
3284 fs->pwrite = quotatab_fsio_pwrite;
3285 fs->write = quotatab_fsio_write;
3286
3287 } else {
3288 quotatab_log("error registering quotatab fs: %s", strerror(errno));
3289 }
3290 }
3291
3292 } else {
3293 /* If we didn't even find a limit entry, then make sure that we
3294 * release the QuotaLock.
3295 */
3296 if (!have_limit_entry) {
3297 quotatab_mutex_lock(F_UNLCK);
3298 }
3299
3300 /* No quota entry for this user. Make sure the quotas are not used. */
3301 quotatab_log("no quota entry found, turning QuotaEngine off");
3302 use_quotas = FALSE;
3303 }
3304
3305 /* Register some Variable entries, for Display files. */
3306 if (pr_var_set(quotatab_pool, "%{mod_quotatab.limit.bytes_in}",
3307 "Maximum number of uploadable bytes", PR_VAR_TYPE_FUNC,
3308 quota_get_bytes_str, &(sess_limit.bytes_in_avail),
3309 sizeof(double *)) < 0)
3310 quotatab_log("error setting %%{mod_quotatab.limit.bytes_in} variable: %s",
3311 strerror(errno));
3312
3313 if (pr_var_set(quotatab_pool, "%{mod_quotatab.limit.bytes_out}",
3314 "Maximum number of downloadable bytes", PR_VAR_TYPE_FUNC,
3315 quota_get_bytes_str, &(sess_limit.bytes_out_avail),
3316 sizeof(double *)) < 0)
3317 quotatab_log("error setting %%{mod_quotatab.limit.bytes_out} variable: %s",
3318 strerror(errno));
3319
3320 if (pr_var_set(quotatab_pool, "%{mod_quotatab.limit.bytes_xfer}",
3321 "Maximum number of transferble bytes", PR_VAR_TYPE_FUNC,
3322 quota_get_bytes_str, &(sess_limit.bytes_xfer_avail),
3323 sizeof(double *)) < 0)
3324 quotatab_log("error setting %%{mod_quotatab.limit.bytes_xfer} variable: %s",
3325 strerror(errno));
3326
3327 if (pr_var_set(quotatab_pool, "%{mod_quotatab.limit.files_in}",
3328 "Maximum number of uploadable files", PR_VAR_TYPE_FUNC,
3329 quota_get_files_str, &(sess_limit.files_in_avail),
3330 sizeof(unsigned int *)) < 0)
3331 quotatab_log("error setting %%{mod_quotatab.limit.files_in} variable: %s",
3332 strerror(errno));
3333
3334 if (pr_var_set(quotatab_pool, "%{mod_quotatab.limit.files_out}",
3335 "Maximum number of downloadable files", PR_VAR_TYPE_FUNC,
3336 quota_get_files_str, &(sess_limit.files_out_avail),
3337 sizeof(unsigned int *)) < 0)
3338 quotatab_log("error setting %%{mod_quotatab.limit.files_out} variable: %s",
3339 strerror(errno));
3340
3341 if (pr_var_set(quotatab_pool, "%{mod_quotatab.limit.files_xfer}",
3342 "Maximum number of transferable files", PR_VAR_TYPE_FUNC,
3343 quota_get_files_str, &(sess_limit.files_xfer_avail),
3344 sizeof(unsigned int *)) < 0)
3345 quotatab_log("error setting %%{mod_quotatab.limit.files_xfer} variable: %s",
3346 strerror(errno));
3347
3348 if (pr_var_set(quotatab_pool, "%{mod_quotatab.tally.bytes_in}",
3349 "Current number of uploaded bytes", PR_VAR_TYPE_FUNC,
3350 quota_get_bytes_str, &(sess_tally.bytes_in_used),
3351 sizeof(double *)) < 0)
3352 quotatab_log("error setting %%{mod_quotatab.tally.bytes_in} variable: %s",
3353 strerror(errno));
3354
3355 if (pr_var_set(quotatab_pool, "%{mod_quotatab.tally.bytes_out}",
3356 "Current number of downloaded bytes", PR_VAR_TYPE_FUNC,
3357 quota_get_bytes_str, &(sess_tally.bytes_out_used),
3358 sizeof(double *)) < 0)
3359 quotatab_log("error setting %%{mod_quotatab.limit.bytes_out} variable: %s",
3360 strerror(errno));
3361
3362 if (pr_var_set(quotatab_pool, "%{mod_quotatab.tally.bytes_xfer}",
3363 "Current number of transferred bytes", PR_VAR_TYPE_FUNC,
3364 quota_get_bytes_str, &(sess_tally.bytes_xfer_used),
3365 sizeof(double *)) < 0)
3366 quotatab_log("error setting %%{mod_quotatab.tally.bytes_xfer} variable: %s",
3367 strerror(errno));
3368
3369 if (pr_var_set(quotatab_pool, "%{mod_quotatab.tally.files_in}",
3370 "Current number of uploaded files", PR_VAR_TYPE_FUNC,
3371 quota_get_files_str, &(sess_tally.files_in_used),
3372 sizeof(unsigned int *)) < 0)
3373 quotatab_log("error setting %%{mod_quotatab.tally.files_in} variable: %s",
3374 strerror(errno));
3375
3376 if (pr_var_set(quotatab_pool, "%{mod_quotatab.tally.files_out}",
3377 "Current number of downloaded files", PR_VAR_TYPE_FUNC,
3378 quota_get_files_str, &(sess_tally.files_out_used),
3379 sizeof(unsigned int *)) < 0)
3380 quotatab_log("error setting %%{mod_quotatab.tally.files_out} variable: %s",
3381 strerror(errno));
3382
3383 if (pr_var_set(quotatab_pool, "%{mod_quotatab.tally.files_xfer}",
3384 "Current number of transferred files", PR_VAR_TYPE_FUNC,
3385 quota_get_files_str, &(sess_tally.files_xfer_used),
3386 sizeof(unsigned int *)) < 0)
3387 quotatab_log("error setting %%{mod_quotatab.tally.files_xfer} variable: %s",
3388 strerror(errno));
3389
3390 return PR_DECLINED(cmd);
3391 }
3392
3393 MODRET quotatab_pre_retr(cmd_rec *cmd) {
3394 have_aborted_transfer = FALSE;
3395 have_err_response = FALSE;
3396
3397 /* Sanity check */
3398 if (!use_quotas)
3399 return PR_DECLINED(cmd);
3400
3401 if (quotatab_ignore_path(cmd->tmp_pool, cmd->arg)) {
3402 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
3403 (char *) cmd->argv[0], cmd->arg, quota_exclude_filter);
3404 return PR_DECLINED(cmd);
3405 }
3406
3407 /* Refresh the tally */
3408 QUOTATAB_TALLY_READ
3409
3410 /* Check quotas to see if bytes download or total quota has been reached.
3411 * Block command if so.
3412 */
3413 if (sess_limit.bytes_out_avail > 0.0 &&
3414 sess_tally.bytes_out_used >= sess_limit.bytes_out_avail) {
3415
3416 /* Report the exceeding of the threshold. */
3417 quotatab_log("%s denied: %s quota exceeded: used %s",
3418 get_quota_type_text(sess_quota_type), (char *) cmd->argv[0],
3419 DISPLAY_BYTES_OUT(cmd));
3420 pr_response_add_err(R_451, _("%s denied: quota exceeded: used %s"),
3421 (char *) cmd->argv[0], DISPLAY_BYTES_OUT(cmd));
3422 have_err_response = TRUE;
3423
3424 /* Set an appropriate errno value. */
3425 errno = get_quota_exceeded_errno(EPERM, NULL);
3426
3427 return PR_ERROR(cmd);
3428
3429 } else if (sess_limit.bytes_xfer_avail > 0.0 &&
3430 sess_tally.bytes_xfer_used >= sess_limit.bytes_xfer_avail) {
3431
3432 /* Report the exceeding of the threshold. */
3433 quotatab_log("%s denied: %s quota exceeded: used %s",
3434 get_quota_type_text(sess_quota_type), (char *) cmd->argv[0],
3435 DISPLAY_BYTES_XFER(cmd));
3436 pr_response_add_err(R_451, _("%s denied: quota exceeded: used %s"),
3437 (char *) cmd->argv[0], DISPLAY_BYTES_XFER(cmd));
3438 have_err_response = TRUE;
3439
3440 /* Set an appropriate errno value. */
3441 errno = get_quota_exceeded_errno(EPERM, NULL);
3442
3443 return PR_ERROR(cmd);
3444 }
3445
3446 /* Check quotas to see if files download or total quota has been reached.
3447 * Block command if so.
3448 */
3449 if (sess_limit.files_out_avail != 0 &&
3450 sess_tally.files_out_used >= sess_limit.files_out_avail) {
3451
3452 /* Report the exceeding of the threshold. */
3453 quotatab_log("%s denied: %s quota exceeded: used %s",
3454 get_quota_type_text(sess_quota_type), (char *) cmd->argv[0],
3455 DISPLAY_FILES_OUT(cmd));
3456 pr_response_add_err(R_451, _("%s denied: quota exceeded: used %s"),
3457 (char *) cmd->argv[0], DISPLAY_FILES_OUT(cmd));
3458 have_err_response = TRUE;
3459
3460 /* Set an appropriate errno value. */
3461 errno = get_quota_exceeded_errno(EPERM, NULL);
3462
3463 return PR_ERROR(cmd);
3464
3465 } else if (sess_limit.files_xfer_avail != 0 &&
3466 sess_tally.files_xfer_used >= sess_limit.files_xfer_avail) {
3467
3468 /* Report the exceeding of the threshold. */
3469 quotatab_log("%s: denied: %s quota exceeded: used %s",
3470 get_quota_type_text(sess_quota_type), (char *) cmd->argv[0],
3471 DISPLAY_FILES_XFER(cmd));
3472 pr_response_add(R_451, _("%s denied: quota exceeded: used %s"),
3473 (char *) cmd->argv[0], DISPLAY_FILES_XFER(cmd));
3474 have_err_response = TRUE;
3475
3476 /* Set an appropriate errno value. */
3477 errno = get_quota_exceeded_errno(EPERM, NULL);
3478
3479 return PR_ERROR(cmd);
3480 }
3481
3482 have_quota_update = QUOTA_HAVE_READ_UPDATE;
3483 return PR_DECLINED(cmd);
3484 }
3485
3486 MODRET quotatab_post_retr(cmd_rec *cmd) {
3487
3488 /* Sanity check */
3489 if (!use_quotas)
3490 return PR_DECLINED(cmd);
3491
3492 if (quotatab_ignore_path(cmd->tmp_pool, cmd->arg)) {
3493 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
3494 (char *) cmd->argv[0], cmd->arg, quota_exclude_filter);
3495 return PR_DECLINED(cmd);
3496 }
3497
3498 /* Write out an updated tally */
3499 QUOTATAB_TALLY_WRITE(0, session.xfer.total_bytes, session.xfer.total_bytes,
3500 0, 1, 1)
3501
3502 /* Check quotas to see if bytes download or total quota has been reached.
3503 * Report this to user if so.
3504 */
3505 if (sess_limit.bytes_out_avail > 0.0 &&
3506 sess_tally.bytes_out_used >= sess_limit.bytes_out_avail) {
3507
3508 /* Report the reaching of the threshold. */
3509 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
3510 DISPLAY_BYTES_OUT(cmd));
3511 pr_response_add(R_DUP, _("%s: notice: quota reached: used %s"),
3512 (char *) cmd->argv[0], DISPLAY_BYTES_OUT(cmd));
3513
3514 } else if (sess_limit.bytes_xfer_avail > 0.0 &&
3515 sess_tally.bytes_xfer_used >= sess_limit.bytes_xfer_avail) {
3516
3517 /* Report the reaching of the threshold. */
3518 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
3519 DISPLAY_BYTES_XFER(cmd));
3520 pr_response_add(R_DUP, _("%s: notice: quota reached: used %s"),
3521 (char *) cmd->argv[0], DISPLAY_BYTES_XFER(cmd));
3522 }
3523
3524 /* Check quotas to see if files download or total quota has been reached.
3525 * Report this to user if so.
3526 */
3527 if (sess_limit.files_out_avail != 0 &&
3528 sess_tally.files_out_used >= sess_limit.files_out_avail) {
3529
3530 /* Report the reaching of the threshold. */
3531 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
3532 DISPLAY_FILES_OUT(cmd));
3533 pr_response_add(R_DUP, _("%s: notice: quota reached: used %s"),
3534 (char *) cmd->argv[0], DISPLAY_FILES_OUT(cmd));
3535
3536 } else if (sess_limit.files_xfer_avail != 0 &&
3537 sess_tally.files_xfer_used >= sess_limit.files_xfer_avail) {
3538
3539 /* Report the reaching of the threshold. */
3540 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
3541 DISPLAY_FILES_XFER(cmd));
3542 pr_response_add(R_DUP, _("%s: notice: quota reached: used %s"),
3543 (char *) cmd->argv[0], DISPLAY_FILES_XFER(cmd));
3544 }
3545
3546 return PR_DECLINED(cmd);
3547 }
3548
3549 MODRET quotatab_post_retr_err(cmd_rec *cmd) {
3550
3551 /* Sanity check */
3552 if (!use_quotas)
3553 return PR_DECLINED(cmd);
3554
3555 if (quotatab_ignore_path(cmd->tmp_pool, cmd->arg)) {
3556 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
3557 (char *) cmd->argv[0], cmd->arg, quota_exclude_filter);
3558 return PR_DECLINED(cmd);
3559 }
3560
3561 /* Write out an updated tally */
3562 QUOTATAB_TALLY_WRITE(0, session.xfer.total_bytes, session.xfer.total_bytes,
3563 0, 0, 0)
3564
3565 /* Check quotas to see if bytes download or total quota has been reached.
3566 * Report this to user if so (if not already reported).
3567 */
3568 if (sess_limit.bytes_out_avail > 0.0 &&
3569 sess_tally.bytes_out_used >= sess_limit.bytes_out_avail) {
3570
3571 if (!have_err_response) {
3572
3573 /* Report the reaching of the threshold. */
3574 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
3575 DISPLAY_BYTES_OUT(cmd));
3576 pr_response_add_err(R_DUP, _("%s: notice: quota reached: used %s"),
3577 (char *) cmd->argv[0], DISPLAY_BYTES_OUT(cmd));
3578 }
3579
3580 } else if (sess_limit.bytes_xfer_avail > 0.0 &&
3581 sess_tally.bytes_xfer_used >= sess_limit.bytes_xfer_avail) {
3582
3583 if (!have_err_response) {
3584
3585 /* Report the reaching of the threshold. */
3586 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
3587 DISPLAY_BYTES_XFER(cmd));
3588 pr_response_add_err(R_DUP, _("%s: notice: quota reached: used %s"),
3589 (char *) cmd->argv[0], DISPLAY_BYTES_XFER(cmd));
3590 }
3591 }
3592
3593 /* Check quotas to see if files download or total quota has been reached.
3594 * Report this to user if so (if not already reported).
3595 */
3596 if (sess_limit.files_out_avail != 0 &&
3597 sess_tally.files_out_used >= sess_limit.files_out_avail) {
3598
3599 if (!have_err_response) {
3600 /* Report the reaching of the threshold. */
3601 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
3602 DISPLAY_FILES_OUT(cmd));
3603 pr_response_add_err(R_DUP, _("%s: notice: quota reached: used %s"),
3604 (char *) cmd->argv[0], DISPLAY_FILES_OUT(cmd));
3605 }
3606
3607 } else if (sess_limit.files_xfer_avail != 0 &&
3608 sess_tally.files_xfer_used >= sess_limit.files_xfer_avail) {
3609
3610 if (!have_err_response) {
3611 /* Report the reaching of the threshold. */
3612 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
3613 DISPLAY_FILES_XFER(cmd));
3614 pr_response_add_err(R_DUP, _("%s: notice: quota reached: used %s"),
3615 (char *) cmd->argv[0], DISPLAY_FILES_XFER(cmd));
3616 }
3617 }
3618
3619 have_err_response = FALSE;
3620 return PR_DECLINED(cmd);
3621 }
3622
3623 MODRET quotatab_pre_rmd(cmd_rec *cmd) {
3624 struct stat st;
3625
3626 /* Sanity check. */
3627 if (!use_quotas || !use_dirs)
3628 return PR_DECLINED(cmd);
3629
3630 /* Briefly cache the size (in bytes) of the directory to be deleted, so that
3631 * if successful, the byte counts can be adjusted correctly.
3632 */
3633 pr_fs_clear_cache2(cmd->arg);
3634 if (pr_fsio_lstat(cmd->arg, &st) < 0) {
3635 quotatab_disk_nbytes = 0;
3636
3637 } else {
3638 quotatab_disk_nbytes = st.st_size;
3639 }
3640
3641 return PR_DECLINED(cmd);
3642 }
3643
3644 MODRET quotatab_post_rmd(cmd_rec *cmd) {
3645
3646 /* Sanity check. */
3647 if (!use_quotas || !use_dirs)
3648 return PR_DECLINED(cmd);
3649
3650 if (quotatab_ignore_path(cmd->tmp_pool, cmd->arg)) {
3651 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
3652 (char *) cmd->argv[0], cmd->arg, quota_exclude_filter);
3653 return PR_DECLINED(cmd);
3654 }
3655
3656 /* Write out an updated quota entry. */
3657 QUOTATAB_TALLY_WRITE(-quotatab_disk_nbytes, 0, 0, -1, 0, -1)
3658
3659 /* Clear the cached bytes. */
3660 quotatab_disk_nbytes = 0;
3661
3662 return PR_DECLINED(cmd);
3663 }
3664
3665 MODRET quotatab_pre_rnto(cmd_rec *cmd) {
3666 struct stat st;
3667
3668 /* Sanity check */
3669 if (!use_quotas)
3670 return PR_DECLINED(cmd);
3671
3672 if (quotatab_ignore_path(cmd->tmp_pool, cmd->arg)) {
3673 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
3674 (char *) cmd->argv[0], cmd->arg, quota_exclude_filter);
3675 return PR_DECLINED(cmd);
3676 }
3677
3678 /* Briefly cache the size (in bytes) of the file being overwritten, so that
3679 * if successful, the byte counts can be adjusted correctly.
3680 */
3681 pr_fs_clear_cache2(cmd->arg);
3682 if (pr_fsio_lstat(cmd->arg, &st) < 0) {
3683 quotatab_disk_nbytes = 0;
3684 quotatab_disk_nfiles = 0;
3685
3686 } else {
3687 quotatab_disk_nbytes = st.st_size;
3688 quotatab_disk_nfiles = 1;
3689 }
3690
3691 return PR_DECLINED(cmd);
3692 }
3693
3694 MODRET quotatab_post_rnto(cmd_rec *cmd) {
3695
3696 /* Sanity check */
3697 if (!use_quotas)
3698 return PR_DECLINED(cmd);
3699
3700 if (quotatab_ignore_path(cmd->tmp_pool, cmd->arg)) {
3701 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
3702 (char *) cmd->argv[0], cmd->arg, quota_exclude_filter);
3703 return PR_DECLINED(cmd);
3704 }
3705
3706 /* Write out an updated quota entry. */
3707 QUOTATAB_TALLY_WRITE(-quotatab_disk_nbytes, 0, -quotatab_disk_nbytes,
3708 -quotatab_disk_nfiles, 0, -quotatab_disk_nfiles)
3709
3710 /* Clear the cached bytes/files. */
3711 quotatab_disk_nbytes = 0;
3712 quotatab_disk_nfiles = 0;
3713
3714 return PR_DECLINED(cmd);
3715 }
3716
3717 MODRET quotatab_pre_stor(cmd_rec *cmd) {
3718 struct stat st;
3719
3720 have_aborted_transfer = FALSE;
3721 have_err_response = FALSE;
3722
3723 /* Sanity check */
3724 if (use_quotas == FALSE) {
3725 return PR_DECLINED(cmd);
3726 }
3727
3728 if (quotatab_ignore_path(cmd->tmp_pool, cmd->arg)) {
3729 have_quota_update = 0;
3730
3731 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
3732 (char *) cmd->argv[0], cmd->arg, quota_exclude_filter);
3733 return PR_DECLINED(cmd);
3734 }
3735
3736 /* Refresh the tally */
3737 QUOTATAB_TALLY_READ
3738
3739 /* Check quotas to see if bytes upload or total quota has been reached.
3740 * Block command if so.
3741 */
3742 if (sess_limit.bytes_in_avail > 0.0 &&
3743 sess_tally.bytes_in_used >= sess_limit.bytes_in_avail) {
3744
3745 /* Report the exceeding of the threshold. */
3746 quotatab_log("%s denied: %s quota exceeded: used %s",
3747 get_quota_type_text(sess_quota_type), (char *) cmd->argv[0],
3748 DISPLAY_BYTES_IN(cmd));
3749 pr_response_add_err(R_552, _("%s denied: quota exceeded: used %s"),
3750 (char *) cmd->argv[0], DISPLAY_BYTES_IN(cmd));
3751 have_err_response = TRUE;
3752
3753 /* Set an appropriate errno value. */
3754 errno = get_quota_exceeded_errno(EPERM, NULL);
3755
3756 return PR_ERROR(cmd);
3757
3758 } else if (sess_limit.bytes_xfer_avail > 0.0 &&
3759 sess_tally.bytes_xfer_used >= sess_limit.bytes_xfer_avail) {
3760
3761 /* Report the exceeding of the threshold. */
3762 quotatab_log("%s denied: %s quota exceeded: used %s",
3763 get_quota_type_text(sess_quota_type), (char *) cmd->argv[0],
3764 DISPLAY_BYTES_XFER(cmd));
3765 pr_response_add_err(R_552, _("%s denied: quota exceeded: used %s"),
3766 (char *) cmd->argv[0], DISPLAY_BYTES_XFER(cmd));
3767 have_err_response = TRUE;
3768
3769 /* Set an appropriate errno value. */
3770 errno = get_quota_exceeded_errno(EPERM, NULL);
3771
3772 return PR_ERROR(cmd);
3773 }
3774
3775 /* Check quotas to see if files upload or total quota has been reached.
3776 * Block the command if so.
3777 */
3778 if (sess_limit.files_in_avail != 0 &&
3779 sess_tally.files_in_used >= sess_limit.files_in_avail) {
3780
3781 /* Report the exceeding of the threshold. */
3782 quotatab_log("%s denied: %s quota exceeded: used %s",
3783 get_quota_type_text(sess_quota_type), (char *) cmd->argv[0],
3784 DISPLAY_FILES_IN(cmd));
3785 pr_response_add_err(R_552, _("%s denied: quota exceeded: used %s"),
3786 (char *) cmd->argv[0], DISPLAY_FILES_IN(cmd));
3787 have_err_response = TRUE;
3788
3789 /* Set an appropriate errno value. */
3790 errno = get_quota_exceeded_errno(EPERM, NULL);
3791
3792 return PR_ERROR(cmd);
3793
3794 } else if (sess_limit.files_xfer_avail != 0 &&
3795 sess_tally.files_xfer_used >= sess_limit.files_xfer_avail) {
3796
3797 /* Report the exceeding of the threshold. */
3798 quotatab_log("%s denied: %s quota exceeded: used %s",
3799 get_quota_type_text(sess_quota_type), (char *) cmd->argv[0],
3800 DISPLAY_FILES_XFER(cmd));
3801 pr_response_add_err(R_552, _("%s denied: quota exceeded: used %s"),
3802 (char *) cmd->argv[0], DISPLAY_FILES_XFER(cmd));
3803 have_err_response = TRUE;
3804
3805 /* Set an appropriate errno value. */
3806 errno = get_quota_exceeded_errno(EPERM, NULL);
3807
3808 return PR_ERROR(cmd);
3809 }
3810
3811 /* Briefly cache the size (in bytes) of the file being appended to, so that
3812 * if successful, the byte counts can be adjusted correctly. If the
3813 * stat fails, it means that a new file is being uploaded, so set the
3814 * disk_nbytes to be zero.
3815 */
3816 pr_fs_clear_cache2(cmd->arg);
3817 if (pr_fsio_lstat(cmd->arg, &st) < 0) {
3818 quotatab_disk_nbytes = 0;
3819
3820 } else {
3821 quotatab_disk_nbytes = st.st_size;
3822 }
3823
3824 have_quota_update = QUOTA_HAVE_WRITE_UPDATE;
3825 return PR_DECLINED(cmd);
3826 }
3827
3828 MODRET quotatab_post_stor(cmd_rec *cmd) {
3829 struct stat st;
3830 off_t store_bytes = session.xfer.total_bytes;
3831
3832 /* Sanity check */
3833 if (!use_quotas) {
3834 have_quota_update = 0;
3835 return PR_DECLINED(cmd);
3836 }
3837
3838 if (quotatab_ignore_path(cmd->tmp_pool, cmd->arg)) {
3839 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
3840 (char *) cmd->argv[0], cmd->arg, quota_exclude_filter);
3841 have_quota_update = 0;
3842 return PR_DECLINED(cmd);
3843 }
3844
3845 /* If the transfer was aborted, AND if DeleteAbortedStores is on, then
3846 * don't update the tally (Bug#3621).
3847 */
3848 if (have_aborted_transfer ||
3849 (session.sf_flags & (SF_ABORT|SF_POST_ABORT))) {
3850 unsigned char *delete_stores;
3851
3852 delete_stores = get_param_ptr(CURRENT_CONF, "DeleteAbortedStores", FALSE);
3853 if (delete_stores != NULL &&
3854 *delete_stores == TRUE) {
3855 quotatab_log("%s: upload aborted and DeleteAbortedStores on, "
3856 "skipping tally update", (char *) cmd->argv[0]);
3857 have_quota_update = 0;
3858 return PR_DECLINED(cmd);
3859 }
3860 }
3861
3862 /* Check on the size of the stored file again, and use the difference
3863 * in file size as the increment. Make sure that no caching effects
3864 * mess with the stat.
3865 */
3866 pr_fs_clear_cache2(cmd->arg);
3867 if (pr_fsio_lstat(cmd->arg, &st) >= 0) {
3868 store_bytes = st.st_size - quotatab_disk_nbytes;
3869
3870 } else {
3871 if (errno == ENOENT) {
3872 store_bytes = 0;
3873
3874 } else {
3875 quotatab_log("%s: error checking '%s': %s", (char *) cmd->argv[0],
3876 cmd->arg, strerror(errno));
3877 }
3878 }
3879
3880 /* NOTE: if use_dirs is TRUE, also take into consideration the increased
3881 * disk usage caused by any increase in the size of the containing directory.
3882 */
3883
3884 /* Write out an updated quota entry */
3885 QUOTATAB_TALLY_WRITE(store_bytes, 0, session.xfer.total_bytes,
3886 quotatab_disk_nbytes ? 0 : 1, 0, 1)
3887
3888 /* Check quotas to see if bytes upload or total quota has been reached.
3889 * Report this to user if so. If it is a hard bytes limit, delete the
3890 * uploaded file.
3891 */
3892 if (sess_limit.bytes_in_avail > 0.0 &&
3893 sess_tally.bytes_in_used >= sess_limit.bytes_in_avail) {
3894
3895 if (!have_err_response) {
3896 /* Report the reaching of the threshold. */
3897 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
3898 DISPLAY_BYTES_IN(cmd));
3899 pr_response_add(R_DUP, _("%s: notice: quota reached: used %s"),
3900 (char *) cmd->argv[0], DISPLAY_BYTES_IN(cmd));
3901 }
3902
3903 if (sess_tally.bytes_in_used > sess_limit.bytes_in_avail &&
3904 sess_limit.quota_limit_type == HARD_LIMIT) {
3905 int res;
3906
3907 res = pr_fsio_unlink(cmd->arg);
3908 if (res < 0 &&
3909 errno == EISDIR &&
3910 use_dirs == TRUE) {
3911 res = pr_fsio_rmdir(cmd->arg);
3912 }
3913
3914 if (res < 0) {
3915 quotatab_log("notice: unable to unlink '%s': %s", cmd->arg,
3916 strerror(errno));
3917
3918 } else {
3919 QUOTATAB_TALLY_WRITE(-store_bytes, 0, -session.xfer.total_bytes,
3920 -1, 0, -1);
3921
3922 /* Report the removal of the file. */
3923 quotatab_log("%s: quota reached: '%s' removed", (char *) cmd->argv[0],
3924 cmd->arg);
3925 pr_response_add(R_DUP, _("%s: notice: quota reached: '%s' removed"),
3926 (char *) cmd->argv[0], cmd->arg);
3927 }
3928 }
3929
3930 } else if (sess_limit.bytes_xfer_avail > 0.0 &&
3931 sess_tally.bytes_xfer_used >= sess_limit.bytes_xfer_avail) {
3932
3933 if (!have_err_response) {
3934 /* Report the reaching of the threshold. */
3935 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
3936 DISPLAY_BYTES_XFER(cmd));
3937 pr_response_add(R_DUP, _("%s: notice: quota reached: used %s"),
3938 (char *) cmd->argv[0], DISPLAY_BYTES_XFER(cmd));
3939 }
3940
3941 if (sess_tally.bytes_xfer_used > sess_limit.bytes_xfer_avail &&
3942 sess_limit.quota_limit_type == HARD_LIMIT) {
3943 int res;
3944
3945 res = pr_fsio_unlink(cmd->arg);
3946 if (res < 0 &&
3947 errno == EISDIR &&
3948 use_dirs == TRUE) {
3949 res = pr_fsio_rmdir(cmd->arg);
3950 }
3951
3952 if (res < 0) {
3953 quotatab_log("notice: unable to unlink '%s': %s", cmd->arg,
3954 strerror(errno));
3955
3956 } else {
3957 QUOTATAB_TALLY_WRITE(-store_bytes, 0, -session.xfer.total_bytes,
3958 -1, 0, -1);
3959
3960 /* Report the removal of the file. */
3961 quotatab_log("%s: quota reached: '%s' removed", (char *) cmd->argv[0],
3962 cmd->arg);
3963 pr_response_add(R_DUP, _("%s: notice: quota reached: '%s' removed"),
3964 (char *) cmd->argv[0], cmd->arg);
3965 }
3966 }
3967 }
3968
3969 /* Check quotas to see if files upload or total quota has been reached.
3970 * Report this to user if so.
3971 */
3972 if (sess_limit.files_in_avail != 0 &&
3973 sess_tally.files_in_used >= sess_limit.files_in_avail) {
3974
3975 if (!have_err_response) {
3976 /* Report the reaching of the threshold. */
3977 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
3978 DISPLAY_FILES_IN(cmd));
3979 pr_response_add(R_DUP, _("%s: notice: quota reached: used %s"),
3980 (char *) cmd->argv[0], DISPLAY_FILES_IN(cmd));
3981 }
3982
3983 } else if (sess_limit.files_xfer_avail != 0 &&
3984 sess_tally.files_xfer_used >= sess_limit.files_xfer_avail) {
3985
3986 if (!have_err_response) {
3987 /* Report the reaching of the threshold. */
3988 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
3989 DISPLAY_FILES_XFER(cmd));
3990 pr_response_add(R_DUP, _("%s: notice: quota reached: used %s"),
3991 (char *) cmd->argv[0], DISPLAY_FILES_XFER(cmd));
3992 }
3993 }
3994
3995 have_quota_update = 0;
3996 return PR_DECLINED(cmd);
3997 }
3998
3999 MODRET quotatab_post_stor_err(cmd_rec *cmd) {
4000 struct stat st;
4001 off_t store_bytes = session.xfer.total_bytes;
4002
4003 /* Sanity check */
4004 if (!use_quotas) {
4005 have_quota_update = 0;
4006 return PR_DECLINED(cmd);
4007 }
4008
4009 if (quotatab_ignore_path(cmd->tmp_pool, cmd->arg)) {
4010 quotatab_log("%s: path '%s' matched QuotaExcludeFilter '%s', ignoring",
4011 (char *) cmd->argv[0], cmd->arg, quota_exclude_filter);
4012 have_quota_update = 0;
4013 return PR_DECLINED(cmd);
4014 }
4015
4016 /* If the transfer was aborted, AND if DeleteAbortedStores is on, then
4017 * don't update the tally (Bug#3621).
4018 */
4019 if (have_aborted_transfer ||
4020 (session.sf_flags & (SF_ABORT|SF_POST_ABORT))) {
4021 unsigned char *delete_stores;
4022
4023 delete_stores = get_param_ptr(CURRENT_CONF, "DeleteAbortedStores", FALSE);
4024 if (delete_stores != NULL &&
4025 *delete_stores == TRUE) {
4026 quotatab_log("%s: upload aborted and DeleteAbortedStores on, "
4027 "skipping tally update", (char *) cmd->argv[0]);
4028 have_quota_update = 0;
4029 return PR_DECLINED(cmd);
4030 }
4031 }
4032
4033 if (store_bytes > 0) {
4034 /* Check on the size of the stored file again, and use the difference
4035 * in file size as the increment. Make sure that no caching effects
4036 * mess with the stat.
4037 */
4038 pr_fs_clear_cache2(cmd->arg);
4039 if (pr_fsio_lstat(cmd->arg, &st) >= 0) {
4040 store_bytes = st.st_size - quotatab_disk_nbytes;
4041
4042 } else {
4043 if (errno == ENOENT) {
4044 store_bytes = 0;
4045
4046 } else {
4047 quotatab_log("%s: error checking '%s': %s", (char *) cmd->argv[0],
4048 cmd->arg, strerror(errno));
4049 }
4050 }
4051 }
4052
4053 if (store_bytes != 0 ||
4054 session.xfer.total_bytes != 0) {
4055
4056 /* Write out an updated quota entry, but only if we have differences
4057 * to write out.
4058 */
4059 QUOTATAB_TALLY_WRITE(store_bytes, 0, session.xfer.total_bytes, 0, 0, 0)
4060 }
4061
4062 /* Check quotas to see if bytes upload or total quota has been reached.
4063 * Report this to user if so (if not already reported). If it is a hard
4064 * bytes limit, delete the uploaded file.
4065 */
4066 if (sess_limit.bytes_in_avail > 0.0 &&
4067 sess_tally.bytes_in_used >= sess_limit.bytes_in_avail) {
4068
4069 if (!have_err_response) {
4070
4071 /* Report the reaching of the threshold. */
4072 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
4073 DISPLAY_BYTES_IN(cmd));
4074 pr_response_add_err(R_DUP, _("%s: notice: quota reached: used %s"),
4075 (char *) cmd->argv[0], DISPLAY_BYTES_IN(cmd));
4076 }
4077
4078 if (sess_tally.bytes_in_used > sess_limit.bytes_in_avail) {
4079 if (sess_limit.quota_limit_type == HARD_LIMIT) {
4080 int res;
4081
4082 res = pr_fsio_unlink(cmd->arg);
4083 if (res < 0 &&
4084 errno == EISDIR &&
4085 use_dirs == TRUE) {
4086 res = pr_fsio_rmdir(cmd->arg);
4087 }
4088
4089 if (res < 0) {
4090 quotatab_log("notice: unable to unlink '%s': %s", cmd->arg,
4091 strerror(errno));
4092
4093 } else {
4094
4095 /* Report the removal of the file. */
4096 quotatab_log("%s: quota reached: '%s' removed", (char *) cmd->argv[0],
4097 cmd->arg);
4098 pr_response_add_err(R_DUP,
4099 _("%s: notice: quota reached: '%s' removed"), (char *) cmd->argv[0],
4100 cmd->arg);
4101 }
4102 }
4103
4104 QUOTATAB_TALLY_WRITE(-store_bytes, 0, -session.xfer.total_bytes, 0, 0, 0);
4105 }
4106
4107 } else if (sess_limit.bytes_xfer_avail > 0.0 &&
4108 sess_tally.bytes_xfer_used >= sess_limit.bytes_xfer_avail) {
4109
4110 if (!have_err_response) {
4111
4112 /* Report the reaching of the threshold. */
4113 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
4114 DISPLAY_BYTES_XFER(cmd));
4115 pr_response_add_err(R_DUP, _("%s: notice: quota reached: used %s"),
4116 (char *) cmd->argv[0], DISPLAY_BYTES_XFER(cmd));
4117 }
4118
4119 if (sess_tally.bytes_xfer_used > sess_limit.bytes_xfer_avail) {
4120 if (sess_limit.quota_limit_type == HARD_LIMIT) {
4121 int res;
4122
4123 res = pr_fsio_unlink(cmd->arg);
4124 if (res < 0 &&
4125 errno == EISDIR &&
4126 use_dirs == TRUE) {
4127 res = pr_fsio_rmdir(cmd->arg);
4128 }
4129
4130 if (res < 0) {
4131 quotatab_log("notice: unable to unlink '%s': %s", cmd->arg,
4132 strerror(errno));
4133
4134 } else {
4135
4136 /* Report the removal of the file. */
4137 quotatab_log("%s: quota reached: '%s' removed", (char *) cmd->argv[0],
4138 cmd->arg);
4139 pr_response_add_err(R_DUP,
4140 _("%s: notice: quota reached: '%s' removed"), (char *) cmd->argv[0],
4141 cmd->arg);
4142 }
4143 }
4144
4145 QUOTATAB_TALLY_WRITE(-store_bytes, 0, -session.xfer.total_bytes, 0, 0, 0);
4146 }
4147 }
4148
4149 /* Check quotas to see if files upload or total quota has been reached.
4150 * Report this to user if so.
4151 */
4152 if (sess_limit.files_in_avail != 0 &&
4153 sess_tally.files_in_used >= sess_limit.files_in_avail) {
4154
4155 /* Report the reaching of the threshold. */
4156 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
4157 DISPLAY_FILES_IN(cmd));
4158 pr_response_add_err(R_DUP, _("%s: notice: quota reached: used %s"),
4159 (char *) cmd->argv[0], DISPLAY_FILES_IN(cmd));
4160
4161 } else if (sess_limit.files_xfer_avail != 0 &&
4162 sess_tally.files_xfer_used >= sess_limit.files_xfer_avail) {
4163
4164 /* Report the reaching of the threshold. */
4165 quotatab_log("%s: quota reached: used %s", (char *) cmd->argv[0],
4166 DISPLAY_FILES_XFER(cmd));
4167 pr_response_add_err(R_DUP, _("%s: notice: quota reached: used %s"),
4168 (char *) cmd->argv[0], DISPLAY_FILES_XFER(cmd));
4169 }
4170
4171 have_quota_update = 0;
4172 return PR_DECLINED(cmd);
4173 }
4174
4175 MODRET quotatab_pre_site(cmd_rec *cmd) {
4176
4177 /* Make sure it's a valid SITE command */
4178 if (cmd->argc < 2) {
4179 return PR_DECLINED(cmd);
4180 }
4181
4182 if (strncasecmp(cmd->argv[1], "COPY", 5) == 0) {
4183 cmd_rec *copy_cmd;
4184
4185 copy_cmd = pr_cmd_alloc(cmd->tmp_pool, 3, cmd->argv[1], cmd->argv[2],
4186 cmd->argv[3]);
4187 return quotatab_pre_copy(copy_cmd);
4188
4189 } else if (strncasecmp(cmd->argv[1], "CPTO", 5) == 0) {
4190 register unsigned int i;
4191 cmd_rec *copy_cmd;
4192 const char *from, *to = "";
4193
4194 if (cmd->argc < 3) {
4195 return PR_DECLINED(cmd);
4196 }
4197
4198 from = pr_table_get(session.notes, "mod_copy.cpfr-path", NULL);
4199 if (from == NULL) {
4200 pr_response_add_err(R_503, _("Bad sequence of commands"));
4201 return PR_ERROR(cmd);
4202 }
4203
4204 /* Construct the target file name by concatenating all the parameters after
4205 * the "SITE CPTO", separating them with spaces.
4206 */
4207 for (i = 2; i <= cmd->argc-1; i++) {
4208 to = pstrcat(cmd->tmp_pool, to, *to ? " " : "",
4209 pr_fs_decode_path(cmd->tmp_pool, cmd->argv[i]), NULL);
4210 }
4211
4212 copy_cmd = pr_cmd_alloc(cmd->tmp_pool, 3, "COPY", from, to);
4213 return quotatab_pre_copy(copy_cmd);
4214 }
4215
4216 return PR_DECLINED(cmd);
4217 }
4218
4219 MODRET quotatab_site(cmd_rec *cmd) {
4220
4221 /* Make sure it's a valid SITE QUOTA command */
4222 if (cmd->argc < 2) {
4223 return PR_DECLINED(cmd);
4224 }
4225
4226 if (strncasecmp(cmd->argv[1], "QUOTA", 6) == 0) {
4227 char *cmd_name;
4228 unsigned char *authenticated = get_param_ptr(cmd->server->conf,
4229 "authenticated", FALSE);
4230
4231 /* The user is required to be authenticated/logged in, first */
4232 if (!authenticated || *authenticated == FALSE) {
4233 pr_response_send(R_530, _("Please login with USER and PASS"));
4234 return PR_ERROR(cmd);
4235 }
4236
4237 /* Is showing of the user's quota barred by configuration? */
4238 if (!allow_site_quota) {
4239 pr_response_add_err(R_500, _("'SITE QUOTA' not understood."));
4240 return PR_ERROR(cmd);
4241 }
4242
4243 /* Check for <Limit> restrictions. */
4244 cmd_name = cmd->argv[0];
4245 pr_cmd_set_name(cmd, "SITE_QUOTA");
4246 if (!dir_check(cmd->tmp_pool, cmd, "NONE", session.cwd, NULL)) {
4247 int xerrno = EPERM;
4248
4249 pr_cmd_set_name(cmd, cmd_name);
4250 pr_response_add_err(R_550, "%s: %s", cmd->arg, strerror(xerrno));
4251
4252 errno = xerrno;
4253 return PR_ERROR(cmd);
4254 }
4255 pr_cmd_set_name(cmd, cmd_name);
4256
4257 /* Log that the user requested their quota. */
4258 quotatab_log("SITE QUOTA requested by user %s", session.user);
4259
4260 /* If quotas are not in use, no need to do anything. */
4261 if (!use_quotas || !have_quota_entry) {
4262 pr_response_add(R_202, _("No quotas in effect"));
4263 return PR_HANDLED(cmd);
4264 }
4265
4266 /* Refresh the tally. */
4267 QUOTATAB_TALLY_READ
4268
4269 pr_response_add(R_200,
4270 _("The current quota for this session are [current/limit]:"));
4271
4272 pr_response_add(R_DUP, _("Name: %s"), sess_limit.name);
4273 pr_response_add(R_DUP, _("Quota Type: %s"),
4274 (sess_limit.quota_type == USER_QUOTA ? _("User") :
4275 sess_limit.quota_type == GROUP_QUOTA ? _("Group") :
4276 sess_limit.quota_type == CLASS_QUOTA ? _("Class") :
4277 sess_limit.quota_type == ALL_QUOTA ? _("All") : _("(unknown)")));
4278
4279 pr_response_add(R_DUP, _("Per Session: %s"),
4280 sess_limit.quota_per_session ? _("True") : _("False"));
4281
4282 pr_response_add(R_DUP, _("Limit Type: %s"),
4283 (sess_limit.quota_limit_type == HARD_LIMIT ? _("Hard") :
4284 sess_limit.quota_limit_type == SOFT_LIMIT ? _("Soft") :
4285 _("(unknown)")));
4286
4287 pr_response_add(R_DUP, _(" Uploaded %s"),
4288 quota_display_site_bytes(cmd->tmp_pool, sess_tally.bytes_in_used,
4289 sess_limit.bytes_in_avail, IN));
4290 pr_response_add(R_DUP, _(" Downloaded %s"),
4291 quota_display_site_bytes(cmd->tmp_pool, sess_tally.bytes_out_used,
4292 sess_limit.bytes_out_avail, OUT));
4293 pr_response_add(R_DUP, _(" Transferred %s"),
4294 quota_display_site_bytes(cmd->tmp_pool, sess_tally.bytes_xfer_used,
4295 sess_limit.bytes_xfer_avail, XFER));
4296
4297 pr_response_add(R_DUP, _(" Uploaded %s"),
4298 quota_display_site_files(cmd->tmp_pool, sess_tally.files_in_used,
4299 sess_limit.files_in_avail, IN));
4300 pr_response_add(R_DUP, _(" Downloaded %s"),
4301 quota_display_site_files(cmd->tmp_pool, sess_tally.files_out_used,
4302 sess_limit.files_out_avail, OUT));
4303 pr_response_add(R_DUP, _(" Transferred %s"),
4304 quota_display_site_files(cmd->tmp_pool, sess_tally.files_xfer_used,
4305 sess_limit.files_xfer_avail, XFER));
4306
4307 /* Add one final line to preserve the spacing. */
4308 pr_response_add(R_DUP,
4309 _("Please contact %s if these entries are inaccurate"),
4310 cmd->server->ServerAdmin ? cmd->server->ServerAdmin : _("ftp-admin"));
4311
4312 return PR_HANDLED(cmd);
4313 }
4314
4315 if (strncasecmp(cmd->argv[1], "HELP", 5) == 0) {
4316
4317 /* Add a description of SITE QUOTA to the output. */
4318 pr_response_add(R_214, "QUOTA");
4319 }
4320
4321 return PR_DECLINED(cmd);
4322 }
4323
4324 MODRET quotatab_post_site(cmd_rec *cmd) {
4325
4326 /* Make sure it's a valid SITE command */
4327 if (cmd->argc < 2)
4328 return PR_DECLINED(cmd);
4329
4330 if (strncasecmp(cmd->argv[1], "COPY", 5) == 0) {
4331 cmd_rec *copy_cmd;
4332
4333 copy_cmd = pr_cmd_alloc(cmd->tmp_pool, 3, cmd->argv[1], cmd->argv[2],
4334 cmd->argv[3]);
4335 return quotatab_post_copy(copy_cmd);
4336
4337 } else if (strncasecmp(cmd->argv[1], "CPTO", 5) == 0) {
4338 register unsigned int i;
4339 cmd_rec *copy_cmd;
4340 const char *from, *to = "";
4341
4342 if (cmd->argc < 3) {
4343 return PR_DECLINED(cmd);
4344 }
4345
4346 from = pr_table_get(session.notes, "mod_copy.cpfr-path", NULL);
4347 if (from == NULL) {
4348 pr_response_add_err(R_503, _("Bad sequence of commands"));
4349 return PR_ERROR(cmd);
4350 }
4351
4352 /* Construct the target file name by concatenating all the parameters after
4353 * the "SITE CPTO", separating them with spaces.
4354 */
4355 for (i = 2; i <= cmd->argc-1; i++) {
4356 to = pstrcat(cmd->tmp_pool, to, *to ? " " : "",
4357 pr_fs_decode_path(cmd->tmp_pool, cmd->argv[i]), NULL);
4358 }
4359
4360 copy_cmd = pr_cmd_alloc(cmd->tmp_pool, 3, "COPY", from, to);
4361 return quotatab_post_copy(copy_cmd);
4362 }
4363
4364 return PR_DECLINED(cmd);
4365 }
4366
4367 MODRET quotatab_post_site_err(cmd_rec *cmd) {
4368
4369 /* Make sure it's a valid SITE command */
4370 if (cmd->argc < 2)
4371 return PR_DECLINED(cmd);
4372
4373 if (strncasecmp(cmd->argv[1], "COPY", 5) == 0) {
4374 cmd_rec *copy_cmd;
4375
4376 copy_cmd = pr_cmd_alloc(cmd->tmp_pool, 3, cmd->argv[1], cmd->argv[2],
4377 cmd->argv[3]);
4378 return quotatab_post_copy_err(copy_cmd);
4379
4380 } else if (strncasecmp(cmd->argv[1], "CPTO", 5) == 0) {
4381 register unsigned int i;
4382 cmd_rec *copy_cmd;
4383 const char *from, *to = "";
4384
4385 from = pr_table_get(session.notes, "mod_copy.cpfr-path", NULL);
4386 if (from == NULL) {
4387 pr_response_add_err(R_503, _("Bad sequence of commands"));
4388 return PR_ERROR(cmd);
4389 }
4390
4391 /* Construct the target file name by concatenating all the parameters after
4392 * the "SITE CPTO", separating them with spaces.
4393 */
4394 for (i = 2; i <= cmd->argc-1; i++) {
4395 to = pstrcat(cmd->tmp_pool, to, *to ? " " : "",
4396 pr_fs_decode_path(cmd->tmp_pool, cmd->argv[i]), NULL);
4397 }
4398
4399 copy_cmd = pr_cmd_alloc(cmd->tmp_pool, 3, "COPY", from, to);
4400 return quotatab_post_copy_err(copy_cmd);
4401 }
4402
4403 return PR_DECLINED(cmd);
4404 }
4405
4406 /* Event handlers
4407 */
4408
4409 static void quotatab_exit_ev(const void *event_data, void *user_data) {
4410
4411 if (have_quota_update) {
4412 /* The session may be ending abruptly, aborted or somesuch in mid-transfer,
4413 * before the change of quota state has been persisted.
4414 *
4415 * Note that it is IMPORTANT that the have_quota_update flag be cleared
4416 * BEFORE writing to the tally table; see Bug#3328.
4417 */
4418
4419 switch (have_quota_update) {
4420 case QUOTA_HAVE_READ_UPDATE:
4421 have_quota_update = 0;
4422 QUOTATAB_TALLY_WRITE(0, session.xfer.total_bytes,
4423 session.xfer.total_bytes, 0, 1, 1)
4424 break;
4425
4426 case QUOTA_HAVE_WRITE_UPDATE:
4427 have_quota_update = 0;
4428 QUOTATAB_TALLY_WRITE(session.xfer.total_bytes, 0,
4429 session.xfer.total_bytes, 1, 0, 1)
4430 break;
4431 }
4432 }
4433
4434 if (use_quotas &&
4435 have_quota_tally_table) {
4436 if (quotatab_close(TYPE_TALLY) < 0)
4437 quotatab_log("error: unable to close QuotaTallyTable: %s",
4438 strerror(errno));
4439 }
4440
4441 quotatab_closelog();
4442 return;
4443 }
4444
4445 #if defined(PR_SHARED_MODULE)
4446 static void quotatab_mod_unload_ev(const void *event_data, void *user_data) {
4447 if (strcmp("mod_quotatab.c", (const char *) event_data) == 0) {
4448 pr_event_unregister(&quotatab_module, NULL, NULL);
4449
4450 if (quota_exclude_pre != NULL) {
4451 pr_regexp_free(NULL, quota_exclude_pre);
4452 quota_exclude_pre = NULL;
4453 }
4454
4455 if (quotatab_pool) {
4456 destroy_pool(quotatab_pool);
4457 quotatab_pool = NULL;
4458 }
4459
4460 quotatab_closelog();
4461 }
4462 }
4463 #endif
4464
4465 static void quotatab_restart_ev(const void *event_data, void *user_data) {
4466
4467 /* Reset the module's memory pool. */
4468 destroy_pool(quotatab_pool);
4469 quotatab_pool = make_sub_pool(permanent_pool);
4470 pr_pool_tag(quotatab_pool, MOD_QUOTATAB_VERSION);
4471
4472 return;
4473 }
4474
4475 static void quotatab_sess_reinit_ev(const void *event_data, void *user_data) {
4476 int res;
4477
4478 /* A HOST command changed the main_server pointer; reinitialize ourselves. */
4479
4480 pr_event_unregister(&quotatab_module, "core.exit", quotatab_exit_ev);
4481 pr_event_unregister(&quotatab_module, "core.session-reinit",
4482 quotatab_sess_reinit_ev);
4483
4484 /* Reset defaults. */
4485 use_quotas = FALSE;
4486 (void) close(quota_logfd);
4487 quota_logfd = -1;
4488 quota_logname = NULL;
4489 quotatab_opts = 0UL;
4490 allow_site_quota = TRUE;
4491 use_dirs = FALSE;
4492 use_quotas = FALSE;
4493 have_quota_entry = FALSE;
4494 have_quota_limit_table = FALSE;
4495 have_quota_tally_table = FALSE;
4496 byte_units = BYTE;
4497
4498 (void) close(quota_lockfd);
4499 quota_lockfd = -1;
4500
4501 (void) quotatab_close(TYPE_LIMIT);
4502 (void) quotatab_close(TYPE_TALLY);
4503
4504 res = quotatab_sess_init();
4505 if (res < 0) {
4506 pr_session_disconnect(&quotatab_module,
4507 PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL);
4508 }
4509 }
4510
4511 /* Initialization routines
4512 */
4513
4514 static int quotatab_init(void) {
4515
4516 /* Initialize the module's memory pool. */
4517 if (quotatab_pool == NULL) {
4518 quotatab_pool = make_sub_pool(permanent_pool);
4519 pr_pool_tag(quotatab_pool, MOD_QUOTATAB_VERSION);
4520 }
4521
4522 #if defined(PR_SHARED_MODULE)
4523 pr_event_register(&quotatab_module, "core.module-unload",
4524 quotatab_mod_unload_ev, NULL);
4525 #endif
4526 pr_event_register(&quotatab_module, "core.restart", quotatab_restart_ev,
4527 NULL);
4528
4529 return 0;
4530 }
4531
4532 static int quotatab_sess_init(void) {
4533 config_rec *c;
4534 unsigned char *quotatab_engine = NULL, *quotatab_showquotas = NULL,
4535 *quotatab_usedirs = NULL;
4536 quota_units_t *units = NULL;
4537
4538 pr_event_register(&quotatab_module, "core.session-reinit",
4539 quotatab_sess_reinit_ev, NULL);
4540
4541 /* Check to see if quotas are enabled for this server. */
4542 quotatab_engine = get_param_ptr(main_server->conf, "QuotaEngine", FALSE);
4543 if (quotatab_engine != NULL &&
4544 *quotatab_engine == TRUE) {
4545 use_quotas = TRUE;
4546
4547 } else {
4548 use_quotas = FALSE;
4549 return 0;
4550 }
4551
4552 /* Check to see if SITE QUOTA enabled for this server. */
4553 quotatab_showquotas = get_param_ptr(main_server->conf, "QuotaShowQuotas",
4554 FALSE);
4555 if (quotatab_showquotas != NULL &&
4556 *quotatab_showquotas == FALSE) {
4557 allow_site_quota = FALSE;
4558
4559 } else {
4560 allow_site_quota = TRUE;
4561 }
4562
4563 quotatab_openlog();
4564
4565 /* Open the quota limit and tally tables. This is being done while the
4566 * proces still has root privs, so the tables _can_ be opened. In the case
4567 * of file quotastreams, the file descriptor is cached, so that this process,
4568 * once root privs are dropped, is still able to read from and write to the
4569 * streams. Other confstream mechanisms may need to cache something
4570 * similarly.
4571 */
4572 PRIVS_ROOT
4573 if (quotatab_open(TYPE_LIMIT) < 0) {
4574 PRIVS_RELINQUISH
4575 quotatab_log("error: unable to open QuotaLimitTable: %s", strerror(errno));
4576 have_quota_limit_table = FALSE;
4577
4578 } else {
4579 PRIVS_RELINQUISH
4580
4581 /* Verify that it's a valid limit table */
4582 if (quotatab_verify(TYPE_LIMIT)) {
4583 have_quota_limit_table = TRUE;
4584
4585 } else {
4586 use_quotas = FALSE;
4587 }
4588 }
4589
4590 PRIVS_ROOT
4591 if (quotatab_open(TYPE_TALLY) < 0) {
4592 PRIVS_RELINQUISH
4593 quotatab_log("error: unable to open QuotaTallyTable: %s", strerror(errno));
4594 have_quota_tally_table = FALSE;
4595
4596 } else {
4597 PRIVS_RELINQUISH
4598
4599 /* Verify that it's a valid tally table */
4600 if (quotatab_verify(TYPE_TALLY)) {
4601 have_quota_tally_table = TRUE;
4602
4603 } else {
4604 use_quotas = FALSE;
4605 }
4606 }
4607
4608 /* Make sure the tables will be closed when the child exits. */
4609 pr_event_register(&quotatab_module, "core.exit", quotatab_exit_ev, NULL);
4610
4611 /* Check for the units to display for byte quotas. */
4612 units = get_param_ptr(main_server->conf, "QuotaDisplayUnits", FALSE);
4613 byte_units = units ? *units : BYTE;
4614
4615 /* Check to see if directories are to be used for tallies for this server. */
4616 quotatab_usedirs = get_param_ptr(main_server->conf, "QuotaDirectoryTally",
4617 FALSE);
4618 if (quotatab_usedirs != NULL &&
4619 *quotatab_usedirs == TRUE) {
4620 use_dirs = TRUE;
4621
4622 } else {
4623 use_dirs = FALSE;
4624 }
4625
4626 c = find_config(main_server->conf, CONF_PARAM, "QuotaExcludeFilter", FALSE);
4627 if (c &&
4628 c->argc == 2) {
4629 quota_exclude_filter = c->argv[0];
4630 quota_exclude_pre = c->argv[1];
4631 }
4632
4633 c = find_config(main_server->conf, CONF_PARAM, "QuotaOptions", FALSE);
4634 while (c != NULL) {
4635 unsigned long opts;
4636
4637 pr_signals_handle();
4638
4639 opts = *((unsigned long *) c->argv[0]);
4640 quotatab_opts |= opts;
4641
4642 c = find_config_next(c, c->next, CONF_PARAM, "QuotaOptions", FALSE);
4643 }
4644
4645 c = find_config(main_server->conf, CONF_PARAM, "QuotaLock", FALSE);
4646 if (c) {
4647 int fd, xerrno;
4648 const char *path;
4649
4650 path = c->argv[0];
4651
4652 /* Make sure the QuotaLock file exists. */
4653 PRIVS_ROOT
4654 fd = open(path, O_RDWR|O_CREAT, 0600);
4655 xerrno = errno;
4656 PRIVS_RELINQUISH
4657
4658 if (fd < 0) {
4659 quotatab_log("unable to open QuotaLock '%s': %s", path, strerror(xerrno));
4660
4661 } else {
4662 /* Make sure that this fd is not one of the main three. */
4663 if (pr_fs_get_usable_fd2(&fd) < 0) {
4664 quotatab_log("warning: unable to find usable fd for lockfd %d: %s", fd,
4665 strerror(errno));
4666 }
4667
4668 quota_lockfd = fd;
4669 }
4670 }
4671
4672 memset(&sess_limit, 0, sizeof(sess_limit));
4673 memset(&sess_tally, 0, sizeof(sess_tally));
4674 return 0;
4675 }
4676
4677 /* Module API tables
4678 */
4679
4680 static conftable quotatab_conftab[] = {
4681 { "QuotaDefault", set_quotadefault, NULL },
4682 { "QuotaDirectoryTally", set_quotadirtally, NULL },
4683 { "QuotaDisplayUnits", set_quotadisplayunits, NULL },
4684 { "QuotaEngine", set_quotaengine, NULL },
4685 { "QuotaExcludeFilter", set_quotaexcludefilter, NULL },
4686 { "QuotaLimitTable", set_quotatable, NULL },
4687 { "QuotaLock", set_quotalock, NULL },
4688 { "QuotaLog", set_quotalog, NULL },
4689 { "QuotaOptions", set_quotaoptions, NULL },
4690 { "QuotaShowQuotas", set_quotashowquotas, NULL },
4691 { "QuotaTallyTable", set_quotatable, NULL },
4692 { NULL }
4693 };
4694
4695 static cmdtable quotatab_cmdtab[] = {
4696 { POST_CMD, C_ABOR, G_NONE, quotatab_post_abor, FALSE, FALSE },
4697 { PRE_CMD, C_APPE, G_NONE, quotatab_pre_appe, FALSE, FALSE },
4698 { POST_CMD, C_APPE, G_NONE, quotatab_post_appe, FALSE, FALSE },
4699 { POST_CMD_ERR, C_APPE, G_NONE, quotatab_post_appe_err, FALSE, FALSE },
4700 { PRE_CMD, C_DELE, G_NONE, quotatab_pre_dele, FALSE, FALSE },
4701 { POST_CMD, C_DELE, G_NONE, quotatab_post_dele, FALSE, FALSE },
4702 { POST_CMD_ERR, C_DELE, G_NONE, quotatab_post_dele_err, FALSE, FALSE },
4703 { PRE_CMD, C_MKD, G_NONE, quotatab_pre_mkd, FALSE, FALSE },
4704 { POST_CMD, C_MKD, G_NONE, quotatab_post_mkd, FALSE, FALSE },
4705 { POST_CMD_ERR, C_MKD, G_NONE, quotatab_post_mkd_err, FALSE, FALSE },
4706 { POST_CMD, C_PASS, G_NONE, quotatab_post_pass, FALSE, FALSE },
4707 { PRE_CMD, C_RETR, G_NONE, quotatab_pre_retr, FALSE, FALSE },
4708 { POST_CMD, C_RETR, G_NONE, quotatab_post_retr, FALSE, FALSE },
4709 { POST_CMD_ERR, C_RETR, G_NONE, quotatab_post_retr_err, FALSE, FALSE },
4710 { PRE_CMD, C_RMD, G_NONE, quotatab_pre_rmd, FALSE, FALSE },
4711 { POST_CMD, C_RMD, G_NONE, quotatab_post_rmd, FALSE, FALSE },
4712 { PRE_CMD, C_RNTO, G_NONE, quotatab_pre_rnto, FALSE, FALSE },
4713 { POST_CMD, C_RNTO, G_NONE, quotatab_post_rnto, FALSE, FALSE },
4714 { PRE_CMD, C_STOR, G_NONE, quotatab_pre_stor, FALSE, FALSE },
4715 { POST_CMD, C_STOR, G_NONE, quotatab_post_stor, FALSE, FALSE },
4716 { POST_CMD_ERR, C_STOR, G_NONE, quotatab_post_stor_err, FALSE, FALSE },
4717 { PRE_CMD, C_STOU, G_NONE, quotatab_pre_stor, FALSE, FALSE },
4718 { POST_CMD, C_STOU, G_NONE, quotatab_post_stor, FALSE, FALSE },
4719 { POST_CMD_ERR, C_STOU, G_NONE, quotatab_post_stor_err, FALSE, FALSE },
4720 { PRE_CMD, C_XMKD, G_NONE, quotatab_pre_mkd, FALSE, FALSE },
4721 { POST_CMD, C_XMKD, G_NONE, quotatab_post_mkd, FALSE, FALSE },
4722 { POST_CMD_ERR, C_XMKD, G_NONE, quotatab_post_mkd_err, FALSE, FALSE },
4723 { PRE_CMD, C_XRMD, G_NONE, quotatab_pre_rmd, FALSE, FALSE },
4724 { POST_CMD, C_XRMD, G_NONE, quotatab_post_rmd, FALSE, FALSE },
4725
4726 { PRE_CMD, C_SITE, G_NONE, quotatab_pre_site, FALSE, FALSE, CL_MISC },
4727 { CMD, C_SITE, G_NONE, quotatab_site, FALSE, FALSE, CL_MISC },
4728 { POST_CMD, C_SITE, G_NONE, quotatab_post_site, FALSE, FALSE, CL_MISC },
4729 { POST_CMD_ERR, C_SITE, G_NONE, quotatab_post_site_err, FALSE, FALSE, CL_MISC },
4730
4731 /* Should this be a single HOOK instead of being in the CMD namespace? */
4732 { PRE_CMD, "COPY", G_NONE, quotatab_pre_copy, FALSE, FALSE },
4733 { POST_CMD, "COPY", G_NONE, quotatab_post_copy, FALSE, FALSE },
4734 { POST_CMD_ERR, "COPY", G_NONE, quotatab_post_copy_err, FALSE, FALSE },
4735
4736 { 0, NULL }
4737 };
4738
4739 module quotatab_module = {
4740 NULL, NULL,
4741
4742 /* Module API version 2.0 */
4743 0x20,
4744
4745 /* Module name */
4746 "quotatab",
4747
4748 /* Module configuration handler table */
4749 quotatab_conftab,
4750
4751 /* Module command handler table */
4752 quotatab_cmdtab,
4753
4754 /* Module authentication handler table */
4755 NULL,
4756
4757 /* Module initialization function */
4758 quotatab_init,
4759
4760 /* Session initialization function */
4761 quotatab_sess_init,
4762
4763 /* Module version */
4764 MOD_QUOTATAB_VERSION
4765 };