Debian Codegraph @

cassandane/Cassandane/Cyrus/Userid.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 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
32 # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
33 # AND FITNESS, IN NO EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE
34 # FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
36 # AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
37 # OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 #
39
40 package Cassandane::Cyrus::Userid;
41 use strict;
42 use warnings;
43
44 use lib '.';
45 use base qw(Cassandane::Cyrus::TestCase);
46 use Cassandane::Util::Log;
47
48 Cassandane::Cyrus::TestCase::magic(NoAutocreate => sub {
49 shift->config_set('autocreate_users' => 'nobody');
50 });
51 Cassandane::Cyrus::TestCase::magic(PopUseACL => sub {
52 shift->config_set('popuseacl' => 'yes');
53 });
54
55
56 sub new
57 {
58 my $class = shift;
59 return $class->SUPER::new({
60 adminstore => 1,
61 services => ['imap', 'pop3']
62 }, @_);
63 }
64
65 sub set_up
66 {
67 my ($self) = @_;
68 $self->SUPER::set_up();
69 }
70
71 sub tear_down
72 {
73 my ($self) = @_;
74 $self->SUPER::tear_down();
75 }
76
77 # Tests userid with dots and unix hierarchy separator
78 sub test_dots_unix
79 :UnixHierarchySep NoAutocreate PopUseACL
80 {
81 my ($self) = @_;
82
83 my $user = 'userid.with.dots';
84 # We will also play a bit with the internal form of this userid
85 (my $user_internal = $user) =~ s/\./^/g;
86
87 # Create user per instance->create_user() - see builds 1170-1176
88 $self->{instance}->create_user($user);
89
90 # There should only be ACLs for (external) userid
91 my $adminclient = $self->{adminstore}->get_client();
92 my $mb = Cassandane::Mboxname->new(
93 config => $self->{instance}->{config},
94 userid => $user
95 )->to_external();
96
97 my %acls = $adminclient->getacl($mb);
98 $self->assert(defined($acls{$user}));
99 $self->assert(!defined($acls{$user_internal}));
100
101
102 # User should be able to login and enter its INBOX
103 my $store = $self->{instance}->get_service('imap')->create_store(username => $user);
104 my $client = $store->get_client();
105 $client->select('INBOX');
106 $self->assert_str_equals('ok', $client->get_last_completion_response());
107 $store->disconnect();
108
109 # Same thing in POP
110 $store = $self->{instance}->get_service('pop3')->create_store(username => $user);
111 $client = $store->get_client();
112 $store->disconnect();
113
114
115 # Internal userid shall not be able to enter its INBOX
116 $store = $self->{instance}->get_service('imap')->create_store(username => $user_internal);
117 $client = $store->get_client();
118 $client->select('INBOX');
119 $self->assert_str_equals('no', $client->get_last_completion_response());
120 $store->disconnect();
121
122 # Same thing in POP
123 $store = $self->{instance}->get_service('pop3')->create_store(username => $user_internal);
124 {
125 # shut up
126 local $SIG{__DIE__};
127 local $SIG{__WARN__} = sub { 1 };
128
129 eval { $client = $store->get_client(); };
130 my $Err = $@;
131 $store->disconnect();
132 $self->assert_matches(qr/Cannot login via POP3/, $Err);
133 };
134
135
136 # We should be able to set ACLs for internal userid
137 $adminclient->setacl($mb, $user_internal => 'lrswipkxtecd')
138 or die "Cannot setacl for $mb: $@";
139 %acls = $adminclient->getacl($mb);
140 $self->assert(defined($acls{$user_internal}));
141
142 # XXX - In an ideal world, internal userid should still not be able to enter
143 # its INBOX. However, since we set its rights, it will be able to access the
144 # external userid mailbox: the '^' character is not forbidden, and since it
145 # is converted to itself in internal form, the code is such that both
146 # external and internal userid have the same mailbox name.
147
148
149 # We should be able to delete ACLs for external/internal userid
150 # But external one, as mailbox owner, should still keep some
151 $adminclient->deleteacl($mb, $user)
152 or die "Cannot deleteacl for $mb: $@";
153 $adminclient->deleteacl($mb, $user_internal)
154 or die "Cannot deleteacl for $mb: $@";
155 %acls = $adminclient->getacl($mb);
156 $self->assert(defined($acls{$user}));
157 $self->assert(!defined($acls{$user_internal}));
158 }
159
160 1;