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