1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
package JWebmail::Model::ReadMails::QMailAuthuser;
use v5.24;
use warnings;
use utf8;
use JWebmail::Config 'MAILDIR_EXTRACTOR';
use IPC::Open2;
use JSON::PP 'decode_json';
use Params::Check 'check';
use Role::Tiny::With;
use Scalar::Util 'blessed';
use namespace::clean;
with 'JWebmail::Model::ReadMails::Role';
package JWebmail::Model::ReadMails::QMailAuthuser::Error {
use Data::Dumper 'Dumper';
use overload
'""' => \&to_string,
bool => sub {1};
sub new {
my $cls = shift;
my $msg = shift;
my $self = {message => $msg};
$self->{data} = $_[0] if @_ == 1;
$self->{data} = [@_] if @_ > 1;
return bless $self, $cls;
}
sub to_string {
my $self = shift;
my $verbose = 1; #shift;
if ($verbose && defined $self->{data}) {
my $errstr = Data::Dumper->new([$self->{data}])->Terse(1)->Indent(0)->Quotekeys(0)->Dump;
return "$self->{message}: $errstr";
}
else {
return $self->{message};
}
}
sub throw {
my $cls = shift;
my $msg = shift;
my $self = $cls->new($msg, @_);
$self->_trace;
die $self;
}
# taken from Mojo::Exception
sub _trace {
my ($self, $start) = (shift, shift // 1);
my @frames;
while (my @trace = caller($start++)) { push @frames, \@trace }
$self->{frames} = \@frames;
}
sub message { shift->{message} }
sub data { shift->{data} }
sub frames { shift->{frames} }
}
my $QMailAuthuserCheck = {
virtual_user => {required => 1},
mailbox_path => {required => 1},
authenticator => {required => 1},
};
sub new {
my $cls = shift;
my $self = @_ == 1 ? $_[0] : {@_};
if (my $pkg = blessed $cls) {
$self = {%$cls, %$self};
$cls = $pkg;
}
local $Params::Check::ALLOW_UNKNOWN = 1;
local $Params::Check::ONLY_ALLOW_DEFINED = 1;
local $Params::Check::WARNINGS_FATAL = 1;
my $s = check($QMailAuthuserCheck, $self)
or die __PACKAGE__ . " creation failed!";
$s->{prog} = MAILDIR_EXTRACTOR;
return bless $s, $cls;
}
sub verify_user {
my $self = shift;
my $auth = shift;
return eval { $self->build_and_run($auth, 'auth'); 1 }
|| do {
my $e = $@;
my $rc = eval { $e->data->{return_code} };
if ($rc == 1) {
return '';
}
else {
die $e;
}
};
}
sub read_headers_for {
my $self = shift;
my $auth = shift;
my %h = @_;
my ($folder, $start, $end, $sort) = @h{qw(folder start end sort)};
return $self->build_and_run($auth, 'list', [$folder, $start, $end, $sort]);
}
sub count {
my $self = shift;
my ($auth, $folder) = @_;
return $self->build_and_run($auth, 'count', [$folder]);
}
sub show {
my $self = shift;
my ($auth, $folder, $mid) = @_;
return $self->build_and_run($auth, 'read', [$folder, $mid]);
}
sub raw {
my $self = shift;
my ($auth, $folder, $mid, $path) = @_;
return $self->build_and_run($auth, 'raw', [$folder, $mid, $path//'']);
}
sub search {
my $self = shift;
my ($auth, $pattern, $folder) = @_;
return $self->build_and_run($auth, 'search', [$pattern, $folder]);
}
sub folders {
my $self = shift;
my ($auth) = @_;
my $res = $self->build_and_run($auth, 'folders');
unshift @$res, '' if ref $res eq 'ARRAY';
return $res;
}
sub move {
my $self = shift;
my ($auth, $mid, $from_f, $to_f) = @_;
my $_resp = $self->build_and_run($auth, 'move', [$mid, $from_f, $to_f]);
return 1;
}
sub build_arg {
my $self = shift;
my ($user_mail_addr, $mode, $args) = @_;
return $self->{authenticator} . ' true 3<&0'
if $mode eq 'auth';
my ($user_name) = $user_mail_addr =~ /(\w*)@/;
return $self->{authenticator} . ' '
. join(' ', map { my $x = s/(['\\])/\\$1/gr; "'$x'" } ($self->{prog}, $self->{mailbox_path}, $self->{virtual_user}, $user_name, $mode, @$args))
. ' 3<&0';
}
sub start_qmauth {
my $self = shift;
my ($auth, $mode, $args) = @_;
my $exec = $self->build_arg($auth->{user}, $mode, $args);
my $pid = open2(my $reader, my $writer, $exec)
or die "failed to create subprocess: $!";
my $challenge = $auth->{challenge} || '';
$writer->print("$auth->{user}\0$auth->{password}\0$challenge\0")
or die "pipe wite failed: $!";
close $writer
or die "closing write pipe failed: $!";
return $pid, $reader;
}
sub read_qmauth {
my $_self = shift;
my ($pid, $reader) = @_;
my $input = <$reader>;
my $rs;
if (eof $reader) {
# for IPC::Open2
if (waitpid($pid, 0) == $pid) {
$rs = $?;
}
}
my ($resp, $rc);
if (!defined $rs) {
my ($r, $e);
eval { $r = decode_json $input; 1 }
or do {
$rc = 6;
$e = "$@";
};
$reader->read(my $buf, 32 * 1024**2);
if (!eof $reader) {
die 'mailpart too large (>32MB)';
kill 'TERM', $pid;
}
close $reader;
waitpid $pid, 0;
$resp = {
head => $r,
body => $buf,
rc => $?,
e => $e,
};
}
elsif ($rs == 3 << 8 || $rs == 0) {
$rc = $rs >> 8;
eval { $resp = decode_json $input if $input; 1 }
or do {
$resp = {
info => "error decoding response",
response => $input,
cause => $@,
return_code => $rc,
};
};
}
else {
$rc = $rs >> 8;
$resp = {
info => "got unsuccessful return code by qmail-authuser",
return_code => $rc,
response => $input,
};
}
return ($resp, $rc);
}
sub build_and_run {
my $self = shift;
my ($auth, $mode, $args) = @_;
my @exec = $self->start_qmauth($auth, $mode, $args||[]);
my ($resp, $rc) = $self->read_qmauth(@exec);
if ($rc) {
JWebmail::Model::ReadMails::QMailAuthuser::Error->throw(
"qmail-authuser connection error", $resp);
}
return $resp;
}
1
__END__
=encoding utf-8
=head1 NAME
QMailAuthuser
=head1 SYNOPSIS
...
=head1 DESCRIPTION
This ReadMails driver starts and communicates with L<JWebmail::Model::Driver::QMailAuthuser::Extract> over qmail-authuser.
The Extract programm runs with elevated priviliges to be able to read and modify mailboxes.
=head1 ATTRIBUTES
=head2 qmail_dir
The parent directory of the bin directory where all qmail executables live.
Default C</var/qmail/>.
=head2 prog
The path to the extractor programm.
Default is the location of L<JWebmail::Model::Driver::QMailAuthuser::Extract> package.
=head2 logfile
A path to a log file that the extractor logs to.
Default '/dev/null' but highly recommended to set a real one.
Keep in mind that a different user need to be able to write to it.
=head1 METHODS
=head2 communicate
Arguments:
=over 6
=item mode
=item args
Depends on the mode
=item user
E-Mail address of the user
=item password
Corresponding e-mail user password
=item challenge
Challenge when using cram
=back
=head1 SEE ALSO
L<JWebmail::Model::ReadMails>, L<JWebmail::Model::Driver::QMailAuthuser::Extract>
|