| 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::Admin; |
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
use Data::Dumper; |
| 44 |
|
| 45 |
use lib '.'; |
| 46 |
use base qw(Cassandane::Cyrus::TestCase); |
| 47 |
use Cassandane::Util::Log; |
| 48 |
use Cassandane::Instance; |
| 49 |
|
| 50 |
sub new |
| 51 |
{ |
| 52 |
my $class = shift; |
| 53 |
my $config = Cassandane::Config::default()->clone(); |
| 54 |
$config->set( imap_admins => 'admin imapadmin' ); |
| 55 |
return $class->SUPER::new({ config => $config, adminstore => 1 }, @_); |
| 56 |
} |
| 57 |
|
| 58 |
sub set_up |
| 59 |
{ |
| 60 |
my ($self) = @_; |
| 61 |
$self->SUPER::set_up(); |
| 62 |
|
| 63 |
my $imap = $self->{instance}->get_service('imap'); |
| 64 |
$self->{imapadminstore} = $imap->create_store(username => 'imapadmin'); |
| 65 |
} |
| 66 |
|
| 67 |
sub tear_down |
| 68 |
{ |
| 69 |
my ($self) = @_; |
| 70 |
|
| 71 |
$self->{imapadminstore}->disconnect(); |
| 72 |
delete $self->{imapadminstore}; |
| 73 |
|
| 74 |
$self->SUPER::tear_down(); |
| 75 |
} |
| 76 |
|
| 77 |
sub test_imap_admins |
| 78 |
{ |
| 79 |
# test whether the imap_admins setting works correctly |
| 80 |
my ($self) = @_; |
| 81 |
|
| 82 |
my $admintalk = $self->{adminstore}->get_client(); |
| 83 |
my $imapadmintalk = $self->{imapadminstore}->get_client(); |
| 84 |
my $talk = $self->{store}->get_client(); |
| 85 |
|
| 86 |
# we should be able to reconstruct as 'admin', because although |
| 87 |
# imap_admins overrides admins, we have 'admin' in imap_admins too |
| 88 |
# (it MUST be there for Cassandane itself to work) |
| 89 |
my $res = $admintalk->_imap_cmd("reconstruct" , 0, {}, "user.cassandane"); |
| 90 |
$self->assert_str_equals('ok', $admintalk->get_last_completion_response()); |
| 91 |
|
| 92 |
# we should not be able to reconstruct as 'cassandane', because |
| 93 |
# reconstruct is an admin-only command |
| 94 |
$res = $talk->_imap_cmd("reconstruct", 0, {}, "user.cassandane"); |
| 95 |
$self->assert_str_equals('no', $talk->get_last_completion_response()); |
| 96 |
$self->assert_matches(qr/permission denied/i, $talk->get_last_error()); |
| 97 |
|
| 98 |
# we should be able to reconstruct as 'imapadmin', because this user |
| 99 |
# is in imap_admins |
| 100 |
$res = $imapadmintalk->_imap_cmd("reconstruct", 0, {}, "user.cassandane"); |
| 101 |
$self->assert_str_equals('ok', $imapadmintalk->get_last_completion_response()); |
| 102 |
} |
| 103 |
|
| 104 |
#Magic word virtdomains in name sets config virtdomains = userid |
| 105 |
sub test_imap_admins_virtdomains |
| 106 |
{ |
| 107 |
# test whether the imap_admins setting works correctly under virtdomains |
| 108 |
my ($self) = @_; |
| 109 |
|
| 110 |
my $domainadmin = '[email protected]'; |
| 111 |
my $defaultdomain = $self->{instance}->{config}->get('defaultdomain') |
| 112 |
// 'internal'; |
| 113 |
my $defdomadmin = "admin\@$defaultdomain"; |
| 114 |
|
| 115 |
$self->{instance}->create_user($domainadmin); |
| 116 |
my $imap = $self->{instance}->get_service('imap'); |
| 117 |
my $domainadminstore = $imap->create_store(username => $domainadmin); |
| 118 |
my $defdomadminstore = $imap->create_store(username => $defdomadmin); |
| 119 |
|
| 120 |
my $admintalk = $self->{adminstore}->get_client(); |
| 121 |
my $imapadmintalk = $self->{imapadminstore}->get_client(); |
| 122 |
my $domainadmintalk = $domainadminstore->get_client(); |
| 123 |
my $defdomadmintalk = $defdomadminstore->get_client(); |
| 124 |
my $talk = $self->{store}->get_client(); |
| 125 |
|
| 126 |
# we should be able to reconstruct as 'admin', because although |
| 127 |
# imap_admins overrides admins, we have 'admin' in imap_admins too |
| 128 |
# (it MUST be there for Cassandane itself to work) |
| 129 |
my $res = $admintalk->_imap_cmd("reconstruct" , 0, {}, "user.cassandane"); |
| 130 |
$self->assert_str_equals('ok', $admintalk->get_last_completion_response()); |
| 131 |
|
| 132 |
# we should not be able to reconstruct as 'cassandane', because |
| 133 |
# reconstruct is an admin-only command |
| 134 |
$res = $talk->_imap_cmd("reconstruct", 0, {}, "user.cassandane"); |
| 135 |
$self->assert_str_equals('no', $talk->get_last_completion_response()); |
| 136 |
$self->assert_matches(qr/permission denied/i, $talk->get_last_error()); |
| 137 |
|
| 138 |
# we should be able to reconstruct as 'imapadmin', because this user |
| 139 |
# is in imap_admins |
| 140 |
$res = $imapadmintalk->_imap_cmd("reconstruct", 0, {}, "user.cassandane"); |
| 141 |
$self->assert_str_equals('ok', |
| 142 |
$imapadmintalk->get_last_completion_response()); |
| 143 |
|
| 144 |
# we MUST NOT be able to reconstruct as '[email protected]', because |
| 145 |
# this user is not in imap_admins, even though bare 'admin' is |
| 146 |
$res = $domainadmintalk->_imap_cmd("reconstruct", 0, {}, "user.cassandane"); |
| 147 |
$self->assert_str_equals('no', |
| 148 |
$domainadmintalk->get_last_completion_response()); |
| 149 |
$self->assert_matches(qr/permission denied/i, |
| 150 |
$domainadmintalk->get_last_error()); |
| 151 |
|
| 152 |
# we should be able to reconstruct as admin@$defaultdomain, because |
| 153 |
# we treat bare username and username@defaultdomain as equivalent |
| 154 |
$res = $defdomadmintalk->_imap_cmd("reconstruct", 0, {}, "user.cassandane"); |
| 155 |
$self->assert_str_equals('ok', |
| 156 |
$defdomadmintalk->get_last_completion_response()); |
| 157 |
} |
| 158 |
|
| 159 |
1; |