aboutsummaryrefslogtreecommitdiff
path: root/www/logged_in_p.pl
blob: a79d790c3dc6fecc4754b1f52aa9adf1fff66c8b (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
#! /usr/bin/env perl
# Copyright 2012, Matthias Andreas Benkard <code@mail.matthias.benkard.de>.


use common::sense;
use Modern::Perl;

use JSON;

use File::Slurp;

use CGI;
use CGI::Fast;
use CGI::Session;

use Mail::ExpandAliases;


while (my $cgi = new CGI::Fast) {
  print $cgi->header(-content_type => 'application/json; charset=UTF-8');

  my $cookie = $cgi->cookie('mulkid_session');
  unless ($cookie) {
    say encode_json({logged_in_p => 0});
    exit(0);
  }

  my $session = new CGI::Session("driver:File", $cookie, {Directory=>"/tmp"});
  unless ($session) {
    say encode_json({logged_in_p => 0});
    exit(0);
  }

  my $aliases = Mail::ExpandAliases->new("/etc/aliases");
  my $email        = $cgi->param('email')    or die "No email address supplied";
  my $session_user = $session->param('user');

  my $alias;
  if ($email =~ /^(.*?)@/) { $alias = $1; }
  my $email_users = $aliases->expand($alias) or die "User not found";

  if ($session_user ~~ @$email_users) {
    say encode_json({logged_in_p => 1});
  } else {
    say encode_json({logged_in_p => 0});
  }
}