| 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::Deny; |
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
|
| 44 |
use lib '.'; |
| 45 |
use base qw(Cassandane::Cyrus::TestCase); |
| 46 |
use Cassandane::Util::Log; |
| 47 |
|
| 48 |
sub new |
| 49 |
{ |
| 50 |
my $class = shift; |
| 51 |
return $class->SUPER::new({}, @_); |
| 52 |
} |
| 53 |
|
| 54 |
sub set_up |
| 55 |
{ |
| 56 |
my ($self) = @_; |
| 57 |
$self->SUPER::set_up(); |
| 58 |
} |
| 59 |
|
| 60 |
sub tear_down |
| 61 |
{ |
| 62 |
my ($self) = @_; |
| 63 |
$self->SUPER::tear_down(); |
| 64 |
} |
| 65 |
|
| 66 |
sub test_basic |
| 67 |
{ |
| 68 |
my ($self) = @_; |
| 69 |
|
| 70 |
xlog $self, "Test the cyr_deny utility with the imap service"; |
| 71 |
|
| 72 |
# Data thanks to hipsteripsum.me |
| 73 |
my @cases = ({ |
| 74 |
# test default options |
| 75 |
user => 'helvetica', |
| 76 |
opts => [ ], |
| 77 |
can_login => 0, |
| 78 |
},{ |
| 79 |
# test the -s option with our service |
| 80 |
user => 'portland', |
| 81 |
opts => [ '-s', 'imap' ], |
| 82 |
can_login => 0, |
| 83 |
},{ |
| 84 |
# test the -s option with another service |
| 85 |
user => 'stumptown', |
| 86 |
opts => [ '-s', 'godard' ], |
| 87 |
can_login => 1, |
| 88 |
},{ |
| 89 |
# test the -m option |
| 90 |
user => 'mustache', |
| 91 |
opts => [ '-m', 'Bugger off, you' ], |
| 92 |
can_login => 0, |
| 93 |
},{ |
| 94 |
# control case - no cyr_deny command run |
| 95 |
user => 'vegan', |
| 96 |
can_login => 1, |
| 97 |
}); |
| 98 |
|
| 99 |
|
| 100 |
xlog $self, "Create all users"; |
| 101 |
foreach my $case (@cases) |
| 102 |
{ |
| 103 |
$self->{instance}->create_user($case->{user}); |
| 104 |
} |
| 105 |
|
| 106 |
xlog $self, "Running cyr_deny for some users"; |
| 107 |
foreach my $case (@cases) |
| 108 |
{ |
| 109 |
next unless defined $case->{opts}; |
| 110 |
$self->{instance}->run_command({ cyrus => 1 }, |
| 111 |
'cyr_deny', @{$case->{opts}}, $case->{user}); |
| 112 |
} |
| 113 |
|
| 114 |
my $svc = $self->{instance}->get_service('imap'); |
| 115 |
foreach my $case (@cases) |
| 116 |
{ |
| 117 |
xlog $self, "Trying to log in as user $case->{user}"; |
| 118 |
my $store = $svc->create_store(username => $case->{user}); |
| 119 |
if ($case->{can_login}) |
| 120 |
{ |
| 121 |
xlog $self, "Expecting this to succeed"; |
| 122 |
my $talk = $store->get_client(); |
| 123 |
my $r = $talk->status('inbox', [ 'messages' ]); |
| 124 |
$self->assert_deep_equals({ messages => 0 }, $r); |
| 125 |
$talk = undef; |
| 126 |
} |
| 127 |
else |
| 128 |
{ |
| 129 |
xlog $self, "Expecting this to fail"; |
| 130 |
eval { $store->get_client(); }; |
| 131 |
my $exception = $@; |
| 132 |
$self->assert_matches(qr/no - login failed: authorization failure/i, $exception); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
sub test_connected |
| 138 |
{ |
| 139 |
my ($self) = @_; |
| 140 |
|
| 141 |
xlog $self, "Test that cyr_deny shuts down any connected sessions"; |
| 142 |
|
| 143 |
xlog $self, "Create a user"; |
| 144 |
my $user = 'gastropub'; |
| 145 |
$self->{instance}->create_user($user); |
| 146 |
|
| 147 |
xlog $self, "Set up a logged-in client for each of two users"; |
| 148 |
my $cass_talk = $self->{store}->get_client(); |
| 149 |
|
| 150 |
my $svc = $self->{instance}->get_service('imap'); |
| 151 |
my $user_store = $svc->create_store(username => $user); |
| 152 |
my $user_talk = $user_store->get_client(); |
| 153 |
|
| 154 |
xlog $self, "Check that we can run a command in each of the two clients"; |
| 155 |
my $res; |
| 156 |
$res = $cass_talk->status('inbox', [ 'messages' ]); |
| 157 |
$self->assert_deep_equals({ messages => 0 }, $res); |
| 158 |
$res = $user_talk->status('inbox', [ 'messages' ]); |
| 159 |
$self->assert_deep_equals({ messages => 0 }, $res); |
| 160 |
|
| 161 |
$user_talk->clear_response_code('alert'); |
| 162 |
|
| 163 |
xlog $self, "Deny the user"; |
| 164 |
$self->{instance}->run_command({ cyrus => 1 }, 'cyr_deny', $user); |
| 165 |
|
| 166 |
xlog $self, "Check that we can run a command in the unaffected user"; |
| 167 |
$res = $cass_talk->status('inbox', [ 'messages' ]); |
| 168 |
$self->assert_deep_equals({ messages => 0 }, $res); |
| 169 |
|
| 170 |
xlog $self, "Check that the affected user is disconnected"; |
| 171 |
$res = undef; |
| 172 |
# Either is_open will return undef, or die; both of these |
| 173 |
# are good. If it returned 1 we should worry. |
| 174 |
eval { $res = $user_talk->is_open(); }; |
| 175 |
$self->assert_null($res); |
| 176 |
|
| 177 |
# Could do this, but Mail::IMAPTalk drops ALERTs in a BYE response |
| 178 |
# $self->assert_matches(qr/Access to this service has been blocked/i, |
| 179 |
# $user_talk->get_response_code('alert')); |
| 180 |
} |
| 181 |
|
| 182 |
1; |