aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Fripost/Schema.pm
blob: 35c69e21dc983e7d85ff77e2268947b877ec9e5e (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
package Fripost::Schema;

=head1 NAME

Schema.pm -

=cut

=head1 DESCRIPTION

Schema.pm abstracts the LDAP schema definition and provides methods to
add, list or delete virtual domains, mailboxes, aliases or lists.

=cut

use 5.010_000;
use strict;
use warnings;
use utf8;

use Net::LDAP;
use Authen::SASL;
use Fripost::Schema::Domain;
use Fripost::Schema::Mailbox;
use Fripost::Schema::Alias;
use Fripost::Schema::List;
use Fripost::Schema::Local;
use Net::IDN::Encode qw/email_to_ascii/;


=head1 METHODS

=over 4

=item B<SASLauth> (I<username>, I<CFG>)

Start a LDAP connection, and SASL-authenticate using proxy
authentication for the given (fully-qualified) user. I<CFG> should
contain definitions for the LDAP suffix and the authentication ID.

=cut

sub SASLauth {
    my $class = shift;
    my ($l,$d) = split /\@/, shift, 2;
    my %cfg = @_;

    my $self = bless {}, $class;
    $self->suffix( join ',', @{$cfg{ldap_suffix}} );
    $self->whoami( "fvu=$l,fvd=$d,".$self->suffix );
    $self->ldap( Net::LDAP::->new( $cfg{ldap_uri}, async => 1 ) );

    my $sasl = Authen::SASL::->new(
                   mechanism => 'DIGEST-MD5',
                   callback => { user => $cfg{ldap_authcID}
                               , pass => $cfg{ldap_authcPW}
                               , authname => 'dn:'.$self->whoami }
    );
    my $mesg = $self->ldap->bind( sasl => $sasl );
    # This is not supposed to happen.
    die $mesg->error if $mesg->code;

    return $self;
}


=item B<auth> (I<username>, I<password>, I<CFG>)

Start a LDAP connection, and (simples-) binds the given user.
I<CFG> should contain definitions for the LDAP suffix and URI.

=cut

sub auth {
    my $class = shift;
    my $id = shift;
    my $pw = shift;
    my %cfg = @_;

    my $self = bless {}, $class;
    $self->suffix( join ',', @{$cfg{ldap_suffix}} );

    if (not (defined $id) or defined $cfg{ldap_bind_dn}) {
        $self->whoami( $cfg{ldap_bind_dn} );
    }
    else {
        my ($l,$d) = split /\@/, $id, 2;
        $self->whoami( "fvu=$l,fvd=$d,".$self->suffix );
    }

    $self->ldap( Net::LDAP::->new( $cfg{ldap_uri}, async => 1 ) );

    my $mesg = $self->ldap->bind( $self->whoami, password => $pw );
    if ($mesg->code) {
        if (defined $cfg{'-die'}) {
            return unless $cfg{'-die'};
            die $cfg{'-die'}."\n";
        }
        die $mesg->error;
    }
    return $self;
}



# The DN of the authorization ID
sub whoami { shift->_set_or_get('_whoami',@_); }

# The LDAP object (of class Net::LDAP)
sub ldap { shift->_set_or_get('_ldap',@_); }

# The suffix under which virtual domains are.
sub suffix { shift->_set_or_get('_suffix',@_); }


# Set or get a key (the first argument), depending on whether a second
# argument is given or not.
sub _set_or_get {
    my $self = shift;
    my $what = shift;

    if (@_) {
        $self->{$what} = $_[0];
    }
    else {
        return $self->{$what};
    }
}



=item B<domain>

Bless the object to C<Fripost::Schema::Domain>, to access
domain-specific methods.

=cut

sub domain { bless shift, 'Fripost::Schema::Domain'; }


=item B<mailbox>

Bless the object to C<Fripost::Schema::Mailbox>, to access
mailbox-specific methods.

=cut

sub mailbox { bless shift, 'Fripost::Schema::Mailbox'; }


=item B<alias>

Bless the object to C<Fripost::Schema::Alias>, to access
alias-specific methods.

=cut

sub alias { bless shift, 'Fripost::Schema::Alias'; }


=item B<list>

Bless the object to C<Fripost::Schema::List>, to access
list-specific methods.

=cut

sub list { bless shift, 'Fripost::Schema::List'; }


=item B<local>

Bless the object to C<Fripost::Schema::Local>, to access
local-specific (mailboxes, aliases and lists) methods.

=cut

sub local { bless shift, 'Fripost::Schema::Local'; }



=item B<done>

Unbinds from the LDAP server.

=cut

sub done {
    my $self = shift;
    $self->ldap->unbind if defined $self and defined $self->ldap;
}


=back

=head1 AUTHOR

Guilhem Moulin C<< <guilhem at fripost.org> >>

=head1 COPYRIGHT

Copyright 2012 Guilhem Moulin.

=head1 LICENSE

This program is free software; you can redistribute it and/or modify it
under the same terms as perl itself.

=cut

1;

__END__