Debian Codegraph @

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

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