Debian Codegraph @

debian/patches/d4ff72c0c32db68ef2cecfe08c53ac42a7bf85dd.diff

raw
1 From d4ff72c0c32db68ef2cecfe08c53ac42a7bf85dd Mon Sep 17 00:00:00 2001
2 From: TJ Saunders <[email protected]>
3 Date: Wed, 20 May 2026 11:37:10 -0700
4 Subject: [PATCH] Issue #2098: Make sure that mod_quotatab provides a
5 `pwrite(2)` FSIO handler as well, so that uploads via SFTP using `pwrite(2)`
6 have hard upload limits enforced.
7
8 ---
9 contrib/mod_quotatab.c | 68 +++++
10 .../Tests/Modules/mod_sftp/quotatab.pm | 271 ++++++++++++++++++
11 tests/t/modules/mod_sftp/quotatab.t | 11 +
12 3 files changed, 350 insertions(+)
13 create mode 100644 tests/t/lib/ProFTPD/Tests/Modules/mod_sftp/quotatab.pm
14 create mode 100644 tests/t/modules/mod_sftp/quotatab.t
15
16 diff --git a/contrib/mod_quotatab.c b/contrib/mod_quotatab.c
17 index 903dca4b61..30d4f0348d 100644
18 --- a/contrib/mod_quotatab.c
19 +++ b/contrib/mod_quotatab.c
20 @@ -1432,6 +1432,73 @@ int quotatab_write(quota_tally_t *tally,
21
22 static off_t copied_bytes = 0;
23
24 +static ssize_t quotatab_fsio_pwrite(pr_fh_t *fh, int fd, const void *buf,
25 + size_t bufsz, off_t offset) {
26 + ssize_t res;
27 + off_t total_bytes;
28 +
29 + res = pwrite(fd, buf, bufsz, offset);
30 + if (res < 0) {
31 + return res;
32 + }
33 +
34 + if (have_quota_update == 0) {
35 + return res;
36 + }
37 +
38 + /* Check to see if we've exceeded our upload limit. mod_xfer will
39 + * have called pr_data_xfer(), which will have updated
40 + * session.xfer.total_bytes, before calling pr_fsio_write(), so
41 + * we do not have to worry about updated/changing session.xfer.total_bytes
42 + * ourselves.
43 + *
44 + * Note that there is a race condition here: it is possible for the same
45 + * user to be writing to the same file in chunks from multiple
46 + * simultaneous connections.
47 + */
48 +
49 + /* If the client is copying a file (versus uploading a file), then we need
50 + * to track the "total bytes" differently.
51 + */
52 + if (session.curr_cmd_id == PR_CMD_SITE_ID &&
53 + (session.curr_cmd_rec->argc >= 2 &&
54 + (strcasecmp(session.curr_cmd_rec->argv[1], "CPTO") == 0 ||
55 + strcasecmp(session.curr_cmd_rec->argv[1], "COPY") == 0))) {
56 + copied_bytes += res;
57 + total_bytes = copied_bytes;
58 +
59 + } else {
60 + total_bytes = session.xfer.total_bytes;
61 + }
62 +
63 + if (sess_limit.bytes_in_avail > 0.0 &&
64 + sess_tally.bytes_in_used + total_bytes > sess_limit.bytes_in_avail) {
65 + int xerrno;
66 + char *errstr = NULL;
67 +
68 + xerrno = get_quota_exceeded_errno(EIO, &errstr);
69 + quotatab_log("quotatab write(): limit exceeded, returning %s", errstr);
70 +
71 + errno = xerrno;
72 + return -1;
73 + }
74 +
75 + if (sess_limit.bytes_xfer_avail > 0.0 &&
76 + sess_tally.bytes_xfer_used + total_bytes > sess_limit.bytes_xfer_avail) {
77 + int xerrno;
78 + char *errstr = NULL;
79 +
80 + xerrno = get_quota_exceeded_errno(EIO, &errstr);
81 + quotatab_log("quotatab write(): transfer limit exceeded, returning %s",
82 + errstr);
83 +
84 + errno = xerrno;
85 + return -1;
86 + }
87 +
88 + return res;
89 +}
90 +
91 static int quotatab_fsio_write(pr_fh_t *fh, int fd, const char *buf,
92 size_t bufsz) {
93 int res;
94 @@ -3214,6 +3281,7 @@ MODRET quotatab_post_pass(cmd_rec *cmd) {
95 * For Issue #1764, this is not a problem due to the fact that
96 * mod_vroot does not override the system FS write callbacks.
97 */
98 + fs->pwrite = quotatab_fsio_pwrite;
99 fs->write = quotatab_fsio_write;
100
101 } else {
102 diff --git a/tests/t/lib/ProFTPD/Tests/Modules/mod_sftp/quotatab.pm b/tests/t/lib/ProFTPD/Tests/Modules/mod_sftp/quotatab.pm
103 new file mode 100644
104 index 0000000000..d3a9a4e93b
105 --- /dev/null
106 +++ b/tests/t/lib/ProFTPD/Tests/Modules/mod_sftp/quotatab.pm
107 @@ -0,0 +1,271 @@
108 +package ProFTPD::Tests::Modules::mod_sftp::quotatab;
109 +
110 +use lib qw(t/lib);
111 +use base qw(ProFTPD::TestSuite::Child);
112 +use strict;
113 +
114 +use File::Path qw(mkpath);
115 +use File::Spec;
116 +use IO::Handle;
117 +use POSIX qw(:fcntl_h);
118 +
119 +use ProFTPD::TestSuite::FTP;
120 +use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
121 +
122 +$| = 1;
123 +
124 +my $order = 0;
125 +
126 +my $TESTS = {
127 + sftp_quotatab_upload_hard_limit_issue2098 => {
128 + order => ++$order,
129 + test_class => [qw(bug forking mod_quotatab_sql mod_sql_sqlite)],
130 + },
131 +
132 +};
133 +
134 +sub new {
135 + return shift()->SUPER::new(@_);
136 +}
137 +
138 +sub list_tests {
139 + return testsuite_get_runnable_tests($TESTS);
140 +}
141 +
142 +sub set_up {
143 + my $self = shift;
144 + $self->SUPER::set_up(@_);
145 +
146 + # Make sure that mod_sftp does not complain about permissions on the hostkey
147 + # files.
148 +
149 + my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
150 + my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
151 +
152 + unless (chmod(0400, $rsa_host_key, $dsa_host_key)) {
153 + die("Can't set perms on $rsa_host_key, $dsa_host_key: $!");
154 + }
155 +}
156 +
157 +sub sftp_quotatab_upload_hard_limit_issue2098 {
158 + my $self = shift;
159 + my $tmpdir = $self->{tmpdir};
160 + my $setup = test_setup($tmpdir, 'quotatab');
161 +
162 + my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
163 + my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
164 +
165 + my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
166 +
167 + my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
168 +
169 + if (open(my $fh, "> $db_script")) {
170 + print $fh <<EOS;
171 +CREATE TABLE quotalimits (
172 + name TEXT NOT NULL PRIMARY KEY,
173 + quota_type TEXT NOT NULL,
174 + per_session TEXT NOT NULL,
175 + limit_type TEXT NOT NULL,
176 + bytes_in_avail REAL NOT NULL,
177 + bytes_out_avail REAL NOT NULL,
178 + bytes_xfer_avail REAL NOT NULL,
179 + files_in_avail INTEGER NOT NULL,
180 + files_out_avail INTEGER NOT NULL,
181 + files_xfer_avail INTEGER NOT NULL
182 +);
183 +INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$setup->{user}', 'user', 'false', 'hard', 32, 0, 0, 2, 0, 0);
184 +
185 +CREATE TABLE quotatallies (
186 + name TEXT NOT NULL PRIMARY KEY,
187 + quota_type TEXT NOT NULL,
188 + bytes_in_used REAL NOT NULL,
189 + bytes_out_used REAL NOT NULL,
190 + bytes_xfer_used REAL NOT NULL,
191 + files_in_used INTEGER NOT NULL,
192 + files_out_used INTEGER NOT NULL,
193 + files_xfer_used INTEGER NOT NULL
194 +);
195 +EOS
196 +
197 + unless (close($fh)) {
198 + die("Can't write $db_script: $!");
199 + }
200 +
201 + } else {
202 + die("Can't open $db_script: $!");
203 + }
204 +
205 + my $cmd = "sqlite3 $db_file < $db_script";
206 +
207 + if ($ENV{TEST_VERBOSE}) {
208 + print STDERR "Executing sqlite3: $cmd\n";
209 + }
210 +
211 + my @output = `$cmd`;
212 + if (scalar(@output) &&
213 + $ENV{TEST_VERBOSE}) {
214 + print STDERR "Output: ", join('', @output), "\n";
215 + }
216 +
217 + # Make sure that, if we're running as root, the database file has
218 + # the permissions/privs set for use by proftpd
219 + if ($< == 0) {
220 + unless (chmod(0666, $db_file)) {
221 + die("Can't set perms on $db_file to 0666: $!");
222 + }
223 + }
224 +
225 + my $config = {
226 + PidFile => $setup->{pid_file},
227 + ScoreboardFile => $setup->{scoreboard_file},
228 + SystemLog => $setup->{log_file},
229 + TraceLog => $setup->{log_file},
230 + Trace => 'fsio:20 quotatab:20 sql:20 ssh2:20 sftp:30',
231 +
232 + AuthUserFile => $setup->{auth_user_file},
233 + AuthGroupFile => $setup->{auth_group_file},
234 + AuthOrder => 'mod_auth_file.c',
235 +
236 + IfModules => {
237 + 'mod_delay.c' => {
238 + DelayEngine => 'off',
239 + },
240 +
241 + 'mod_quotatab_sql.c' => [
242 + 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
243 + 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
244 + 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
245 + 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
246 +
247 + 'QuotaEngine on',
248 + "QuotaLog $setup->{log_file}",
249 + 'QuotaLimitTable sql:/get-quota-limit',
250 + 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
251 + ],
252 +
253 + 'mod_sftp.c' => [
254 + "SFTPEngine on",
255 + "SFTPLog $setup->{log_file}",
256 + "SFTPHostKey $rsa_host_key",
257 + "SFTPHostKey $dsa_host_key",
258 + ],
259 +
260 + 'mod_sql.c' => {
261 + SQLEngine => 'log',
262 + SQLBackend => 'sqlite3',
263 + SQLConnectInfo => $db_file,
264 + SQLLogFile => $setup->{log_file},
265 + },
266 + },
267 + };
268 +
269 + my ($port, $config_user, $config_group) = config_write($setup->{config_file},
270 + $config);
271 +
272 + # Open pipes, for use between the parent and child processes. Specifically,
273 + # the child will indicate when it's done with its test by writing a message
274 + # to the parent.
275 + my ($rfh, $wfh);
276 + unless (pipe($rfh, $wfh)) {
277 + die("Can't open pipe: $!");
278 + }
279 +
280 + require Net::SSH2;
281 +
282 + my $ex;
283 +
284 + # Fork child
285 + $self->handle_sigchld();
286 + defined(my $pid = fork()) or die("Can't fork: $!");
287 + if ($pid) {
288 + eval {
289 + my $ssh2 = Net::SSH2->new();
290 +
291 + sleep(1);
292 +
293 + unless ($ssh2->connect('127.0.0.1', $port)) {
294 + my ($err_code, $err_name, $err_str) = $ssh2->error();
295 + die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
296 + }
297 +
298 + unless ($ssh2->auth_password($setup->{user}, $setup->{passwd})) {
299 + my ($err_code, $err_name, $err_str) = $ssh2->error();
300 + die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
301 + }
302 +
303 + my $sftp = $ssh2->sftp();
304 + unless ($sftp) {
305 + my ($err_code, $err_name, $err_str) = $ssh2->error();
306 + die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
307 + }
308 +
309 + my $fh = $sftp->open('test.dat', O_WRONLY|O_CREAT|O_TRUNC, 0644);
310 + unless ($fh) {
311 + my ($err_code, $err_name) = $sftp->error();
312 + die("Can't open test.txt: [$err_name] ($err_code)");
313 + }
314 +
315 + my $count = 20;
316 + for (my $i = 0; $i < $count; $i++) {
317 + print $fh "ABCD" x 8192;
318 + }
319 +
320 + # To issue the FXP_CLOSE, we have to explicitly destroy the filehandle
321 + $fh = undef;
322 +
323 + $sftp = undef;
324 + $ssh2->disconnect();
325 + };
326 + if ($@) {
327 + $ex = $@;
328 + }
329 +
330 + $wfh->print("done\n");
331 + $wfh->flush();
332 +
333 + } else {
334 + eval { server_wait($setup->{config_file}, $rfh) };
335 + if ($@) {
336 + warn($@);
337 + exit 1;
338 + }
339 +
340 + exit 0;
341 + }
342 +
343 + # Stop server
344 + server_stop($setup->{pid_file});
345 + $self->assert_child_ok($pid);
346 +
347 + eval {
348 + if (open(my $fh, "< $setup->{log_file}")) {
349 + my $ok = 0;
350 +
351 + while (my $line = <$fh>) {
352 + chomp($line);
353 +
354 + if ($ENV{TEST_VERBOSE}) {
355 + print STDERR "# $line\n";
356 + }
357 +
358 + if ($line =~ /limit exceeded/) {
359 + $ok = 1;
360 + last;
361 + }
362 + }
363 +
364 + close($fh);
365 + $self->assert($ok, test_msg("Did not see expected QuotaLog message"));
366 +
367 + } else {
368 + die("Can't read $setup->{log_file}: $!");
369 + }
370 + };
371 + if ($@) {
372 + $ex = $@;
373 + }
374 +
375 + test_cleanup($setup->{log_file}, $ex);
376 +}
377 +
378 +1;
379 diff --git a/tests/t/modules/mod_sftp/quotatab.t b/tests/t/modules/mod_sftp/quotatab.t
380 new file mode 100644
381 index 0000000000..ddcf1ccf42
382 --- /dev/null
383 +++ b/tests/t/modules/mod_sftp/quotatab.t
384 @@ -0,0 +1,11 @@
385 +#!/usr/bin/env perl
386 +
387 +use lib qw(t/lib);
388 +use strict;
389 +
390 +use Test::Unit::HarnessUnit;
391 +
392 +$| = 1;
393 +
394 +my $r = Test::Unit::HarnessUnit->new();
395 +$r->start("ProFTPD::Tests::Modules::mod_sftp::quotatab");