| 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::Sieve; |
| 41 |
use Net::CalDAVTalk 0.12; |
| 42 |
use strict; |
| 43 |
use warnings; |
| 44 |
use IO::File; |
| 45 |
use version; |
| 46 |
use utf8; |
| 47 |
use File::Temp qw/tempfile/; |
| 48 |
use DateTime; |
| 49 |
use Date::Parse; |
| 50 |
|
| 51 |
use lib '.'; |
| 52 |
use base qw(Cassandane::Cyrus::TestCase); |
| 53 |
use Cassandane::Util::Log; |
| 54 |
use Encode qw(decode); |
| 55 |
use MIME::Base64 qw(encode_base64); |
| 56 |
use Data::Dumper; |
| 57 |
|
| 58 |
sub new |
| 59 |
{ |
| 60 |
my $class = shift; |
| 61 |
my $config = Cassandane::Config->default()->clone(); |
| 62 |
|
| 63 |
my ($maj, $min) = Cassandane::Instance->get_version(); |
| 64 |
if ($maj == 3 && $min == 0) { |
| 65 |
# need to explicitly add 'body' to sieve_extensions for 3.0 |
| 66 |
$config->set(sieve_extensions => |
| 67 |
"fileinto reject vacation vacation-seconds imap4flags notify " . |
| 68 |
"envelope relational regex subaddress copy date index " . |
| 69 |
"imap4flags mailbox mboxmetadata servermetadata variables " . |
| 70 |
"body"); |
| 71 |
} |
| 72 |
elsif ($maj < 3) { |
| 73 |
# also for 2.5 (the earliest Cyrus that Cassandane can test) |
| 74 |
$config->set(sieve_extensions => |
| 75 |
"fileinto reject vacation vacation-seconds imap4flags notify " . |
| 76 |
"envelope relational regex subaddress copy date index " . |
| 77 |
"imap4flags body"); |
| 78 |
} |
| 79 |
$config->set(sievenotifier => 'mailto'); |
| 80 |
$config->set(caldav_realm => 'Cassandane'); |
| 81 |
$config->set(httpmodules => ['caldav', 'carddav', 'jmap']); |
| 82 |
$config->set(calendar_user_address_set => 'example.com'); |
| 83 |
$config->set(httpallowcompress => 'no'); |
| 84 |
$config->set(caldav_historical_age => -1); |
| 85 |
$config->set(icalendar_max_size => 100000); |
| 86 |
$config->set(virtdomains => 'no'); |
| 87 |
$config->set(jmap_nonstandard_extensions => 'yes'); |
| 88 |
$config->set(conversations => 'yes'); |
| 89 |
|
| 90 |
my $self = $class->SUPER::new({ |
| 91 |
config => $config, |
| 92 |
deliver => 1, |
| 93 |
jmap => 1, |
| 94 |
services => [ 'imap', 'sieve' ], |
| 95 |
adminstore => 1, |
| 96 |
}, @_); |
| 97 |
|
| 98 |
$self->needs('component', 'sieve'); |
| 99 |
return $self; |
| 100 |
} |
| 101 |
|
| 102 |
sub set_up |
| 103 |
{ |
| 104 |
my ($self) = @_; |
| 105 |
$self->SUPER::set_up(); |
| 106 |
if ($self->{jmap}) { |
| 107 |
$self->{jmap}->DefaultUsing([ |
| 108 |
'urn:ietf:params:jmap:core', |
| 109 |
'urn:ietf:params:jmap:mail', |
| 110 |
'urn:ietf:params:jmap:calendars', |
| 111 |
'urn:ietf:params:jmap:principals', |
| 112 |
'urn:ietf:params:jmap:calendars:preferences', |
| 113 |
'https://cyrusimap.org/ns/jmap/calendars', |
| 114 |
'https://cyrusimap.org/ns/jmap/mail', |
| 115 |
'https://cyrusimap.org/ns/jmap/debug', |
| 116 |
]); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
sub tear_down |
| 121 |
{ |
| 122 |
my ($self) = @_; |
| 123 |
$self->SUPER::tear_down(); |
| 124 |
} |
| 125 |
|
| 126 |
sub read_errors |
| 127 |
{ |
| 128 |
my ($filename) = @_; |
| 129 |
|
| 130 |
my @errors; |
| 131 |
if ( -f $filename ) |
| 132 |
{ |
| 133 |
open FH, '<', $filename |
| 134 |
or die "Cannot open $filename for reading: $!"; |
| 135 |
@errors = readline(FH); |
| 136 |
close FH; |
| 137 |
if (get_verbose) |
| 138 |
{ |
| 139 |
xlog "errors: "; |
| 140 |
map { xlog $_ } @errors; |
| 141 |
} |
| 142 |
# Hack to remove spurious junk generated when |
| 143 |
# running coveraged code under ggcov-run |
| 144 |
@errors = grep { ! m/libggcov:/ && ! m/profiling:/ } @errors; |
| 145 |
} |
| 146 |
return @errors; |
| 147 |
} |
| 148 |
|
| 149 |
sub compile_sievec |
| 150 |
{ |
| 151 |
my ($self, $name, $script) = @_; |
| 152 |
|
| 153 |
my $basedir = $self->{instance}->{basedir}; |
| 154 |
|
| 155 |
xlog $self, "Checking preconditions for compiling sieve script $name"; |
| 156 |
|
| 157 |
$self->assert_not_file_test("$basedir/$name.script", '-f'); |
| 158 |
$self->assert_not_file_test("$basedir/$name.bc", '-f'); |
| 159 |
$self->assert_not_file_test("$basedir/$name.errors", '-f'); |
| 160 |
|
| 161 |
open(FH, '>', "$basedir/$name.script") |
| 162 |
or die "Cannot open $basedir/$name.script for writing: $!"; |
| 163 |
print FH $script; |
| 164 |
close(FH); |
| 165 |
|
| 166 |
xlog $self, "Running sievec on script $name"; |
| 167 |
my $result = $self->{instance}->run_command( |
| 168 |
{ |
| 169 |
cyrus => 1, |
| 170 |
redirects => { stderr => "$basedir/$name.errors" }, |
| 171 |
handlers => { |
| 172 |
exited_normally => sub { return 'success'; }, |
| 173 |
exited_abnormally => sub { return 'failure'; }, |
| 174 |
}, |
| 175 |
}, |
| 176 |
"sievec", "$basedir/$name.script", "$basedir/$name.bc"); |
| 177 |
|
| 178 |
# Read the errors file in @errors |
| 179 |
my (@errors) = read_errors("$basedir/$name.errors"); |
| 180 |
|
| 181 |
if ($result eq 'success') |
| 182 |
{ |
| 183 |
xlog $self, "Checking that sievec wrote the output .bc file"; |
| 184 |
$self->assert_file_test("$basedir/$name.bc", '-f'); |
| 185 |
xlog $self, "Checking that sievec didn't write anything to stderr"; |
| 186 |
$self->assert_equals(0, scalar(@errors)); |
| 187 |
} |
| 188 |
elsif ($result eq 'failure') |
| 189 |
{ |
| 190 |
xlog $self, "Checking that sievec didn't write the output .bc file"; |
| 191 |
$self->assert_not_file_test("$basedir/$name.bc", '-f'); |
| 192 |
} |
| 193 |
|
| 194 |
return ($result, join("\n", @errors)); |
| 195 |
} |
| 196 |
|
| 197 |
sub compile_timsieved |
| 198 |
{ |
| 199 |
my ($self, $name, $script) = @_; |
| 200 |
|
| 201 |
my $basedir = $self->{instance}->{basedir}; |
| 202 |
my $bindir = $self->{instance}->{cyrus_destdir} . |
| 203 |
$self->{instance}->{cyrus_prefix} . '/bin'; |
| 204 |
my $srv = $self->{instance}->get_service('sieve'); |
| 205 |
|
| 206 |
xlog $self, "Checking preconditions for compiling sieve script $name"; |
| 207 |
|
| 208 |
$self->assert_not_file_test("$basedir/$name.script", '-f'); |
| 209 |
$self->assert_not_file_test("$basedir/$name.errors", '-f'); |
| 210 |
|
| 211 |
open(FH, '>', "$basedir/$name.script") |
| 212 |
or die "Cannot open $basedir/$name.script for writing: $!"; |
| 213 |
print FH $script; |
| 214 |
close(FH); |
| 215 |
|
| 216 |
if (! -f "$basedir/sieve.passwd" ) |
| 217 |
{ |
| 218 |
open(FH, '>', "$basedir/sieve.passwd") |
| 219 |
or die "Cannot open $basedir/sieve.passwd for writing: $!"; |
| 220 |
print FH "\ntestpw\n"; |
| 221 |
close(FH); |
| 222 |
} |
| 223 |
|
| 224 |
xlog $self, "Running installsieve on script $name"; |
| 225 |
my $result = $self->{instance}->run_command({ |
| 226 |
redirects => { |
| 227 |
# No cyrus => 1 as installsieve is a Perl |
| 228 |
# script which doesn't need Valgrind and |
| 229 |
# doesn't understand the Cyrus -C option |
| 230 |
stdin => "$basedir/sieve.passwd", |
| 231 |
stderr => "$basedir/$name.errors" |
| 232 |
}, |
| 233 |
handlers => { |
| 234 |
exited_normally => sub { return 'success'; }, |
| 235 |
exited_abnormally => sub { return 'failure'; }, |
| 236 |
}, |
| 237 |
}, |
| 238 |
"$bindir/installsieve", |
| 239 |
"-i", "$basedir/$name.script", |
| 240 |
"-u", "cassandane", |
| 241 |
$srv->host() . ":" . $srv->port()); |
| 242 |
|
| 243 |
# Read the errors file in @errors |
| 244 |
my (@errors) = read_errors("$basedir/$name.errors"); |
| 245 |
|
| 246 |
if ($result eq 'success') |
| 247 |
{ |
| 248 |
xlog $self, "Checking that installsieve didn't write anything to stderr"; |
| 249 |
$self->assert_equals(0, scalar(@errors)); |
| 250 |
} |
| 251 |
|
| 252 |
return ($result, join("\n", @errors)); |
| 253 |
} |
| 254 |
|
| 255 |
sub compile_sieve_script |
| 256 |
{ |
| 257 |
my ($self, $name, $script) = @_; |
| 258 |
|
| 259 |
my $meth = 'compile_' . $self->{compile_method}; |
| 260 |
return $self->$meth($name, $script); |
| 261 |
} |
| 262 |
|
| 263 |
sub badscript_common |
| 264 |
{ |
| 265 |
my ($self) = @_; |
| 266 |
|
| 267 |
my $res; |
| 268 |
my $errs; |
| 269 |
|
| 270 |
($res, $errs) = $self->compile_sieve_script('badrequire', |
| 271 |
"require [\"nonesuch\"];\n"); |
| 272 |
$self->assert_str_equals('failure', $res); |
| 273 |
$self->assert_matches(qr/Unsupported feature.*nonesuch/, $errs); |
| 274 |
|
| 275 |
($res, $errs) = $self->compile_sieve_script('badreject1', |
| 276 |
"reject \"foo\"\n"); |
| 277 |
$self->assert_str_equals('failure', $res); |
| 278 |
$self->assert_matches(qr/reject.*MUST be enabled/, $errs); |
| 279 |
|
| 280 |
($res, $errs) = $self->compile_sieve_script('badreject2', |
| 281 |
"require [\"reject\"];\nreject\n"); |
| 282 |
$self->assert_str_equals('failure', $res); |
| 283 |
$self->assert_matches(qr/syntax error/, $errs); |
| 284 |
|
| 285 |
($res, $errs) = $self->compile_sieve_script('badreject3', |
| 286 |
"require [\"reject\"];\nreject 42\n"); |
| 287 |
$self->assert_str_equals('failure', $res); |
| 288 |
$self->assert_matches(qr/syntax error/, $errs); |
| 289 |
|
| 290 |
# TODO: test UTF-8 verification of the string parameter |
| 291 |
|
| 292 |
($res, $errs) = $self->compile_sieve_script('badfileinto1', |
| 293 |
"fileinto \"foo\"\n"); |
| 294 |
$self->assert_str_equals('failure', $res); |
| 295 |
$self->assert_matches(qr/fileinto.*MUST be enabled/, $errs); |
| 296 |
|
| 297 |
($res, $errs) = $self->compile_sieve_script('badfileinto2', |
| 298 |
"require [\"fileinto\"];\nfileinto\n"); |
| 299 |
$self->assert_str_equals('failure', $res); |
| 300 |
$self->assert_matches(qr/syntax error/, $errs); |
| 301 |
|
| 302 |
($res, $errs) = $self->compile_sieve_script('badfileinto3', |
| 303 |
"require [\"fileinto\"];\nfileinto 42\n"); |
| 304 |
$self->assert_str_equals('failure', $res); |
| 305 |
$self->assert_matches(qr/syntax error/, $errs); |
| 306 |
|
| 307 |
($res, $errs) = $self->compile_sieve_script('badfileinto4', |
| 308 |
"require [\"fileinto\"];\nfileinto :copy \"foo\"\n"); |
| 309 |
$self->assert_str_equals('failure', $res); |
| 310 |
$self->assert_matches(qr/copy.*MUST be enabled/, $errs); |
| 311 |
|
| 312 |
($res, $errs) = $self->compile_sieve_script('badfileinto5', |
| 313 |
"require [\"fileinto\",\"copy\"];\nfileinto \"foo\"\n"); |
| 314 |
$self->assert_str_equals('failure', $res); |
| 315 |
$self->assert_matches(qr/syntax error/, $errs); |
| 316 |
|
| 317 |
($res, $errs) = $self->compile_sieve_script('badfileinto6', |
| 318 |
"require [\"fileinto\",\"copy\"];\nfileinto :copy \"foo\"\n"); |
| 319 |
$self->assert_str_equals('failure', $res); |
| 320 |
$self->assert_matches(qr/syntax error/, $errs); |
| 321 |
|
| 322 |
($res, $errs) = $self->compile_sieve_script('badchar1', |
| 323 |
"require [\"fileinto\"];\n☃;\nfileinto \"foo\";\n"); |
| 324 |
$self->assert_str_equals('failure', $res); |
| 325 |
$self->assert_matches(qr/non-ASCII/, $errs); |
| 326 |
|
| 327 |
($res, $errs) = $self->compile_sieve_script('goodfileinto7', |
| 328 |
"require [\"fileinto\",\"copy\"];\nfileinto \"foo\";\n"); |
| 329 |
$self->assert_str_equals('success', $res); |
| 330 |
|
| 331 |
($res, $errs) = $self->compile_sieve_script('goodfileinto8', |
| 332 |
"require [\"fileinto\",\"copy\"];\nfileinto :copy \"foo\";\n"); |
| 333 |
$self->assert_str_equals('success', $res); |
| 334 |
|
| 335 |
my $badregex1 = << 'EOF'; |
| 336 |
require ["regex"]; |
| 337 |
if header :regex "Subject" "Message (x)?(.*" { |
| 338 |
stop; |
| 339 |
} |
| 340 |
EOF |
| 341 |
($res, $errs) = $self->compile_sieve_script('badregex1', $badregex1); |
| 342 |
$self->assert_str_equals('failure', $res); |
| 343 |
$self->assert_matches(qr/unbalanced/, $errs); |
| 344 |
|
| 345 |
# TODO: test UTF-8 verification of the string parameter |
| 346 |
} |
| 347 |
|
| 348 |
# Disabled for now - addflag does not work |
| 349 |
# on shared mailboxes in 2.5. |
| 350 |
# https://github.com/cyrusimap/cyrus-imapd/issues/1453 |
| 351 |
sub XXXtest_shared_delivery_addflag |
| 352 |
:Admin |
| 353 |
:needs_component_sieve |
| 354 |
{ |
| 355 |
my ($self) = @_; |
| 356 |
|
| 357 |
xlog $self, "Testing setting a flag on a sieve script on a"; |
| 358 |
xlog $self, "shared folder. Bug 3617 / issue #1453"; |
| 359 |
|
| 360 |
my $imaptalk = $self->{store}->get_client(); |
| 361 |
$self->{store}->set_fetch_attributes(qw(uid flags)); |
| 362 |
|
| 363 |
xlog $self, "Create the target folder"; |
| 364 |
my $admintalk = $self->{adminstore}->get_client(); |
| 365 |
my $target = "shared.departments.cis"; |
| 366 |
$admintalk->create($target) |
| 367 |
or die "Cannot create folder \"$target\": $@"; |
| 368 |
$admintalk->setacl($target, admin => 'lrswipkxtecda') |
| 369 |
or die "Cannot setacl for \"$target\": $@"; |
| 370 |
$admintalk->setacl($target, 'cassandane' => 'lrswipkxtecd') |
| 371 |
or die "Cannot setacl for \"$target\": $@"; |
| 372 |
$admintalk->setacl($target, 'anyone' => 'p') |
| 373 |
or die "Cannot setacl for \"$target\": $@"; |
| 374 |
|
| 375 |
xlog $self, "Install the sieve script"; |
| 376 |
my $scriptname = 'cosbySweater'; |
| 377 |
$self->{instance}->install_sieve_script(<<EOF |
| 378 |
require ["imap4flags"]; |
| 379 |
if header :comparator "i;ascii-casemap" :is "Subject" "quinoa" { |
| 380 |
addflag "\\\\Flagged"; |
| 381 |
keep; |
| 382 |
stop; |
| 383 |
} |
| 384 |
EOF |
| 385 |
, username => undef, |
| 386 |
name => $scriptname); |
| 387 |
|
| 388 |
xlog $self, "Tell the folder to run the sieve script"; |
| 389 |
$admintalk->setmetadata($target, "/shared/vendor/cmu/cyrus-imapd/sieve", $scriptname) |
| 390 |
or die "Cannot set metadata: $@"; |
| 391 |
|
| 392 |
xlog $self, "Deliver a message"; |
| 393 |
my $msg1 = $self->{gen}->generate(subject => "quinoa"); |
| 394 |
$self->{instance}->deliver($msg1, users => [], folder => $target); |
| 395 |
|
| 396 |
xlog $self, "Check that the message made it to target"; |
| 397 |
$self->{store}->set_folder($target); |
| 398 |
$msg1->set_attribute(flags => [ '\\Recent', '\\Flagged' ]); |
| 399 |
$self->check_messages({ 1 => $msg1 }, check_guid => 0); |
| 400 |
} |
| 401 |
|
| 402 |
use Cassandane::Tiny::Loader 'tiny-tests/Sieve'; |
| 403 |
|
| 404 |
1; |