| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mail::MtPolicyd::Plugin::SqlUserConfig; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
2391
|
use Moose; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
16
|
|
|
4
|
2
|
|
|
2
|
|
13028
|
use namespace::autoclean; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
22
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '1.23'; # VERSION |
|
7
|
|
|
|
|
|
|
# ABSTRACT: mtpolicyd plugin for retrieving the user config of a user |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
extends 'Mail::MtPolicyd::Plugin'; |
|
10
|
|
|
|
|
|
|
with 'Mail::MtPolicyd::Plugin::Role::SqlUtils'; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
219
|
use Mail::MtPolicyd::Plugin::Result; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
52
|
|
|
14
|
2
|
|
|
2
|
|
1081
|
use JSON; |
|
|
2
|
|
|
|
|
12625
|
|
|
|
2
|
|
|
|
|
17
|
|
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has 'sql_query' => ( |
|
17
|
|
|
|
|
|
|
is => 'rw', isa => 'Str', |
|
18
|
|
|
|
|
|
|
default => 'SELECT config FROM user_config WHERE address=?', |
|
19
|
|
|
|
|
|
|
); |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has '_json' => ( |
|
22
|
|
|
|
|
|
|
is => 'ro', isa => 'JSON', lazy => 1, |
|
23
|
|
|
|
|
|
|
default => sub { |
|
24
|
|
|
|
|
|
|
return JSON->new; |
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
); |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has 'field' => ( is => 'rw', isa => 'Str', default => 'recipient' ); |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub _get_config { |
|
31
|
1
|
|
|
1
|
|
2
|
my ( $self, $r ) = @_; |
|
32
|
1
|
|
|
|
|
48
|
my $key = $r->attr( $self->field ); |
|
33
|
1
|
50
|
33
|
|
|
15
|
if( ! defined $key || $key =~ /^\s*$/ ) { |
|
34
|
0
|
|
|
|
|
0
|
die('key field '.$self->field.' not defined or empty in request'); |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
1
|
|
|
|
|
50
|
my $sth = $self->execute_sql( $self->sql_query, $key ); |
|
38
|
1
|
|
|
|
|
11
|
my ( $json ) = $sth->fetchrow_array; |
|
39
|
1
|
50
|
|
|
|
4
|
if( ! defined $json ) { |
|
40
|
0
|
|
|
|
|
0
|
die( 'no user-config found for '.$key ); |
|
41
|
|
|
|
|
|
|
} |
|
42
|
1
|
|
|
|
|
46
|
return $self->_json->decode( $json ); |
|
43
|
|
|
|
|
|
|
} |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub run { |
|
46
|
1
|
|
|
1
|
1
|
610
|
my ( $self, $r ) = @_; |
|
47
|
1
|
|
|
|
|
1
|
my $config; |
|
48
|
|
|
|
|
|
|
|
|
49
|
1
|
|
|
|
|
2
|
eval { $config = $self->_get_config($r) }; |
|
|
1
|
|
|
|
|
4
|
|
|
50
|
1
|
50
|
|
|
|
4
|
if( $@ ) { |
|
51
|
0
|
|
|
|
|
0
|
$self->log($r, 'unable to retrieve user-config: '.$@); |
|
52
|
0
|
|
|
|
|
0
|
return; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
|
|
55
|
1
|
|
|
|
|
4
|
foreach my $key ( keys %$config ) { |
|
56
|
1
|
|
|
|
|
42
|
$r->session->{$key} = $config->{$key}; |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
6
|
return; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
1; |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
__END__ |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=pod |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=encoding UTF-8 |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 NAME |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Mail::MtPolicyd::Plugin::SqlUserConfig - mtpolicyd plugin for retrieving the user config of a user |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 VERSION |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
version 1.23 |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
This plugin will retrieve a JSON string from an SQL database and will merge the data structure |
|
84
|
|
|
|
|
|
|
into the current session. |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
This could be used to retrieve configuration values for users from a database. |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 PARAMETERS |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=over |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=item sql_query (default: SELECT config FROM user_config WHERE address=?) |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
The SQL query to retrieve the JSON configuration string. |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
The content of the first row/column is used. |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=item field (default: recipient) |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
The request field used in the sql query to retrieve the user configuration. |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=back |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 EXAMPLE USER SPECIFIC GREYLISTING |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Create the following table in the SQL database: |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
CREATE TABLE `user_config` ( |
|
109
|
|
|
|
|
|
|
`id` int(11) NOT NULL AUTO_INCREMENT, |
|
110
|
|
|
|
|
|
|
`address` varchar(255) DEFAULT NULL, |
|
111
|
|
|
|
|
|
|
`config` TEXT NOT NULL, |
|
112
|
|
|
|
|
|
|
PRIMARY KEY (`id`), |
|
113
|
|
|
|
|
|
|
UNIQUE KEY `address` (`address`) |
|
114
|
|
|
|
|
|
|
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
INSERT INTO TABLE `user_config` VALUES( NULL, 'karlson@vomdach.de', '{"greylisting":"on"}' ); |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
In mtpolicyd.conf: |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
db_dsn="dbi:mysql:mail" |
|
121
|
|
|
|
|
|
|
db_user=mail |
|
122
|
|
|
|
|
|
|
db_password=password |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
<Plugin user-config> |
|
125
|
|
|
|
|
|
|
module = "SqlUserConfig" |
|
126
|
|
|
|
|
|
|
sql_query = "SELECT config FROM user_config WHERE address=?" |
|
127
|
|
|
|
|
|
|
</Plugin> |
|
128
|
|
|
|
|
|
|
<Plugin greylist> |
|
129
|
|
|
|
|
|
|
enabled = "off" # off by default |
|
130
|
|
|
|
|
|
|
uc_enabled = "greylisting" # override with value of key 'greylisting' is set in session |
|
131
|
|
|
|
|
|
|
module = "Greylist" |
|
132
|
|
|
|
|
|
|
score = -5 |
|
133
|
|
|
|
|
|
|
mode = "passive" |
|
134
|
|
|
|
|
|
|
</Plugin> |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 AUTHOR |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Markus Benning <ich@markusbenning.de> |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>. |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
This is free software, licensed under: |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
The GNU General Public License, Version 2, June 1991 |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=cut |