summaryrefslogtreecommitdiff
path: root/lib/JWebmail/Controller/Webmail.pm
blob: acf7557e4c71434c9a9f7f642ab8d396a371a604 (plain)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package JWebmail::Controller::Webmail;

use Mojo::Base Mojolicious::Controller;

use Carp 'carp';
use List::Util 'first';

use Mojo::Util qw(encode decode b64_encode b64_decode);
use Mojolicious::Types;

use JWebmail::View::Webmail;
use JWebmail::View::RenderMail;

use constant TRUE_RANDOM => eval { require Crypt::URandom; Crypt::URandom->import('urandom'); 1 };

use constant {
    SES_USER => 'user', # Key for user name in active session
    STS_AUTH => 'auth',
};


# no action has been taken, display login page
sub noaction {
    my $self = shift;

    my $user = $self->session(SES_USER);
    if ($user) {
        $self->res->code(307);
        $self->redirect_to('home');
        return;
    }
    $self->render(action => 'login');
}


# middleware
sub auth {
    my $self = shift;

    my $user = $self->session(SES_USER);
    my ($pw, $ch) = $self->_session_passwd();

    unless ($user && $pw) {
        $self->flash(message => $self->l('No active session.'));
        $self->res->code(401);
        $self->redirect_to('logout');
        return 0;
    }

    $self->stash(STS_AUTH() => $self->users->Auth(user => $user, password => $pw, challenge => $ch));

    return 1;
}


sub _time :prototype(&$$) {
    my $code = shift;
    my $self = shift;
    my $name = shift;

    $self->timing->begin($name);

    my @res = $code->();

    my $elapsed = $self->timing->elapsed($name);
    $self->app->log->debug(sprintf("%s took %fs", $name, $elapsed));

    return wantarray ? @res : $res[-1];
}


sub login {
    my $self = shift;

    my $uses_cram = $self->config->{session}{secure} eq 'cram';

    my $v = $self->validation;

    my $user = $v->required('userid')->size(4, 50)->param;
    my $passwd = $v->required('password')->size(4, 50)->like(qr/^.+$/)->param; # no new-lines
    my $challenge;
    if ($uses_cram) {
        $challenge = $v->required('challenge')->size(4, 50)->param;
    }

    if ($v->has_error) {
        $self->render(status => 400);
        return;
    }

    my $auth = $self->users->Auth(user => $user, password => $passwd);
    $auth->{challenge} = $challenge if $uses_cram;
    my $valid = _time { $self->users->verify_user($auth) } $self, 'verify user';

    if ($valid) {
        $self->session(SES_USER() => $user);
        $self->_session_passwd($passwd, $challenge);

        $self->res->code(303);
        $self->redirect_to('displayheaders');
    }
    else {
        $self->render(
            status => 401,
            warning => $self->l('Login failed!'),
        );
    }
}


sub logout {
    my $self = shift;

    delete $self->session->{SES_USER()};
    $self->_session_passwd('');

    # $self->session(expires => 1);

    $self->res->code(303);
    $self->redirect_to('login');
}


sub about {
    my $self = shift;

    $self->stash(
        scriptadmin => $self->config->{defaults}{scriptadmin},
        http_host   => $self->tx->req->url->to_abs->host,
        request_uri => $self->tx->req->url,
        remote_addr => $self->tx->original_remote_address,
    );
}


sub displayheaders {
    no warnings 'experimental::smartmatch';
    my $self = shift;

    my $auth = $self->stash(STS_AUTH);

    my $folders = _time { $self->users->folders($auth) } $self, 'user folders';

    unless ( !$self->stash('folder') || $self->stash('folder') ~~ $folders ) {
        $self->render(template => 'error',
            status => 404,
            error => $self->l('no_folder'),
            links => [map { $self->url_for(folder => $_) } @$folders],
        );
        return;
    }

    my $v = $self->validation;
    my $sort = $v->optional('sort')->like(qr'^!?(?:date|subject|sender|size)$')->param // '!date';
    my $search = $v->optional('search')->param;

    if ($v->has_error) {
        local $" = ' ';
        $self->render(template => 'error', error => "errors in @{ $v->failed }", status => 400);
        return;
    }

    my $count = _time { $self->users->count($auth, $self->stash('folder')) } $self, 'user count';

    my ($start, $end) = $self->paginate($count->{total_mails});

    $self->timing->begin('user_headers');
    my $headers = do {
        if ($search) {
            $self->users->search(
                $auth, $search, $self->stash('folder'),
            )
        }
        else {
            $self->users->read_headers_for(
                $auth,
                folder => $self->stash('folder'),
                start  => $start,
                end    => $end,
                sort   => $sort,
            )
        }
    };
    my $elapsed = $self->timing->elapsed('user_headers');
    $self->app->log->debug(sprintf("Reading user headers took %fs", $elapsed));

    $self->stash(
        v               => JWebmail::View::Webmail->new,
        msgs            => $headers,
        mail_folders    => $folders,
        total_size      => $count->{byte_size},
        total_new_mails => $count->{unread_mails},
    );
}


