Debian Codegraph @

cassandane/Cassandane/Cyrus/LDAP.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2018 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 # XXX Most of these tests are tagged with :min_version_3_0_8, as
41 # XXX the architecture used for testing LDAP depends on the fix
42 # XXX for https://github.com/cyrusimap/cyrus-imapd/issues/2282
43
44 package Cassandane::Cyrus::LDAP;
45 use strict;
46 use warnings;
47 use Cwd qw(realpath);
48 use Data::Dumper;
49
50 use lib '.';
51 use base qw(Cassandane::Cyrus::TestCase);
52 use Cassandane::Util::Log;
53
54 sub new
55 {
56 my ($class, @args) = @_;
57
58 my $config = Cassandane::Config->default()->clone();
59 $config->set(
60 ldap_base => "o=cyrus",
61 ldap_group_base => "ou=groups,o=cyrus",
62 ldap_domain_base_dn => "ou=domains,o=cyrus",
63 ldap_user_attribute => "uid",
64 ldap_member_attribute => "memberof",
65 ldap_group_hasmember_attribute => "hasmember",
66 ldap_sasl => "no",
67 auth_mech => 'pts',
68 pts_module => 'ldap',
69 ptloader_sock => '@basedir@/conf/ptsock',
70 );
71
72 my $self = $class->SUPER::new({
73 config => $config,
74 adminstore => 1,
75 services => [qw( imap ptloader )],
76 start_instances => 0,
77 }, @args);
78
79 $self->needs('dependency', 'ldap');
80 return $self;
81 }
82
83 sub set_up
84 {
85 my ($self) = @_;
86
87 $self->SUPER::set_up();
88
89 $self->{ldapport} = Cassandane::PortManager::alloc("localhost");
90
91 $self->{instance}->{config}->set(
92 ldap_uri => "ldap://localhost:$self->{ldapport}/",
93 );
94
95 # arrange for the fakeldapd to be started
96 my ($maj, $min) = Cassandane::Instance->get_version($self->{installation});
97 if ($maj < 3 || ($maj == 3 && $min < 4)) {
98 $self->{instance}->add_start(
99 name => 'fakeldapd',
100 argv => [
101 realpath('utils/fakeldapd'),
102 '-p', $self->{ldapport},
103 '-l', realpath('data/directory.ldif'),
104 ],
105 );
106 }
107 elsif (not exists $self->{daemons}->{fakeldapd}) {
108 $self->{instance}->add_daemon(
109 name => 'fakeldapd',
110 argv => [
111 realpath('utils/fakeldapd'),
112 '-p', $self->{ldapport},
113 '-l', realpath('data/directory.ldif'),
114 ],
115 wait => 'y',
116 );
117 }
118
119 $self->_start_instances();
120 $self->{instance}->create_user("otheruser");
121 }
122
123 sub tear_down
124 {
125 my ($self) = @_;
126
127 $self->SUPER::tear_down();
128 }
129
130 sub test_alternate_ptscache_db_path
131 :min_version_3_0_8 :AltPTSDBPath
132 {
133 my ($self) = @_;
134
135 # just interact with the store, and it should work
136 my $admintalk = $self->{adminstore}->get_client();
137
138 $admintalk->list('user.cassandane', '*');
139 $self->assert_str_equals('ok',
140 $admintalk->get_last_completion_response());
141
142 my $confdir = $self->{instance}->{basedir} . "/conf";
143 $self->assert_file_test($confdir . "/non-default-ptscache.db");
144 $self->assert_not_file_test($confdir . "/ptclient/ptscache.db");
145 }
146
147 sub test_setacl_groupid
148 :min_version_3_0_8
149 {
150 my ($self) = @_;
151
152 my $admintalk = $self->{adminstore}->get_client();
153
154 $admintalk->create("user.cassandane.groupid");
155 $self->assert_str_equals('ok',
156 $admintalk->get_last_completion_response());
157
158 $admintalk->setacl("user.cassandane.groupid",
159 "group:foo",
160 "lrswipkxtecdan");
161 $self->assert_str_equals('ok',
162 $admintalk->get_last_completion_response());
163 }
164
165 sub test_setacl_groupid_spaces
166 :min_version_3_0_8
167 {
168 my ($self) = @_;
169
170 my $admintalk = $self->{adminstore}->get_client();
171
172 $admintalk->create("user.cassandane.groupid_spaces");
173 $self->assert_str_equals('ok',
174 $admintalk->get_last_completion_response());
175
176 $admintalk->setacl("user.cassandane.groupid_spaces",
177 "group:this group name has spaces",
178 "lrswipkxtecdan");
179 $self->assert_str_equals('ok',
180 $admintalk->get_last_completion_response());
181
182 my $data = $admintalk->getacl("user.cassandane.groupid_spaces");
183 $self->assert_str_equals('ok',
184 $admintalk->get_last_completion_response());
185
186 $self->assert(scalar @{$data} % 2 == 0);
187 my %acl = @{$data};
188 $self->assert_str_equals($acl{"group:this group name has spaces"},
189 "lrswipkxtecdan");
190
191 $admintalk->select("user.cassandane.groupid_spaces");
192 $self->assert_str_equals('ok',
193 $admintalk->get_last_completion_response());
194 }
195
196 sub test_list_groupaccess_noracl
197 :min_version_3_0_8 :NoAltNamespace
198 {
199 my ($self) = @_;
200
201 my $admintalk = $self->{adminstore}->get_client();
202 my $imaptalk = $self->{store}->get_client();
203
204 $admintalk->create("user.otheruser.groupaccess");
205 $self->assert_str_equals('ok',
206 $admintalk->get_last_completion_response());
207
208 $admintalk->setacl("user.otheruser.groupaccess",
209 "group:group co", "lrswipkxtecdan");
210 $self->assert_str_equals('ok',
211 $admintalk->get_last_completion_response());
212
213 my $list = $imaptalk->list("", "*");
214 my @boxes = sort map { $_->[2] } @{$list};
215
216 $self->assert_deep_equals(\@boxes,
217 ['INBOX', 'user.otheruser.groupaccess']);
218 }
219
220 sub test_list_groupaccess_racl
221 :ReverseACLs :min_version_3_1 :NoAltNamespace :Conversations
222 {
223 my ($self) = @_;
224
225 my $admintalk = $self->{adminstore}->get_client();
226 my $imaptalk = $self->{store}->get_client();
227
228 $admintalk->create("user.otheruser.groupaccess");
229 $self->assert_str_equals('ok',
230 $admintalk->get_last_completion_response());
231
232 my $precounters = $self->{store}->get_counters();
233
234 $admintalk->setacl("user.otheruser.groupaccess",
235 "group:group co", "lrswipkxtecdn");
236 $self->assert_str_equals('ok',
237 $admintalk->get_last_completion_response());
238
239 my $postcounters = $self->{store}->get_counters();
240 $self->assert_num_not_equals($precounters->{raclmodseq}, $postcounters->{raclmodseq}, "RACL modseq changed");
241
242 if (get_verbose()) {
243 $self->{instance}->run_command(
244 { cyrus => 1, },
245 'cyr_dbtool',
246 "$self->{instance}->{basedir}/conf/mailboxes.db",
247 'twoskip',
248 'show'
249 );
250 }
251
252 my $list = $imaptalk->list("", "*");
253 my @boxes = sort map { $_->[2] } @{$list};
254
255 $self->assert_deep_equals(\@boxes,
256 ['INBOX', 'user.otheruser.groupaccess']);
257 }
258
259 sub do_test_list_order
260 {
261 my ($self) = @_;
262
263 my $admintalk = $self->{adminstore}->get_client();
264 my $imaptalk = $self->{store}->get_client();
265
266 $imaptalk->create("INBOX.zzz");
267 $self->assert_str_equals('ok',
268 $imaptalk->get_last_completion_response());
269
270 $imaptalk->create("INBOX.aaa");
271 $self->assert_str_equals('ok',
272 $imaptalk->get_last_completion_response());
273
274 my %adminfolders = (
275 'user.otheruser.order-user' => 'cassandane',
276 'user.otheruser.order-co' => 'group:group co',
277 'user.otheruser.order-c' => 'group:group c',
278 'user.otheruser.order-o' => 'group:group o',
279 'shared.order-co' => 'group:group co',
280 'shared.order-c' => 'group:group c',
281 'shared.order-o' => 'group:group o',
282 );
283
284 while (my ($folder, $identifier) = each %adminfolders) {
285 $admintalk->create($folder);
286 $self->assert_str_equals('ok',
287 $admintalk->get_last_completion_response(),
288 "created folder $folder successfully");
289
290 $admintalk->setacl($folder, $identifier, "lrswipkxtecdn");
291 $self->assert_str_equals('ok',
292 $admintalk->get_last_completion_response(),
293 "setacl folder $folder for $identifier successfully");
294
295 if ($folder =~ m/^shared/) {
296 # subvert default permissions on shared namespace for
297 # purpose of testing ordering
298 $admintalk->setacl($folder, "anyone", "p");
299 $self->assert_str_equals('ok',
300 $admintalk->get_last_completion_response(),
301 "setacl folder $folder for anyone successfully");
302 }
303 }
304
305 if (get_verbose()) {
306 $self->{instance}->run_command(
307 { cyrus => 1, },
308 'cyr_dbtool',
309 "$self->{instance}->{basedir}/conf/mailboxes.db",
310 'twoskip',
311 'show'
312 );
313 }
314
315 my $list = $imaptalk->list("", "*");
316 my @boxes = map { $_->[2] } @{$list};
317
318 # Note: order is
319 # * mine, alphabetically,
320 # * other users', alphabetically,
321 # * shared, alphabetically
322 # ... which is not the order we created them ;)
323 # Also, the "order-o" folders are not returned, because cassandane
324 # is not a member of that group
325 my @expect = qw(
326 INBOX
327 INBOX.aaa
328 INBOX.zzz
329 user.otheruser.order-c
330 user.otheruser.order-co
331 user.otheruser.order-user
332 );
333 my ($maj, $min) = Cassandane::Instance->get_version();
334 if ($maj > 3 || ($maj == 3 && $min > 4)) {
335 push @expect, qw(shared);
336 }
337 push @expect, qw( shared.order-c shared.order-co );
338 $self->assert_deep_equals(\@boxes, \@expect);
339 }
340
341 sub test_list_order_noracl
342 :min_version_3_0_8 :NoAltNamespace
343 {
344 my $self = shift;
345 return $self->do_test_list_order(@_);
346 }
347
348 sub test_list_order_racl
349 :ReverseACLs :min_version_3_1 :NoAltNamespace
350 {
351 my $self = shift;
352 return $self->do_test_list_order(@_);
353 }
354
355 1;