Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::UserPassword;
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 userpassword_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 userpassword_anon_ok => {
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 userpassword_ok {
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 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
70 '/bin/bash');
71 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
72
73 my $user_pass_cleartext = 'R@lly';
74 my $des_salt = join('', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
75 my $user_pass_ciphertext = crypt($user_pass_cleartext, $des_salt);
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 UserPassword => "$user $user_pass_ciphertext",
87
88 IfModules => {
89 'mod_delay.c' => {
90 DelayEngine => 'off',
91 },
92 },
93 };
94
95 my ($port, $config_user, $config_group) = config_write($config_file, $config);
96
97 # Open pipes, for use between the parent and child processes. Specifically,
98 # the child will indicate when it's done with its test by writing a message
99 # to the parent.
100 my ($rfh, $wfh);
101 unless (pipe($rfh, $wfh)) {
102 die("Can't open pipe: $!");
103 }
104
105 my $ex;
106
107 # Fork child
108 $self->handle_sigchld();
109 defined(my $pid = fork()) or die("Can't fork: $!");
110 if ($pid) {
111 eval {
112 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
113 $client->login($user, $user_pass_cleartext);
114 };
115
116 if ($@) {
117 $ex = $@;
118 }
119
120 $wfh->print("done\n");
121 $wfh->flush();
122
123 } else {
124 eval { server_wait($config_file, $rfh) };
125 if ($@) {
126 warn($@);
127 exit 1;
128 }
129
130 exit 0;
131 }
132
133 # Stop server
134 server_stop($pid_file);
135
136 $self->assert_child_ok($pid);
137
138 if ($ex) {
139 die($ex);
140 }
141
142 unlink($log_file);
143 }
144
145 sub userpassword_anon_ok {
146 my $self = shift;
147 my $tmpdir = $self->{tmpdir};
148
149 my $config_file = "$tmpdir/config.conf";
150 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
151 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
152
153 my $log_file = File::Spec->rel2abs('tests.log');
154
155 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
156 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
157
158 my ($config_user, $config_group) = config_get_identity();
159
160 my $anon_dir = File::Spec->rel2abs($tmpdir);
161 my $passwd = 'test';
162 my $uid = 500;
163 my $gid = 500;
164
165 # Make sure that, if we're running as root, that the home directory has
166 # permissions/privs set for the account we create
167 if ($< == 0) {
168 unless (chmod(0755, $anon_dir)) {
169 die("Can't set perms on $anon_dir to 0755: $!");
170 }
171
172 unless (chown($uid, $gid, $anon_dir)) {
173 die("Can't set owner of $anon_dir to $uid/$gid: $!");
174 }
175 }
176
177 auth_user_write($auth_user_file, $config_user, 'foo', $uid, $gid, '/tmp',
178 '/bin/bash');
179 auth_group_write($auth_group_file, $config_group, $gid, $config_user);
180
181 my $user_pass_cleartext = 'test';
182 my $des_salt = join('', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
183 my $user_pass_ciphertext = crypt($user_pass_cleartext, $des_salt);
184
185 my $config = {
186 PidFile => $pid_file,
187 ScoreboardFile => $scoreboard_file,
188 SystemLog => $log_file,
189
190 AuthUserFile => $auth_user_file,
191 AuthGroupFile => $auth_group_file,
192 AuthOrder => 'mod_auth_file.c',
193
194 Anonymous => {
195 $anon_dir => {
196 User => $config_user,
197 Group => $config_group,
198 RequireValidShell => 'off',
199 UserPassword => "$config_user $user_pass_ciphertext",
200 AnonRequirePassword => 'on',
201 },
202 },
203
204 IfModules => {
205 'mod_delay.c' => {
206 DelayEngine => 'off',
207 },
208 },
209 };
210
211 my $port;
212 ($port, $config_user, $config_group) = config_write($config_file, $config);
213
214 # Open pipes, for use between the parent and child processes. Specifically,
215 # the child will indicate when it's done with its test by writing a message
216 # to the parent.
217 my ($rfh, $wfh);
218 unless (pipe($rfh, $wfh)) {
219 die("Can't open pipe: $!");
220 }
221
222 my $ex;
223
224 # Fork child
225 $self->handle_sigchld();
226 defined(my $pid = fork()) or die("Can't fork: $!");
227 if ($pid) {
228 eval {
229 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
230 my ($resp_code, $resp_msg) = $client->login($config_user, $user_pass_cleartext);
231
232 my $expected;
233
234 $expected = 230;
235 $self->assert($expected == $resp_code,
236 test_msg("Expected $expected, got $resp_code"));
237
238 $expected = 'Anonymous access granted, restrictions apply';
239 $self->assert($expected eq $resp_msg,
240 test_msg("Expected '$expected', got '$resp_msg'"));
241
242 $client->quit();
243 };
244
245 if ($@) {
246 $ex = $@;
247 }
248
249 $wfh->print("done\n");
250 $wfh->flush();
251
252 } else {
253 eval { server_wait($config_file, $rfh) };
254 if ($@) {
255 warn($@);
256 exit 1;
257 }
258
259 exit 0;
260 }
261
262 # Stop server
263 server_stop($pid_file);
264
265 $self->assert_child_ok($pid);
266
267 if ($ex) {
268 die($ex);
269 }
270
271 unlink($log_file);
272 }
273
274 1;