Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::AuthUsingAlias;
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 authusingalias_on => {
19 order => ++$order,
20 test_class => [qw(forking rootprivs)],
21 },
22
23 authusingalias_off => {
24 order => ++$order,
25 test_class => [qw(forking rootprivs)],
26 },
27
28 };
29
30 sub new {
31 return shift()->SUPER::new(@_);
32 }
33
34 sub list_tests {
35 return testsuite_get_runnable_tests($TESTS);
36 }
37
38 sub authusingalias_on {
39 my $self = shift;
40 my $tmpdir = $self->{tmpdir};
41
42 my $config_file = "$tmpdir/config.conf";
43 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
44 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
45
46 my $log_file = File::Spec->rel2abs('tests.log');
47
48 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
49 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
50
51 my $user = 'proftpd';
52 my $passwd = 'test';
53 my $home_dir = File::Spec->rel2abs($tmpdir);
54 my $uid = 500;
55 my $gid = 500;
56
57 # Make sure that, if we're running as root, that the home directory has
58 # permissions/privs set for the account we create
59 if ($< == 0) {
60 unless (chmod(0755, $home_dir)) {
61 die("Can't set perms on $home_dir to 0755: $!");
62 }
63
64 unless (chown($uid, $gid, $home_dir)) {
65 die("Can't set owner of $home_dir to $uid/$gid: $!");
66 }
67 }
68
69 my $alias = 'ftp';
70
71 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
72 '/bin/bash');
73 auth_user_write($auth_user_file, $alias, $passwd, $uid, $gid, $home_dir,
74 '/bin/bash');
75 auth_group_write($auth_group_file, 'ftpd', $gid, "$user,$alias");
76
77 my $config = {
78 PidFile => $pid_file,
79 ScoreboardFile => $scoreboard_file,
80 SystemLog => $log_file,
81
82 AuthUserFile => $auth_user_file,
83 AuthGroupFile => $auth_group_file,
84 AuthOrder => 'mod_auth_file.c',
85
86 IfModules => {
87 'mod_delay.c' => {
88 DelayEngine => 'off',
89 },
90 },
91 };
92
93 my ($port, $config_user, $config_group) = config_write($config_file, $config);
94
95 if (open(my $fh, ">> $config_file")) {
96 print $fh <<EOC;
97 <Anonymous $home_dir>
98 User $user
99 Group ftpd
100 UserAlias $alias $user
101 AuthUsingAlias on
102 RequireValidShell off
103
104 <Limit LOGIN>
105 DenyUser $alias
106 </Limit>
107 </Anonymous>
108 EOC
109 unless (close($fh)) {
110 die("Can't write $config_file: $!");
111 }
112
113 } else {
114 die("Can't open $config_file: $!");
115 }
116
117 # Open pipes, for use between the parent and child processes. Specifically,
118 # the child will indicate when it's done with its test by writing a message
119 # to the parent.
120 my ($rfh, $wfh);
121 unless (pipe($rfh, $wfh)) {
122 die("Can't open pipe: $!");
123 }
124
125 my $ex;
126
127 # Fork child
128 $self->handle_sigchld();
129 defined(my $pid = fork()) or die("Can't fork: $!");
130 if ($pid) {
131 eval {
132 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
133 $client->login($alias, $passwd);
134 };
135
136 if ($@) {
137 $ex = $@;
138 }
139
140 $wfh->print("done\n");
141 $wfh->flush();
142
143 } else {
144 eval { server_wait($config_file, $rfh) };
145 if ($@) {
146 warn($@);
147 exit 1;
148 }
149
150 exit 0;
151 }
152
153 # Stop server
154 server_stop($pid_file);
155
156 $self->assert_child_ok($pid);
157
158 if ($ex) {
159 die($ex);
160 }
161
162 unlink($log_file);
163 }
164
165 sub authusingalias_off {
166 my $self = shift;
167 my $tmpdir = $self->{tmpdir};
168
169 my $config_file = "$tmpdir/config.conf";
170 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
171 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
172
173 my $log_file = File::Spec->rel2abs('tests.log');
174
175 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
176 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
177
178 my $user = 'proftpd';
179 my $passwd = 'test';
180 my $home_dir = File::Spec->rel2abs($tmpdir);
181 my $uid = 500;
182 my $gid = 500;
183
184 # Make sure that, if we're running as root, that the home directory has
185 # permissions/privs set for the account we create
186 if ($< == 0) {
187 unless (chmod(0755, $home_dir)) {
188 die("Can't set perms on $home_dir to 0755: $!");
189 }
190
191 unless (chown($uid, $gid, $home_dir)) {
192 die("Can't set owner of $home_dir to $uid/$gid: $!");
193 }
194 }
195
196 my $alias = 'ftp';
197
198 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
199 '/bin/bash');
200 auth_group_write($auth_group_file, 'ftpd', $gid, "$user,$alias");
201
202 my $config = {
203 PidFile => $pid_file,
204 ScoreboardFile => $scoreboard_file,
205 SystemLog => $log_file,
206
207 AuthUserFile => $auth_user_file,
208 AuthGroupFile => $auth_group_file,
209 AuthOrder => 'mod_auth_file.c',
210
211 IfModules => {
212 'mod_delay.c' => {
213 DelayEngine => 'off',
214 },
215 },
216 };
217
218 my ($port, $config_user, $config_group) = config_write($config_file, $config);
219
220 if (open(my $fh, ">> $config_file")) {
221 print $fh <<EOC;
222 <Anonymous $home_dir>
223 User $user
224 Group ftpd
225 UserAlias $alias $user
226 AuthUsingAlias off
227 RequireValidShell off
228
229 <Limit LOGIN>
230 DenyUser $user
231 </Limit>
232 </Anonymous>
233 EOC
234 unless (close($fh)) {
235 die("Can't write $config_file: $!");
236 }
237
238 } else {
239 die("Can't open $config_file: $!");
240 }
241
242 # Open pipes, for use between the parent and child processes. Specifically,
243 # the child will indicate when it's done with its test by writing a message
244 # to the parent.
245 my ($rfh, $wfh);
246 unless (pipe($rfh, $wfh)) {
247 die("Can't open pipe: $!");
248 }
249
250 my $ex;
251
252 # Fork child
253 $self->handle_sigchld();
254 defined(my $pid = fork()) or die("Can't fork: $!");
255 if ($pid) {
256 eval {
257 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
258 eval { $client->login($alias, $passwd) };
259 unless ($@) {
260 die("Login succeeded unexpectedly");
261 }
262
263 my $resp_code = $client->response_code();
264 my $resp_msg = $client->response_msg();
265
266 my $expected;
267
268 $expected = 530;
269 $self->assert($expected == $resp_code,
270 test_msg("Expected '$expected', got '$resp_code'"));
271
272 $expected = 'Login incorrect.';
273 $self->assert($expected eq $resp_msg,
274 test_msg("Expected '$expected', got '$resp_msg'"));
275 };
276
277 if ($@) {
278 $ex = $@;
279 }
280
281 $wfh->print("done\n");
282 $wfh->flush();
283
284 } else {
285 eval { server_wait($config_file, $rfh) };
286 if ($@) {
287 warn($@);
288 exit 1;
289 }
290
291 exit 0;
292 }
293
294 # Stop server
295 server_stop($pid_file);
296
297 $self->assert_child_ok($pid);
298
299 if ($ex) {
300 die($ex);
301 }
302
303 unlink($log_file);
304 }
305
306 1;