Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Config/UserOwner.pm

raw
1 package ProFTPD::Tests::Config::UserOwner;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Spec;
8 use IO::Handle;
9
10 use ProFTPD::TestSuite::FTP;
11 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
12
13 $| = 1;
14
15 my $order = 0;
16
17 my $TESTS = {
18 userowner_file => {
19 order => ++$order,
20 test_class => [qw(forking rootprivs)],
21 },
22
23 userowner_dir => {
24 order => ++$order,
25 test_class => [qw(forking rootprivs)],
26 },
27
28 userowner_failed_norootprivs => {
29 order => ++$order,
30 test_class => [qw(forking norootprivs)],
31 },
32
33 userowner_anon_mkd => {
34 order => ++$order,
35 test_class => [qw(forking rootprivs)],
36 },
37
38 };
39
40 sub new {
41 return shift()->SUPER::new(@_);
42 }
43
44 sub list_tests {
45 return testsuite_get_runnable_tests($TESTS);
46 }
47
48 sub userowner_file {
49 my $self = shift;
50 my $tmpdir = $self->{tmpdir};
51
52 my $config_file = "$tmpdir/config.conf";
53 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
54 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
55
56 my $log_file = test_get_logfile();
57
58 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
59 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
60
61 my $user = 'proftpd';
62 my $passwd = 'test';
63 my $group = 'ftpd';
64 my $home_dir = File::Spec->rel2abs($tmpdir);
65 my $uid = 500;
66 my $gid = 500;
67
68 # Make sure that, if we're running as root, that the home directory has
69 # permissions/privs set for the account we create
70 if ($< == 0) {
71 unless (chmod(0755, $home_dir)) {
72 die("Can't set perms on $home_dir to 0755: $!");
73 }
74
75 unless (chown($uid, $gid, $home_dir)) {
76 die("Can't set owner of $home_dir to $uid/$gid: $!");
77 }
78 }
79
80 my $owner = 'proftpd2';
81 my $owner_uid = 1000;
82
83 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
84 '/bin/bash');
85 auth_user_write($auth_user_file, $owner, "none", $owner_uid, $gid, $home_dir,
86 '/bin/bash');
87 auth_group_write($auth_group_file, $group, $gid, $user);
88
89 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
90
91 my $config = {
92 PidFile => $pid_file,
93 ScoreboardFile => $scoreboard_file,
94 SystemLog => $log_file,
95
96 AuthUserFile => $auth_user_file,
97 AuthGroupFile => $auth_group_file,
98 AuthOrder => 'mod_auth_file.c',
99
100 Directory => {
101 '~' => {
102 UserOwner => $owner,
103 },
104 },
105
106 IfModules => {
107 'mod_cap.c' => {
108 CapabilitiesEngine => 'off',
109 },
110
111 'mod_delay.c' => {
112 DelayEngine => 'off',
113 },
114 },
115 };
116
117 my ($port, $config_user, $config_group) = config_write($config_file, $config);
118
119 # Open pipes, for use between the parent and child processes. Specifically,
120 # the child will indicate when it's done with its test by writing a message
121 # to the parent.
122 my ($rfh, $wfh);
123 unless (pipe($rfh, $wfh)) {
124 die("Can't open pipe: $!");
125 }
126
127 my $ex;
128
129 # Fork child
130 $self->handle_sigchld();
131 defined(my $pid = fork()) or die("Can't fork: $!");
132 if ($pid) {
133 eval {
134 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
135 $client->login($user, $passwd);
136
137 # Now upload a file into the directory, and make sure that the UserOwner
138 # directive is applied properly
139 my $conn = $client->stor_raw('test.txt');
140 unless ($conn) {
141 die("Failed to STOR test.txt: " . $client->response_code() . " " .
142 $client->response_msg());
143 }
144
145 my $buf = "Hello, World!\n";
146 $conn->write($buf, length($buf), 25);
147 eval { $conn->close() };
148
149 my $resp_code = $client->response_code();
150 my $resp_msg = $client->response_msg();
151 $self->assert_transfer_ok($resp_code, $resp_msg);
152
153 $client->quit();
154
155 my $owning_uid = (stat($test_file))[4];
156 $self->assert($owner_uid == $owning_uid,
157 test_msg("Expected $owner_uid, got $owning_uid"));
158 };
159
160 if ($@) {
161 $ex = $@;
162 }
163
164 $wfh->print("done\n");
165 $wfh->flush();
166
167 } else {
168 eval { server_wait($config_file, $rfh) };
169 if ($@) {
170 warn($@);
171 exit 1;
172 }
173
174 exit 0;
175 }
176
177 # Stop server
178 server_stop($pid_file);
179
180 $self->assert_child_ok($pid);
181
182 if ($ex) {
183 test_append_logfile($log_file, $ex);
184 unlink($log_file);
185
186 die($ex);
187 }
188
189 unlink($log_file);
190 }
191
192 sub userowner_dir {
193 my $self = shift;
194 my $tmpdir = $self->{tmpdir};
195
196 my $config_file = "$tmpdir/config.conf";
197 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
198 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
199
200 my $log_file = test_get_logfile();
201
202 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
203 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
204
205 my $user = 'proftpd';
206 my $passwd = 'test';
207 my $group = 'ftpd';
208 my $home_dir = File::Spec->rel2abs($tmpdir);
209 my $uid = 500;
210 my $gid = 500;
211
212 # Make sure that, if we're running as root, that the home directory has
213 # permissions/privs set for the account we create
214 if ($< == 0) {
215 unless (chmod(0755, $home_dir)) {
216 die("Can't set perms on $home_dir to 0755: $!");
217 }
218
219 unless (chown($uid, $gid, $home_dir)) {
220 die("Can't set owner of $home_dir to $uid/$gid: $!");
221 }
222 }
223
224 my $owner = 'proftpd2';
225 my $owner_uid = 1000;
226
227 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
228 '/bin/bash');
229 auth_user_write($auth_user_file, $owner, "none", $owner_uid, $gid, $home_dir,
230 '/bin/bash');
231 auth_group_write($auth_group_file, $group, $gid, $user);
232
233 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
234
235 my $config = {
236 PidFile => $pid_file,
237 ScoreboardFile => $scoreboard_file,
238 SystemLog => $log_file,
239
240 AuthUserFile => $auth_user_file,
241 AuthGroupFile => $auth_group_file,
242 AuthOrder => 'mod_auth_file.c',
243
244 Directory => {
245 '~' => {
246 UserOwner => $owner,
247 },
248 },
249
250 IfModules => {
251 'mod_cap.c' => {
252 CapabilitiesEngine => 'off',
253 },
254
255 'mod_delay.c' => {
256 DelayEngine => 'off',
257 },
258 },
259 };
260
261 my ($port, $config_user, $config_group) = config_write($config_file, $config);
262
263 # Open pipes, for use between the parent and child processes. Specifically,
264 # the child will indicate when it's done with its test by writing a message
265 # to the parent.
266 my ($rfh, $wfh);
267 unless (pipe($rfh, $wfh)) {
268 die("Can't open pipe: $!");
269 }
270
271 my $ex;
272
273 # Fork child
274 $self->handle_sigchld();
275 defined(my $pid = fork()) or die("Can't fork: $!");
276 if ($pid) {
277 eval {
278 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
279 $client->login($user, $passwd);
280
281 my ($resp_code, $resp_msg) = $client->mkd('test.d');
282 my $expected;
283
284 $expected = 257;
285 $self->assert($expected == $resp_code,
286 test_msg("Expected response code $expected, got $resp_code"));
287
288 $expected = "\"$test_dir\" - Directory successfully created";
289 $self->assert($expected eq $resp_msg,
290 test_msg("Expected response message '$expected', got '$resp_msg'"));
291
292 $client->quit();
293
294 $self->assert(-d $test_dir,
295 test_msg("$test_dir directory does not exist as expected"));
296
297 my $owning_uid = (stat($test_dir))[4];
298
299 $self->assert($owner_uid == $owning_uid,
300 test_msg("Expected owner UID $owner_uid, got $owning_uid"));
301 };
302
303 if ($@) {
304 $ex = $@;
305 }
306
307 $wfh->print("done\n");
308 $wfh->flush();
309
310 } else {
311 eval { server_wait($config_file, $rfh) };
312 if ($@) {
313 warn($@);
314 exit 1;
315 }
316
317 exit 0;
318 }
319
320 # Stop server
321 server_stop($pid_file);
322
323 $self->assert_child_ok($pid);
324
325 if ($ex) {
326 test_append_logfile($log_file, $ex);
327 unlink($log_file);
328
329 die($ex);
330 }
331
332 unlink($log_file);
333 }
334
335 sub userowner_failed_norootprivs {
336 my $self = shift;
337 my $tmpdir = $self->{tmpdir};
338
339 my $config_file = "$tmpdir/config.conf";
340 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
341 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
342
343 my $log_file = test_get_logfile();
344
345 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
346 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
347
348 my $owner = 'proftpd2';
349 my $owner_uid = 1000;
350
351 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
352
353 my $config = {
354 PidFile => $pid_file,
355 ScoreboardFile => $scoreboard_file,
356 SystemLog => $log_file,
357
358 AuthUserFile => $auth_user_file,
359 AuthGroupFile => $auth_group_file,
360 AuthOrder => 'mod_auth_file.c',
361
362 Directory => {
363 '~' => {
364 UserOwner => $owner,
365 },
366 },
367
368 IfModules => {
369 'mod_delay.c' => {
370 DelayEngine => 'off',
371 },
372 },
373 };
374
375 my ($port, $config_user, $config_group) = config_write($config_file, $config);
376
377 my $user = 'proftpd';
378 my $passwd = 'test';
379 my $group = 'ftpd';
380 my $home_dir = File::Spec->rel2abs($tmpdir);
381 my ($uid, $gid) = (getpwnam($config_user))[2,3];
382
383 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
384 '/bin/bash');
385 auth_user_write($auth_user_file, $owner, "none", $owner_uid, $gid, $home_dir,
386 '/bin/bash');
387 auth_group_write($auth_group_file, $group, $gid, $user);
388
389 # Open pipes, for use between the parent and child processes. Specifically,
390 # the child will indicate when it's done with its test by writing a message
391 # to the parent.
392 my ($rfh, $wfh);
393 unless (pipe($rfh, $wfh)) {
394 die("Can't open pipe: $!");
395 }
396
397 my $ex;
398
399 # Fork child
400 $self->handle_sigchld();
401 defined(my $pid = fork()) or die("Can't fork: $!");
402 if ($pid) {
403 eval {
404 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
405 $client->login($user, $passwd);
406
407 # Now upload a file into the directory, and make sure that the UserOwner
408 # directive is applied properly
409 my $conn = $client->stor_raw('test.txt');
410 unless ($conn) {
411 die("Failed to STOR test.txt: " . $client->response_code() . " " .
412 $client->response_msg());
413 }
414
415 my $buf = "Hello, World!\n";
416 $conn->write($buf, length($buf), 25);
417 eval { $conn->close() };
418
419 my $resp_code = $client->response_code();
420 my $resp_msg = $client->response_msg();
421 $self->assert_transfer_ok($resp_code, $resp_msg);
422
423 $client->quit();
424
425 my $owning_uid = (stat($test_file))[4];
426 $self->assert($uid == $owning_uid,
427 test_msg("Expected $uid, got $owning_uid"));
428 };
429
430 if ($@) {
431 $ex = $@;
432 }
433
434 $wfh->print("done\n");
435 $wfh->flush();
436
437 } else {
438 eval { server_wait($config_file, $rfh) };
439 if ($@) {
440 warn($@);
441 exit 1;
442 }
443
444 exit 0;
445 }
446
447 # Stop server
448 server_stop($pid_file);
449
450 $self->assert_child_ok($pid);
451
452 if ($ex) {
453 test_append_logfile($log_file, $ex);
454 unlink($log_file);
455
456 die($ex);
457 }
458
459 unlink($log_file);
460 }
461
462 sub userowner_anon_mkd {
463 my $self = shift;
464 my $tmpdir = $self->{tmpdir};
465
466 my $config_file = "$tmpdir/config.conf";
467 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
468 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
469
470 my $log_file = test_get_logfile();
471
472 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
473 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
474
475 my ($config_user, $config_group) = config_get_identity();
476 my $anon_dir = File::Spec->rel2abs($tmpdir);
477 my $uid = 500;
478 my $gid = 500;
479
480 # Make sure that, if we're running as root, that the home directory has
481 # permissions/privs set for the account we create
482 if ($< == 0) {
483 unless (chmod(0755, $anon_dir)) {
484 die("Can't set perms on $anon_dir to 0755: $!");
485 }
486
487 unless (chown($uid, $gid, $anon_dir)) {
488 die("Can't set owner of $anon_dir to $uid/$gid: $!");
489 }
490 }
491
492 auth_user_write($auth_user_file, $config_user, 'foo', $uid, $gid, '/tmp',
493 '/bin/bash');
494 auth_group_write($auth_group_file, $config_group, $gid, $config_user);
495
496 my $test_dir = File::Spec->rel2abs("$anon_dir/test.d");
497 my $owner_uid = 0;
498
499 my $config = {
500 PidFile => $pid_file,
501 ScoreboardFile => $scoreboard_file,
502 SystemLog => $log_file,
503 TraceLog => $log_file,
504 Trace => 'fsio:10 fileperms:10',
505
506 AuthUserFile => $auth_user_file,
507 AuthGroupFile => $auth_group_file,
508 AuthOrder => 'mod_auth_file.c',
509
510 IfModules => {
511 'mod_cap.c' => {
512 CapabilitiesEngine => 'off',
513 },
514
515 'mod_delay.c' => {
516 DelayEngine => 'off',
517 },
518 },
519
520 Anonymous => {
521 $anon_dir => {
522 User => $config_user,
523 Group => $config_group,
524 UserAlias => "anonymous $config_user",
525 RequireValidShell => 'off',
526
527 # Test the UserOwner directive in the <Anonymous> setting
528 UserOwner => 'root',
529 },
530 },
531 };
532
533 my ($port, $user, $group) = config_write($config_file, $config);
534
535 # Open pipes, for use between the parent and child processes. Specifically,
536 # the child will indicate when it's done with its test by writing a message
537 # to the parent.
538 my ($rfh, $wfh);
539 unless (pipe($rfh, $wfh)) {
540 die("Can't open pipe: $!");
541 }
542
543 my $ex;
544
545 # Fork child
546 $self->handle_sigchld();
547 defined(my $pid = fork()) or die("Can't fork: $!");
548 if ($pid) {
549 eval {
550 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
551 $client->login('anonymous', '[email protected]');
552
553 my ($resp_code, $resp_msg) = $client->mkd('test.d');
554
555 my $expected;
556
557 $expected = 257;
558 $self->assert($expected == $resp_code,
559 test_msg("Expected response code $expected, got $resp_code"));
560
561 $expected = "\"/test.d\" - Directory successfully created";
562 $self->assert($expected eq $resp_msg,
563 test_msg("Expected response message '$expected', got '$resp_msg'"));
564
565 $self->assert(-d $test_dir,
566 test_msg("$test_dir directory does not exist as expected"));
567
568 $client->quit();
569
570 my $owning_uid = (stat($test_dir))[4];
571 $self->assert($owner_uid == $owning_uid,
572 test_msg("Expected owner UID $owner_uid, got $owning_uid"));
573 };
574
575 if ($@) {
576 $ex = $@;
577 }
578
579 $wfh->print("done\n");
580 $wfh->flush();
581
582 } else {
583 eval { server_wait($config_file, $rfh) };
584 if ($@) {
585 warn($@);
586 exit 1;
587 }
588
589 exit 0;
590 }
591
592 # Stop server
593 server_stop($pid_file);
594
595 $self->assert_child_ok($pid);
596
597 if ($ex) {
598 test_append_logfile($log_file, $ex);
599 unlink($log_file);
600
601 die($ex);
602 }
603
604 unlink($log_file);
605 }
606
607 1;