]>
Commit | Line | Data |
---|---|---|
b00d2d73 DC |
1 | package PMG::API2::SpamAssassin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::Tools; | |
7 | use PVE::SafeSyslog; | |
8 | use PVE::INotify; | |
9 | use PVE::Exception qw(raise_param_exc); | |
10 | use PVE::RESTHandler; | |
11 | use PMG::RESTEnvironment; | |
12 | use PVE::JSONSchema qw(get_standard_option); | |
13 | ||
14 | use PMG::Utils; | |
85d295d6 | 15 | use PMG::Config; |
b00d2d73 DC |
16 | |
17 | use Mail::SpamAssassin; | |
18 | ||
19 | use base qw(PVE::RESTHandler); | |
20 | ||
b00d2d73 DC |
21 | __PACKAGE__->register_method ({ |
22 | name => 'index', | |
23 | path => '', | |
24 | method => 'GET', | |
25 | description => "Directory index.", | |
46607089 | 26 | permissions => { check => [ 'admin', 'audit' ] }, |
b00d2d73 DC |
27 | parameters => { |
28 | additionalProperties => 0, | |
29 | properties => { | |
30 | node => get_standard_option('pve-node'), | |
31 | }, | |
32 | }, | |
33 | returns => { | |
34 | type => 'array', | |
35 | items => { | |
36 | type => "object", | |
37 | properties => {}, | |
38 | }, | |
39 | links => [ { rel => 'child', href => "{subdir}" } ], | |
40 | }, | |
41 | code => sub { | |
42 | my ($param) = @_; | |
43 | ||
44 | my $res = []; | |
45 | ||
46 | push @$res, { subdir => "rules" }; | |
47 | ||
48 | return $res; | |
49 | }}); | |
50 | ||
51 | __PACKAGE__->register_method({ | |
52 | name => 'rules_status', | |
53 | path => 'rules', | |
54 | method => 'GET', | |
46607089 DM |
55 | description => "SpamAssassin rules status.", |
56 | permissions => { check => [ 'admin', 'audit' ] }, | |
57 | proxyto => 'node', | |
b00d2d73 DC |
58 | parameters => { |
59 | additionalProperties => 0, | |
60 | properties => { | |
61 | node => get_standard_option('pve-node'), | |
62 | }, | |
63 | }, | |
64 | returns => { | |
65 | type => 'array', | |
66 | items => { | |
67 | type => "object", | |
68 | properties => { | |
69 | channel => { type => 'string' }, | |
70 | update_avail => { type => 'boolean' }, | |
71 | version => { type => 'string', optional => 1 }, | |
72 | last_updated => { type => 'integer', optional => 1}, | |
73 | update_version => { type => 'string', optional => 1}, | |
74 | }, | |
75 | }, | |
76 | }, | |
77 | code => sub { | |
78 | my ($param) = @_; | |
79 | ||
80 | my $saversion = $Mail::SpamAssassin::VERSION; | |
d6b4011d SI |
81 | my $sa_update_dir = "/var/lib/spamassassin/$saversion/"; |
82 | ||
83 | my $check_channel = sub { | |
84 | my ($channel) = @_; | |
85 | ||
86 | # see sa-update source: | |
87 | my $channel_file_base = $channel; | |
88 | $channel_file_base =~ s/[^A-Za-z0-9-]+/_/g; | |
89 | my $channelfile = "${sa_update_dir}${channel_file_base}.cf"; | |
90 | ||
91 | my $mtime = -1; | |
92 | my $version = -1; | |
93 | my $newversion = -1; | |
94 | ||
95 | if (-f $channelfile) { | |
96 | # stat metadata cf file | |
97 | $mtime = (stat($channelfile))[9]; # 9 is mtime | |
98 | ||
99 | # parse version from metadata cf file | |
100 | my $metadata = PVE::Tools::file_read_firstline($channelfile); | |
101 | if ($metadata =~ m/\s([0-9]+)$/) { | |
102 | $version = $1; | |
103 | } else { | |
104 | warn "invalid metadata in '$channelfile'\n"; | |
105 | } | |
106 | } | |
107 | # call sa-update to see if updates are available | |
b00d2d73 | 108 | |
98fd3ab0 | 109 | my $cmd = "sa-update -v --checkonly --channel $channel"; |
d6b4011d SI |
110 | PVE::Tools::run_command($cmd, noerr => 1, logfunc => sub { |
111 | my ($line) = @_; | |
b00d2d73 | 112 | |
d6b4011d SI |
113 | if ($line =~ m/Update available for channel \S+: -?[0-9]+ -> ([0-9]+)/) { |
114 | $newversion = $1; | |
115 | } | |
116 | }); | |
b00d2d73 | 117 | |
d6b4011d SI |
118 | my $result = { |
119 | channel => $channel, | |
120 | }; | |
b00d2d73 | 121 | |
d6b4011d SI |
122 | $result->{version} = $version if $version > -1; |
123 | $result->{update_version} = $newversion if $newversion > -1; | |
124 | $result->{last_updated} = $mtime if $mtime > -1; | |
b00d2d73 | 125 | |
d6b4011d SI |
126 | if ($newversion > $version) { |
127 | $result->{update_avail} = 1; | |
128 | } else { | |
129 | $result->{update_avail} = 0; | |
b00d2d73 | 130 | } |
d6b4011d | 131 | return $result; |
b00d2d73 DC |
132 | }; |
133 | ||
d6b4011d | 134 | my @channels = ('updates.spamassassin.org'); |
b00d2d73 | 135 | |
d6b4011d SI |
136 | my $localchannels = PMG::Utils::local_spamassassin_channels(); |
137 | push(@channels, map { $_->{channelurl} } @$localchannels); | |
b00d2d73 | 138 | |
d6b4011d | 139 | return [ map { $check_channel->($_) } @channels]; |
b00d2d73 DC |
140 | }}); |
141 | ||
142 | __PACKAGE__->register_method({ | |
143 | name => 'update_rules', | |
144 | path => 'rules', | |
145 | method => 'POST', | |
146 | description => "Update SpamAssassin rules.", | |
147 | protected => 1, | |
46607089 DM |
148 | permissions => { check => [ 'admin' ] }, |
149 | proxyto => 'node', | |
b00d2d73 DC |
150 | parameters => { |
151 | additionalProperties => 0, | |
152 | properties => { | |
153 | node => get_standard_option('pve-node'), | |
154 | }, | |
155 | }, | |
156 | returns => { type => 'string' }, | |
157 | code => sub { | |
158 | my ($param) = @_; | |
159 | ||
160 | my $rpcenv = PMG::RESTEnvironment->get(); | |
161 | my $authuser = $rpcenv->get_user(); | |
162 | ||
163 | my $realcmd = sub { | |
164 | my $upid = shift; | |
165 | ||
85d295d6 DM |
166 | # setup proxy env (assume sa-update use http) |
167 | my $pmg_cfg = PMG::Config->new(); | |
168 | if (my $http_proxy = $pmg_cfg->get('admin', 'http_proxy')) { | |
169 | $ENV{http_proxy} = $http_proxy; | |
170 | } | |
171 | ||
98fd3ab0 | 172 | my $cmd = "sa-update -v"; |
b00d2d73 DC |
173 | |
174 | PVE::Tools::run_command($cmd, noerr => 1); | |
98fd3ab0 SI |
175 | |
176 | PMG::Utils::update_local_spamassassin_channels(1); | |
b00d2d73 DC |
177 | }; |
178 | ||
179 | return $rpcenv->fork_worker('saupdate', undef, $authuser, $realcmd); | |
180 | }}); | |
181 | ||
182 | 1; |