sub readmail {
    my $self = shift;

    my $mid = $self->stash('id');
    my $auth = $self->stash(STS_AUTH);

    my $mail;
    my $ok = eval { $mail = $self->users->show($auth, '', $mid); 1 };
    if (!$ok) {
        my $err = "$@";
        if ($err =~ /unkown mail-id|no such message/) {
            $self->reply->not_found;
            return;
        }
        die;
    }

    $self->stash(
        v   => JWebmail::View::RenderMail->new(c => $self),
        msg => $mail,
    );
}


sub raw {
    my $self = shift;

    my $mid = $self->stash('id');
    my $auth = $self->stash(STS_AUTH);

    # select a single body element
    my $v = $self->validation;
    my $path = $v->optional('path')->like(qr(^\d(\.\d)*$)a)->param;
    return $self->render(text => 'Issue in parameter "path": '.join(' ', $v->error('path')->@*), status => 400, format => 'txt') if $v->has_error;

    my $content = $self->users->raw($auth, '', $mid, $path);

    $self->res->headers->content_disposition(qq[attachment; filename="$content->{head}{filename}"])
        if lc ($content->{head}{content_disposition}//'') eq 'attachment';
    my $ct = JWebmail::View::RenderMail::to_mime_type($content->{head});
    if ($ct =~ m'^text/') { $ct .= '; charset=UTF-8' }
    $self->res->headers->content_type($ct);
    $self->render(data => $content->{body});
}


sub writemail { }


sub sendmail {
    my $self = shift;

    my $v = $self->validation;
    $v->csrf_protect;

    my %mail = (
        to      => scalar $v->required('to', 'not_empty')->check('mail_line')->every_param,
        message => scalar $v->required('body', 'not_empty')->param,
        subject => scalar $v->required('subject', 'not_empty')->param,
        cc      => scalar $v->optional('cc', 'not_empty')->check('mail_line')->every_param,
        bcc     => scalar $v->optional('bcc', 'not_empty')->check('mail_line')->every_param,
        reply   => scalar $v->optional('back_to', 'not_empty')->check('mail_line')->param,
        attach  => scalar $v->optional('attach', 'non_empty_ul')->upload->param,
        from    => scalar $self->stash(STS_AUTH)->{user},
    );
    $mail{attach_type} = Mojolicious::Types->new()->file_type($mail{attach}->filename) if $mail{attach};

    if ($v->has_error) {
        $self->log->debug("mail send failed. Error in @{ $v->failed }");

        $self->render(action => 'writemail',
            warning => $self->l('error_send'),
        );
        return;
    }

    my $error = $self->send_mail(\%mail);

    if ($error) {
        $v->error(send => ['internal_error']); # make validation fail so that values are restored

        $self->render(action => 'writemail',
            warning => $self->l('error_send'),
            status => 400,
        );
        return;
    }

    $self->flash(message => $self->l('succ_send'));
    $self->res->code(303);
    $self->redirect_to('displayheaders');
}


sub move {
    my $self = shift;

    my $v = $self->validation;
    $v->csrf_protect;

    if ($v->has_error) {
        return;
    }

    my $auth = $self->stash(STS_AUTH);
    my $folders = $self->users->folders($auth);

    my $mm = $self->every_param('mail');
    my $folder = $self->param('folder');

    no warnings 'experimental::smartmatch';
    die "$folder not valid" unless $folder ~~ $folders;

    $self->users->move($auth, $_, '', $folder) for @$mm;

    $self->flash(message => $self->l('succ_move'));
    $self->res->code(303);
    $self->redirect_to('displayheaders');
}


### session password handling

use constant { S_PASSWD => 'pw', S_OTP_S3D_PW => 'otp_s3d_pw' };

sub _rand_data {
    my $len = shift;

    if (TRUE_RANDOM) {
        #return makerandom_octet(Length => $len, Strength => 0); # was used for Crypt::Random
        return urandom($len);
    }
    else {
        my $res = '';
        for (0..$len-1) {
            vec($res, $_, 8) = int rand 256;
        }

        return $res;
    }
}

sub _session_passwd {
    my ($self, $passwd, $challenge) = @_;
    my $secAlg = $self->config->{session}{secure};

    $self->_warn_crypt;

    if (defined $passwd) { # set
        if ($secAlg eq 'cram') {
            $self->session(S_PASSWD() => $passwd, challenge => $challenge);
        }
        elsif ($secAlg eq 's3d') {
            unless ($passwd) {
                $self->s3d(S_PASSWD, '');
                delete $self->session->{S_OTP_S3D_PW()};
                return;
            }
            die "'$passwd' contains invalid character \\n" if $passwd =~ /\n/; 
            if (length $passwd < 20) {
                $passwd .= "\n" . ' ' x (20 - length($passwd) - 1);
            }
            my $passwd_utf8 = encode('UTF-8', $passwd);
            my $rand_bytes = _rand_data(length $passwd_utf8);
            $self->s3d(S_PASSWD, b64_encode($passwd_utf8 ^ $rand_bytes, ''));
            $self->session(S_OTP_S3D_PW, b64_encode($rand_bytes, ''));
        }
        else {
            $self->session(S_PASSWD() => $passwd);
        }
    }
    else { # get
        if ($secAlg eq 'cram') {
            wantarray or carp "you forgot the challenge";
            return ($self->session(S_PASSWD), $self->session('challenge'));
        }
        elsif ($secAlg eq 's3d') {
            my $pw = b64_decode($self->s3d(S_PASSWD) || '');
            my $otp = b64_decode($self->session(S_OTP_S3D_PW) || '');
            my ($res) = split "\n", decode('UTF-8', $pw ^ $otp), 2;
            return $res;
        }
        else {
            return $self->session(S_PASSWD);
        }
    }
}

sub _warn_crypt {
    my $self = shift;

    state $once = 0;

    if ( !TRUE_RANDOM && !$once && lc $self->config->{session}{secure} eq 's3d' ) {
        $self->log->warn("Falling back to pseudo random generation. Please install Crypt::URandom");
        $once = 1;
    }
}


1

__END__

=encoding utf-8

=head1 NAME

Webmail - All functions comprising the webmail application.

=head1 SYNOPSIS

    my $r = $app->routes;
    $r->get('/about')->to('Webmail#about');
    $r->post('/login')->to('Webmail#login');

=head1 DESCRIPTION

The controller of JWebmail.

=head1 ROUTES

=head2 noaction

The login page. This should be the root.

=head2 auth

    my $a = $r->under('/')->to('Webmail#auth');

    An intermediate route that makes sure a user has a valid session.

=head2 login

Post route that checks login data.

=head2 logout

Route that clears session data.

=head2 about

Public route.

=head2 displayheaders

Provides an overview over messages.

=head2 readmail

Displays a single mail.
Can also be displayed as JSON or only one body part as the appropriate content type.

=head2 writemail

A mail editor.

=head2 sendmail

Sends a mail written in writemail.

=head2 move

Moves mails between mail forlders.

=head1 METHODS

=head2 _session_passwd

A helper used to set and get the session password. The behavior can be altered by
setting the config variable C<< session => {secure => 's3d'} >>.

  $c->_session_passwd('s3cret');

Currently the following modes are supported:

=over 6

=item none 

The password is plainly stored in session cookie.
The cookie is stored on the client side and send with every request.

=item cram 

A nonce is send to the client and the cram_md5 is generated there via js
and crypto-js.
This is vulnurable to replay attacks as the nonce is not invalidated ever.

=item s3d 

The password is stored on the server. Additionally the password is encrypted
by an one-time-pad that is stored in the users cookie.
This is vulnurable to replay attacks during an active session.
On log-in it is transfered plainly.

=back

=head1 DEPENDENCIES

Crypt::URandom is recommended