Debian Codegraph @

cassandane/Cassandane/Cyrus/Mboxgroups.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2024 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::Mboxgroups;
41 use strict;
42 use warnings;
43 use Cwd qw(realpath);
44 use JSON;
45 use Data::Dumper;
46
47 use lib '.';
48 use base qw(Cassandane::Cyrus::TestCase);
49 use base qw(Cassandane::Unit::TestCase);
50 use Cassandane::Util::Log;
51
52 sub new
53 {
54 my ($class, @args) = @_;
55
56 my $config = Cassandane::Config->default()->clone();
57 $config->set(
58 auth_mech => 'mboxgroups',
59 );
60
61 my $self = $class->SUPER::new({
62 config => $config,
63 adminstore => 1,
64 services => [qw( imap )],
65 start_instances => 0,
66 }, @args);
67
68 return $self;
69 }
70
71 sub set_up
72 {
73 my ($self) = @_;
74
75 $self->SUPER::set_up();
76
77 $self->_start_instances();
78
79 $self->{instance}->create_user("otheruser");
80
81 my $admintalk = $self->{adminstore}->get_client();
82
83 $admintalk->_imap_cmd('SETUSERGROUP', 0, '', 'cassandane', 'group:group c');
84 $admintalk->_imap_cmd('SETUSERGROUP', 0, '', 'cassandane', 'group:group co');
85 $admintalk->_imap_cmd('SETUSERGROUP', 0, '', 'otheruser', 'group:group co');
86 $admintalk->_imap_cmd('SETUSERGROUP', 0, '', 'otheruser', 'group:group o');
87 }
88
89 sub tear_down
90 {
91 my ($self) = @_;
92
93 # clean this up as soon as we're done with it, cause it's holding a
94 # port open!
95 delete $self->{server};
96
97 $self->SUPER::tear_down();
98 }
99
100 sub imap_getusergroup
101 {
102 my ($self, $talk, $item) = @_;
103
104 my $usergroups = {};
105 my $handlers = {
106 'usergroup' => sub {
107 my (undef, $response) = @_;
108
109 my %ug = @{$response};
110 while (my ($user, $groups) = each %ug) {
111 $usergroups->{$user} = { map { $_ => 1 } @{$groups} };
112 }
113 },
114 };
115
116 $talk->_imap_cmd('GETUSERGROUP', 0, $handlers, $item);
117 $self->assert_str_equals('ok', $talk->get_last_completion_response());
118
119 return $usergroups;
120 }
121
122 sub test_setacl_groupid
123 {
124 my ($self) = @_;
125
126 my $admintalk = $self->{adminstore}->get_client();
127
128 $admintalk->create("user.cassandane.groupid");
129 $self->assert_str_equals('ok',
130 $admintalk->get_last_completion_response());
131
132 $admintalk->setacl("user.cassandane.groupid",
133 "group:foo",
134 "lrswipkxtecdan");
135 $self->assert_str_equals('ok',
136 $admintalk->get_last_completion_response());
137 }
138
139 sub test_setacl_groupid_spaces
140 {
141 my ($self) = @_;
142
143 my $admintalk = $self->{adminstore}->get_client();
144
145 $admintalk->create("user.cassandane.groupid_spaces");
146 $self->assert_str_equals('ok',
147 $admintalk->get_last_completion_response());
148
149 $admintalk->setacl("user.cassandane.groupid_spaces",
150 "group:this group name has spaces",
151 "lrswipkxtecdan");
152 $self->assert_str_equals('ok',
153 $admintalk->get_last_completion_response());
154
155 my $data = $admintalk->getacl("user.cassandane.groupid_spaces");
156 $self->assert_str_equals('ok',
157 $admintalk->get_last_completion_response());
158
159 $self->assert(scalar @{$data} % 2 == 0);
160 my %acl = @{$data};
161 $self->assert_str_equals($acl{"group:this group name has spaces"},
162 "lrswipkxtecdan");
163
164 $admintalk->select("user.cassandane.groupid_spaces");
165 $self->assert_str_equals('ok',
166 $admintalk->get_last_completion_response());
167 }
168
169 sub test_list_groupaccess_noracl
170 :NoAltNamespace
171 {
172 my ($self) = @_;
173
174 my $admintalk = $self->{adminstore}->get_client();
175 my $imaptalk = $self->{store}->get_client();
176
177 $admintalk->create("user.otheruser.groupaccess");
178 $self->assert_str_equals('ok',
179 $admintalk->get_last_completion_response());
180
181 $admintalk->setacl("user.otheruser.groupaccess",
182 "group:group co", "lrswipkxtecdan");
183 $self->assert_str_equals('ok',
184 $admintalk->get_last_completion_response());
185
186 my $list = $imaptalk->list("", "*");
187 my @boxes = sort map { $_->[2] } @{$list};
188
189 $self->assert_deep_equals(\@boxes,
190 ['INBOX', 'user.otheruser.groupaccess']);
191 }
192
193 sub test_list_groupaccess_racl
194 :ReverseACLs :NoAltNamespace
195 {
196 my ($self) = @_;
197
198 my $admintalk = $self->{adminstore}->get_client();
199 my $imaptalk = $self->{store}->get_client();
200
201 $admintalk->create("user.otheruser.groupaccess");
202 $self->assert_str_equals('ok',
203 $admintalk->get_last_completion_response());
204
205 $admintalk->setacl("user.otheruser.groupaccess",
206 "group:group co", "lrswipkxtecdn");
207 $self->assert_str_equals('ok',
208 $admintalk->get_last_completion_response());
209
210 if (get_verbose()) {
211 $self->{instance}->run_command(
212 { cyrus => 1, },
213 'cyr_dbtool',
214 "$self->{instance}->{basedir}/conf/mailboxes.db",
215 'twoskip',
216 'show'
217 );
218 }
219
220 my $list = $imaptalk->list("", "*");
221 my @boxes = sort map { $_->[2] } @{$list};
222
223 $self->assert_deep_equals(\@boxes,
224 ['INBOX', 'user.otheruser.groupaccess']);
225 }
226
227 sub do_test_list_order
228 {
229 my ($self) = @_;
230
231 my $admintalk = $self->{adminstore}->get_client();
232 my $imaptalk = $self->{store}->get_client();
233
234 $imaptalk->create("INBOX.zzz");
235 $self->assert_str_equals('ok',
236 $imaptalk->get_last_completion_response());
237
238 $imaptalk->create("INBOX.aaa");
239 $self->assert_str_equals('ok',
240 $imaptalk->get_last_completion_response());
241
242 my %adminfolders = (
243 'user.otheruser.order-user' => 'cassandane',
244 'user.otheruser.order-co' => 'group:group co',
245 'user.otheruser.order-c' => 'group:group c',
246 'user.otheruser.order-o' => 'group:group o',
247 'shared.order-co' => 'group:group co',
248 'shared.order-c' => 'group:group c',
249 'shared.order-o' => 'group:group o',
250 );
251
252 while (my ($folder, $identifier) = each %adminfolders) {
253 $admintalk->create($folder);
254 $self->assert_str_equals('ok',
255 $admintalk->get_last_completion_response(),
256 "created folder $folder successfully");
257
258 $admintalk->setacl($folder, $identifier, "lrswipkxtecdn");
259 $self->assert_str_equals('ok',
260 $admintalk->get_last_completion_response(),
261 "setacl folder $folder for $identifier successfully");
262
263 if ($folder =~ m/^shared/) {
264 # subvert default permissions on shared namespace for
265 # purpose of testing ordering
266 $admintalk->setacl($folder, "anyone", "p");
267 $self->assert_str_equals('ok',
268 $admintalk->get_last_completion_response(),
269 "setacl folder $folder for anyone successfully");
270 }
271 }
272
273 if (get_verbose()) {
274 $self->{instance}->run_command(
275 { cyrus => 1, },
276 'cyr_dbtool',
277 "$self->{instance}->{basedir}/conf/mailboxes.db",
278 'twoskip',
279 'show'
280 );
281 }
282
283 my $list = $imaptalk->list("", "*");
284 my @boxes = map { $_->[2] } @{$list};
285
286 # Note: order is
287 # * mine, alphabetically,
288 # * other users', alphabetically,
289 # * shared, alphabetically
290 # ... which is not the order we created them ;)
291 # Also, the "order-o" folders are not returned, because cassandane
292 # is not a member of that group
293 my @expect = qw(
294 INBOX
295 INBOX.aaa
296 INBOX.zzz
297 user.otheruser.order-c
298 user.otheruser.order-co
299 user.otheruser.order-user
300 );
301 my ($maj, $min) = Cassandane::Instance->get_version();
302 if ($maj > 3 || ($maj == 3 && $min > 4)) {
303 push @expect, qw(shared);
304 }
305 push @expect, qw( shared.order-c shared.order-co );
306 $self->assert_deep_equals(\@boxes, \@expect);
307 }
308
309 sub test_list_order_noracl
310 :NoAltNamespace
311 {
312 my $self = shift;
313 return $self->do_test_list_order(@_);
314 }
315
316 sub test_list_order_racl
317 :ReverseACLs :NoAltNamespace
318 {
319 my $self = shift;
320 return $self->do_test_list_order(@_);
321 }
322
323 sub test_nonadmin_group_mgmt
324 {
325 my ($self) = @_;
326
327 my $talk = $self->{store}->get_client();
328
329 # should have the xusergroups capability
330 $self->assert_not_null($talk->capability()->{xusergroups});
331
332 # group management commands are admin-only
333 $talk->_imap_cmd('GETUSERGROUP', 0, '', 'cassandane');
334 $self->assert_str_equals('no', $talk->get_last_completion_response());
335
336 $talk->_imap_cmd('SETUSERGROUP', 0, '', 'cassandane', 'group:foo');
337 $self->assert_str_equals('no', $talk->get_last_completion_response());
338
339 $talk->_imap_cmd('UNSETUSERGROUP', 0, '', 'cassandane', 'group:foo');
340 $self->assert_str_equals('no', $talk->get_last_completion_response());
341 }
342
343 sub test_admin_getusergroup
344 {
345 my ($self) = @_;
346
347 my $admintalk = $self->{adminstore}->get_client();
348
349 # should have the xusergroups capability
350 $self->assert_not_null($admintalk->capability()->{xusergroups});
351
352 # get a user's groups
353 my $usergroups = $self->imap_getusergroup($admintalk, 'cassandane');
354 $self->assert_not_null($usergroups->{'cassandane'});
355 $self->assert_not_null($usergroups->{'cassandane'}->{'group:group c'});
356 $self->assert_not_null($usergroups->{'cassandane'}->{'group:group co'});
357 $self->assert_null($usergroups->{'cassandane'}->{'group:group o'});
358
359 # get a group's users
360 $usergroups = $self->imap_getusergroup($admintalk, 'group:group co');
361 $self->assert_not_null($usergroups->{'group:group co'});
362 $self->assert_not_null($usergroups->{'group:group co'}->{'cassandane'});
363 $self->assert_not_null($usergroups->{'group:group co'}->{'otheruser'});
364 $self->assert_null($usergroups->{'group:group co'}->{'nobody'});
365 }
366
367 sub test_admin_setusergroup
368 {
369 my ($self) = @_;
370
371 my $admintalk = $self->{adminstore}->get_client();
372
373 # should have the xusergroups capability
374 $self->assert_not_null($admintalk->capability()->{xusergroups});
375
376 # get a user's groups
377 my $usergroups = $self->imap_getusergroup($admintalk, 'cassandane');
378 $self->assert_not_null($usergroups->{'cassandane'});
379 $self->assert_not_null($usergroups->{'cassandane'}->{'group:group c'});
380 $self->assert_not_null($usergroups->{'cassandane'}->{'group:group co'});
381 $self->assert_null($usergroups->{'cassandane'}->{'group:group o'});
382 $self->assert_null($usergroups->{'cassandane'}->{'group:new group'});
383
384 # set membership in a new group
385 $admintalk->_imap_cmd('SETUSERGROUP', 0, '',
386 'cassandane', 'group:new group');
387 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
388
389 # get the groups again, should be in new group now
390 $usergroups = $self->imap_getusergroup($admintalk, 'cassandane');
391 $self->assert_not_null($usergroups->{'cassandane'});
392 $self->assert_not_null($usergroups->{'cassandane'}->{'group:group c'});
393 $self->assert_not_null($usergroups->{'cassandane'}->{'group:group co'});
394 $self->assert_null($usergroups->{'cassandane'}->{'group:group o'});
395 $self->assert_not_null($usergroups->{'cassandane'}->{'group:new group'});
396
397 # get the new group's membership, should contain (only) the user
398 $usergroups = $self->imap_getusergroup($admintalk, 'group:new group');
399 $self->assert_not_null($usergroups->{'group:new group'});
400 $self->assert_not_null($usergroups->{'group:new group'}->{'cassandane'});
401 $self->assert_equals(1, scalar keys %{$usergroups->{'group:new group'}});
402 }
403
404 sub test_admin_unsetusergroup
405 {
406 my ($self) = @_;
407
408 my $admintalk = $self->{adminstore}->get_client();
409
410 # should have the xusergroups capability
411 $self->assert_not_null($admintalk->capability()->{xusergroups});
412
413 # get a user's groups
414 my $usergroups = $self->imap_getusergroup($admintalk, 'cassandane');
415 $self->assert_not_null($usergroups->{'cassandane'});
416 $self->assert_not_null($usergroups->{'cassandane'}->{'group:group c'});
417 $self->assert_not_null($usergroups->{'cassandane'}->{'group:group co'});
418 $self->assert_null($usergroups->{'cassandane'}->{'group:group o'});
419 $self->assert_null($usergroups->{'cassandane'}->{'group:new group'});
420
421 # remove membership from a group
422 $admintalk->_imap_cmd('UNSETUSERGROUP', 0, '',
423 'cassandane', 'group:group c');
424 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
425
426 # get the groups again, shouldn't be in the group anymore
427 $usergroups = $self->imap_getusergroup($admintalk, 'cassandane');
428 $self->assert_not_null($usergroups->{'cassandane'});
429 $self->assert_null($usergroups->{'cassandane'}->{'group:group c'});
430 $self->assert_not_null($usergroups->{'cassandane'}->{'group:group co'});
431 $self->assert_null($usergroups->{'cassandane'}->{'group:group o'});
432
433 # get the group's membership, shouldn't contain the user
434 $usergroups = $self->imap_getusergroup($admintalk, 'group:group c');
435 $self->assert_not_null($usergroups->{'group:group c'});
436 $self->assert_null($usergroups->{'group:group c'}->{'cassandane'});
437 }
438
439 1;