Debian Codegraph @

cassandane/Cassandane/Cyrus/ACL.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2017 FastMail Pty Ltd. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # 3. The name "Fastmail Pty Ltd" must not be used to
18 # endorse or promote products derived from this software without
19 # prior written permission. For permission or any legal
20 # details, please contact
21 # FastMail Pty Ltd
22 # PO Box 234
23 # Collins St West 8007
24 # Victoria
25 # Australia
26 #
27 # 4. Redistributions of any form whatsoever must retain the following
28 # acknowledgment:
29 # "This product includes software developed by Fastmail Pty. Ltd."
30 #
31 # FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
32 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
33 # EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT
34 # OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
35 # USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
36 # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
37 # OF THIS SOFTWARE.
38 #
39
40 package Cassandane::Cyrus::ACL;
41 use strict;
42 use warnings;
43 use DateTime;
44 use Data::Dumper;
45
46 use lib '.';
47 use base qw(Cassandane::Cyrus::TestCase);
48 use Cassandane::Util::Log;
49 use Cassandane::Generator;
50 use Cassandane::MessageStoreFactory;
51 use Cassandane::Instance;
52
53 sub new
54 {
55 my $class = shift;
56 return $class->SUPER::new({adminstore => 1}, @_);
57 }
58
59 sub set_up
60 {
61 my ($self) = @_;
62
63 $self->SUPER::set_up();
64
65 my $admintalk = $self->{adminstore}->get_client();
66
67 # let's create ourselves an archive user
68 # sub folders of another user - one is subscribable
69 $self->{instance}->create_user("archive",
70 subdirs => [ 'cassandane', ['cassandane', 'sent'] ]);
71 $admintalk->setacl("user.archive.cassandane.sent", "cassandane", "lrswp");
72 }
73
74 sub tear_down
75 {
76 my ($self) = @_;
77 $self->SUPER::tear_down();
78 }
79
80 #
81 # Test regular delete
82 #
83 sub test_delete
84 :NoAltNameSpace
85 {
86 my ($self) = @_;
87
88 my $admintalk = $self->{adminstore}->get_client();
89 my $talk = $self->{store}->get_client();
90
91 $self->{adminstore}->set_folder('user.archive.cassandane.sent');
92 $self->make_message("Message A", store => $self->{adminstore});
93
94 $self->{store}->set_folder('user.archive.cassandane.sent');
95 $self->{store}->_select();
96
97 my $res = $talk->store('1', '+flags', '(\\deleted)');
98 $self->assert_null($res); # means it failed
99 $self->assert_str_equals('no', $talk->get_last_completion_response());
100 $self->assert($talk->get_last_error() =~ m/permission denied/i);
101 }
102
103 sub test_many_users
104 :NoAltNameSpace
105 {
106 my ($self) = @_;
107
108 my $admintalk = $self->{adminstore}->get_client();
109 my $talk = $self->{store}->get_client();
110 $self->make_message("Message A");
111
112 $talk->create("INBOX.multi");
113 $self->assert_str_equals('ok', $talk->get_last_completion_response());
114
115 for (1..100) {
116 $admintalk->setacl("user.cassandane.multi", "test$_", "lrswipcd");
117 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
118 }
119
120 my $res = $talk->select("INBOX.multi");
121 $self->assert_str_equals('ok', $talk->get_last_completion_response());
122 }
123
124 sub test_move
125 :NoAltNameSpace
126 {
127 my ($self) = @_;
128
129 my $admintalk = $self->{adminstore}->get_client();
130 my $talk = $self->{store}->get_client();
131
132 $self->{adminstore}->set_folder('user.archive.cassandane.sent');
133 $self->make_message("Message A", store => $self->{adminstore});
134
135 $self->{store}->set_folder('user.archive.cassandane.sent');
136 $self->{store}->_select();
137
138 my $res = $talk->move('1', "INBOX");
139 $self->assert_null($res); # means it failed
140 $self->assert_str_equals('no', $talk->get_last_completion_response());
141 $self->assert($talk->get_last_error() =~ m/permission denied/i);
142 }
143
144 sub test_setacl_badacl
145 :NoAltNameSpace
146 {
147 my ($self) = @_;
148
149 my $admintalk = $self->{adminstore}->get_client();
150 my $talk = $self->{store}->get_client();
151
152 $talk->create("INBOX.badacl");
153 $self->assert_str_equals('ok', $talk->get_last_completion_response());
154
155 $admintalk->setacl("user.cassandane.badacl", "foo", "ylrswipcd");
156 $self->assert_str_equals('bad', $admintalk->get_last_completion_response());
157 }
158
159 sub test_setacl_addacl
160 :NoAltNameSpace
161 {
162 my ($self) = @_;
163
164 my $admintalk = $self->{adminstore}->get_client();
165 my $talk = $self->{store}->get_client();
166
167 $talk->create("INBOX.addacl");
168 $self->assert_str_equals('ok', $talk->get_last_completion_response());
169
170 $admintalk->setacl("user.cassandane.addacl", "foo", "lrswipkxtecdn");
171 $admintalk->setacl("user.cassandane.addacl", "foo", "+a");
172 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
173 }
174
175 sub test_setacl_rmacl
176 :NoAltNameSpace
177 {
178 my ($self) = @_;
179
180 my $admintalk = $self->{adminstore}->get_client();
181 my $talk = $self->{store}->get_client();
182
183 $talk->create("INBOX.rmacl");
184 $self->assert_str_equals('ok', $talk->get_last_completion_response());
185
186 $admintalk->setacl("user.cassandane.rmacl", "foo", "lrswipkxtecdan");
187 $admintalk->setacl("user.cassandane.rmacl", "foo", "-a");
188 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
189 }
190
191 sub test_setacl_addacl_exists
192 :NoAltNameSpace
193 {
194 my ($self) = @_;
195
196 my $admintalk = $self->{adminstore}->get_client();
197 my $talk = $self->{store}->get_client();
198
199 $talk->create("INBOX.exists");
200 $self->assert_str_equals('ok', $talk->get_last_completion_response());
201
202 $admintalk->setacl("user.cassandane.exists", "foo", "lrswipkxtecdan");
203 $admintalk->setacl("user.cassandane.exists", "foo", "+a");
204 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
205 }
206
207 sub test_setacl_rmacl_unexists
208 :NoAltNameSpace
209 {
210 my ($self) = @_;
211
212 my $admintalk = $self->{adminstore}->get_client();
213 my $talk = $self->{store}->get_client();
214
215 $talk->create("INBOX.rmunexists");
216 $self->assert_str_equals('ok', $talk->get_last_completion_response());
217
218 $admintalk->setacl("user.cassandane.rmunexists", "foo", "lrswipkxtecdn");
219 $admintalk->setacl("user.cassandane.rmunexists", "foo", "-a");
220 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
221 }
222
223 sub test_reconstruct
224 {
225 my ($self) = @_;
226
227 my $admintalk = $self->{adminstore}->get_client();
228 my $talk = $self->{store}->get_client();
229
230 my $oldacl = $admintalk->getacl("user.archive.cassandane.sent");
231
232 $self->{instance}->run_command({ cyrus => 1 }, 'reconstruct');
233
234 my $newacl = $admintalk->getacl("user.archive.cassandane.sent");
235 $self->assert_deep_equals($oldacl, $newacl);
236 }
237
238 sub test_setacl_emptyid
239 {
240 my ($self) = @_;
241
242 my $admintalk = $self->{adminstore}->get_client();
243 my $talk = $self->{store}->get_client();
244
245 $talk->create("INBOX.emptyid");
246 $self->assert_str_equals('ok', $talk->get_last_completion_response());
247
248 # send an empty identifier for SETACL
249 $admintalk->setacl("user.cassandane.emptyid", "", "lrswipcd");
250 $self->assert_str_equals('no', $admintalk->get_last_completion_response());
251 }
252
253 sub test_setacl_badrights
254 :NoAltNameSpace
255 {
256 my ($self) = @_;
257
258 my $admintalk = $self->{adminstore}->get_client();
259 my $talk = $self->{store}->get_client();
260
261 $talk->create("INBOX.badrights");
262 $self->assert_str_equals('ok', $talk->get_last_completion_response());
263
264 my $origacl = $admintalk->getacl("user.cassandane.badrights");
265
266 $admintalk->setacl("user.cassandane.badrights", "cassandane", "b");
267 $self->assert_str_equals('bad', $admintalk->get_last_completion_response());
268
269 my $newacl = $admintalk->getacl("user.cassandane.badrights");
270 $self->assert_deep_equals($origacl, $newacl);
271 }
272
273 #Magic word virtdomains in name sets config virtdomains = userid
274 sub test_virtdomains_noinherit1
275 :NoAltNamespace :CrossDomains
276 {
277 my ($self) = @_;
278
279 my $defaultdomain = $self->{instance}->{config}->get('defaultdomain')
280 // 'internal';
281 my $cass_defdom = "cassandane\@$defaultdomain";
282 my $cass_dom = '[email protected]';
283
284 # get stores that authenticate as each username
285 $self->{instance}->create_user($cass_dom);
286 my $imap = $self->{instance}->get_service('imap');
287 my $defdom_store = $imap->create_store(username => $cass_defdom);
288 my $dom_store = $imap->create_store(username => $cass_dom);
289
290 # set up a target folder and some permissions
291 $self->{instance}->create_user('banana');
292 my $folder = 'user.banana';
293 my $admintalk = $self->{adminstore}->get_client();
294 $admintalk->setacl($folder, cassandane => 'lrswip');
295 $admintalk->setacl($folder, $cass_dom => 'lrs');
296 $admintalk->getacl($folder);
297
298 # make the stores all look at the same folder
299 $self->{store}->set_folder($folder);
300 $defdom_store->set_folder($folder);
301 $dom_store->set_folder($folder);
302
303 # 'cassandane' should be able to make a message
304 $self->make_message("message from cassandane", store => $self->{store});
305
306 # 'cassandane@{defaultdomain}' should be able to make a message
307 $self->make_message("message from $cass_defdom", store => $defdom_store);
308
309 # 'cassandane@somedomain' should NOT be able to make a message
310 eval {
311 $self->make_message("message from $cass_dom", store => $dom_store);
312 };
313 my $err = q{} . $@;
314 $self->assert_matches(qr/permission denied/i, $err);
315 }
316
317 #Magic word virtdomains in name sets config virtdomains = userid
318 sub test_virtdomains_noinherit2
319 :NoAltNamespace :CrossDomains
320 {
321 my ($self) = @_;
322
323 my $defaultdomain = $self->{instance}->{config}->get('defaultdomain')
324 // 'internal';
325 my $cass_defdom = "cassandane\@$defaultdomain";
326 my $cass_dom = '[email protected]';
327
328 # get stores that authenticate as each username
329 $self->{instance}->create_user($cass_dom);
330 my $imap = $self->{instance}->get_service('imap');
331 my $defdom_store = $imap->create_store(username => $cass_defdom);
332 my $dom_store = $imap->create_store(username => $cass_dom);
333
334 # set up a target folder and some permissions
335 $self->{instance}->create_user('banana');
336 my $folder = 'user.banana';
337 my $admintalk = $self->{adminstore}->get_client();
338 $admintalk->setacl($folder, cassandane => 'lrs');
339 $admintalk->setacl($folder, $cass_dom => 'lrswip');
340 $admintalk->getacl($folder);
341
342 # make the stores all look at the same folder
343 $self->{store}->set_folder($folder);
344 $defdom_store->set_folder($folder);
345 $dom_store->set_folder($folder);
346
347 # 'cassandane' should NOT be able to make a message
348 eval {
349 $self->make_message("message from cassandane",
350 store => $self->{store});
351 };
352 my $err = q{} . $@;
353 $self->assert_matches(qr/permission denied/i, $err);
354
355 # 'cassandane@{defaultdomain}' should NOT be able to make a message
356 eval {
357 $self->make_message("message from $cass_defdom",
358 store => $defdom_store);
359 };
360 $err = q{} . $@;
361 $self->assert_matches(qr/permission denied/i, $err);
362
363 # 'cassandane@somedomain' should be able to make a message
364 $self->make_message("message from $cass_dom", store => $dom_store);
365 }
366
367 sub test_listrights_other_user
368 {
369 my ($self) = @_;
370
371 my $talk = $self->{store}->get_client;
372
373 # Create a victim user. We grant the attacker no explicit rights on the
374 # victim's mailbox: the default ACL "anyone p" is enough to expose the
375 # bug. (LISTRIGHTS gates only on "has any right at all", where it ought
376 # to require ACL_ADMIN; ACL_POST from the anyone grant is sufficient to
377 # slip past.)
378 $self->{instance}->create_user('victim');
379
380 my $seen;
381 $talk->_imap_cmd(
382 'LISTRIGHTS', 0,
383 { listrights => sub { $seen = 1 } },
384 'Other Users.victim', 'victim',
385 );
386
387 $self->assert_str_equals('no', $talk->get_last_completion_response);
388 $self->assert_null($seen);
389 }
390
391 # see also LDAP.pm for groupid tests
392
393 1;