line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mail::Barracuda::API; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
42031
|
use 5.008008; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
34
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
29
|
|
6
|
1
|
|
|
1
|
|
1264
|
use Data::Dumper; |
|
1
|
|
|
|
|
15738
|
|
|
1
|
|
|
|
|
84
|
|
7
|
1
|
|
|
1
|
|
605
|
use XML::Simple; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use LWP::UserAgent; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 NAME |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Mail::Barracuda::API - Manage Barracuda Antispam Appliance |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 SYNOPSIS |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
use Mail::Barracuda::API; |
20
|
|
|
|
|
|
|
my $api = Mail::Barracuda::API->new( |
21
|
|
|
|
|
|
|
server => 'mybarracuda.mydomain.com', |
22
|
|
|
|
|
|
|
port => 8000, |
23
|
|
|
|
|
|
|
api_key => 'my API key', |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
$api->domainAdd(domain => 'example.com', mailhost=> 'mail.example.com'); |
26
|
|
|
|
|
|
|
$api->domainRemove(domain => 'example.com'); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 DESCRIPTION |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
This module provides a Perl interface to parts of the |
31
|
|
|
|
|
|
|
Barracuda Antispam Appliance API |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head2 Methods |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head3 new |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $api = Mail::Barracuda::API->new( |
38
|
|
|
|
|
|
|
server => 'http://mybarracuda.mydomain.com', |
39
|
|
|
|
|
|
|
port => 8000, |
40
|
|
|
|
|
|
|
api_key => 'my API key', |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Sets up a Mail::Barracuda::API session. Port defaults to 8000. |
44
|
|
|
|
|
|
|
All other parameters are necessary.. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub new { |
49
|
|
|
|
|
|
|
my $invocant = shift; |
50
|
|
|
|
|
|
|
my $class = ref($invocant) || $invocant; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my $self = { |
53
|
|
|
|
|
|
|
port => 8000, |
54
|
|
|
|
|
|
|
@_ |
55
|
|
|
|
|
|
|
}; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
return bless $self, $class; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head3 userChange |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
my $response = $api->userChange( |
63
|
|
|
|
|
|
|
email => 'jane@example.com', |
64
|
|
|
|
|
|
|
var => 'user_password', |
65
|
|
|
|
|
|
|
val=> '4321', |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Sets property user_password on account jane@example.com to 4321. See API |
69
|
|
|
|
|
|
|
manual for other valid vars. |
70
|
|
|
|
|
|
|
$response is a 0 if successful and a 1 if a error occured. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub userChange { |
75
|
|
|
|
|
|
|
my ($class, %args) = @_; |
76
|
|
|
|
|
|
|
my $result = 0; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $email = $args{email}; |
79
|
|
|
|
|
|
|
my $var = $args{var}; |
80
|
|
|
|
|
|
|
my $val = $args{val}; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my $cmd = "config_set.cgi?account=$email&variable=$var&value=$val"; |
83
|
|
|
|
|
|
|
my ($respcode, $status) = $class->_doRequest(command => $cmd); |
84
|
|
|
|
|
|
|
if ($respcode != 200) { |
85
|
|
|
|
|
|
|
print STDERR "$respcode: Could not change user $email var $var to $val\n"; |
86
|
|
|
|
|
|
|
$result = 1; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
return $result; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head3 domainAdd |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my $response = $api->domainAdd( |
95
|
|
|
|
|
|
|
domain => 'example.com', |
96
|
|
|
|
|
|
|
mailhost => 'mail.example.com', |
97
|
|
|
|
|
|
|
); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Sets up a domain on the Barracuda Appliance. |
100
|
|
|
|
|
|
|
$response is a 0 if successful and a 1 if a error occured. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=cut |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub domainAdd { |
105
|
|
|
|
|
|
|
my ($class, %args) = @_; |
106
|
|
|
|
|
|
|
my $result = 0; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
my $domain = $args{domain}; |
109
|
|
|
|
|
|
|
my $mailhost = $args{mailhost}; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
my $cmd = "add_domain.cgi?domain=$domain"; |
112
|
|
|
|
|
|
|
my ($respcode, $status) = $class->_doRequest(command => $cmd); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
if ($respcode != 200) { |
115
|
|
|
|
|
|
|
print STDERR "Could not add domain $domain: $status\n"; |
116
|
|
|
|
|
|
|
$result = 1; |
117
|
|
|
|
|
|
|
} else { |
118
|
|
|
|
|
|
|
$cmd = "config_set.cgi?variable=mta_relay_advanced_host"; |
119
|
|
|
|
|
|
|
$cmd .= "&domain=$domain&value=$mailhost"; |
120
|
|
|
|
|
|
|
$class->_doRequest(command => $cmd); |
121
|
|
|
|
|
|
|
if ($respcode != 200) { |
122
|
|
|
|
|
|
|
print STDERR "$respcode: Could not set mailhost for $domain: $status\n"; |
123
|
|
|
|
|
|
|
$result = 1; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
return $result; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head3 domainRemove |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
my $response = $api->domainRemove(domain => 'example.com'); |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Removes a domain from the Barracuda Appliance. |
135
|
|
|
|
|
|
|
$response is a 0 if successful and a 1 if a error occured. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub domainRemove { |
140
|
|
|
|
|
|
|
my ($class, %args) = @_; |
141
|
|
|
|
|
|
|
my $result = 0; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
my $domain = $args{domain}; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
my $cmd = "delete_domain.cgi?domain=$domain"; |
146
|
|
|
|
|
|
|
my ($respcode, $status) = $class->_doRequest(command => $cmd); |
147
|
|
|
|
|
|
|
if ($respcode != 200) { |
148
|
|
|
|
|
|
|
print STDERR "$respcode: Could not remove domain $domain: $status\n"; |
149
|
|
|
|
|
|
|
$result = 1; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
return $result; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head3 userAdd |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
my $response = $api->userAdd( |
157
|
|
|
|
|
|
|
email => 'jane@example.com', |
158
|
|
|
|
|
|
|
paassword => '12345', |
159
|
|
|
|
|
|
|
}; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Adds a quarantine and personal settings login for the email address provided. |
162
|
|
|
|
|
|
|
$response is a 0 if successful and a 1 if a error occured. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=cut |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub userAdd { |
167
|
|
|
|
|
|
|
my ($class, %args) = @_; |
168
|
|
|
|
|
|
|
my $result = 0; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
my $email = $args{email}; |
171
|
|
|
|
|
|
|
my $pass = $args{password}; |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
my $cmd = "config_add.cgi?account=$email&create=1"; |
174
|
|
|
|
|
|
|
my ($respcode, $status) = $class->_doRequest(command => $cmd); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
if ($respcode != 708) { |
177
|
|
|
|
|
|
|
print STDERR "$respcode: Could not add user $email: $status\n"; |
178
|
|
|
|
|
|
|
$result = 1; |
179
|
|
|
|
|
|
|
} else { |
180
|
|
|
|
|
|
|
my $resp = $class->userChange( |
181
|
|
|
|
|
|
|
email => $email, |
182
|
|
|
|
|
|
|
var => 'user_password', |
183
|
|
|
|
|
|
|
val => $pass, |
184
|
|
|
|
|
|
|
); |
185
|
|
|
|
|
|
|
if ($resp != 0) { |
186
|
|
|
|
|
|
|
print STDERR "$respcode: Could not set password for user $email: $status\n"; |
187
|
|
|
|
|
|
|
$result = 1; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
return $result; |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head3 userRemove |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
my $response = $api->userRemove(email => 'jane@example.com'); |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Removes the user from quarantine and personal settings from the Appliance. |
199
|
|
|
|
|
|
|
$response is a 0 if successful and a 1 if a error occured. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=cut |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
sub userRemove { |
204
|
|
|
|
|
|
|
my ($class, %args) = @_; |
205
|
|
|
|
|
|
|
my $result = 0; |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
my $email = $args{email}; |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
my $cmd = "config_delete.cgi?account=$email&remove=1"; |
210
|
|
|
|
|
|
|
my ($respcode, $status) = $class->_doRequest(command => $cmd); |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
if ($respcode != 607) { |
213
|
|
|
|
|
|
|
print STDERR "$respcode: Could not remove user $email: $status\n"; |
214
|
|
|
|
|
|
|
$result = 1; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
return $result; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
# Extract response code from XML response, returns status number and |
221
|
|
|
|
|
|
|
# a brief explanation |
222
|
|
|
|
|
|
|
sub _parseResponse{ |
223
|
|
|
|
|
|
|
my $res = pop(@_); |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
my $xs = XML::Simple->new(); |
226
|
|
|
|
|
|
|
my $ref = $xs->XMLin($res); |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
my ($code, $result); |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
if (exists($ref->{Result})) { |
231
|
|
|
|
|
|
|
if (ref($ref->{Result}) eq "HASH") { |
232
|
|
|
|
|
|
|
$code = $ref->{Result}->{Code}; |
233
|
|
|
|
|
|
|
$result = $ref->{Result}->{String}; |
234
|
|
|
|
|
|
|
} else { |
235
|
|
|
|
|
|
|
($code, $result) = split(/:/, $ref->{Result}); |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
} elsif (exists($ref->{Error})) { |
239
|
|
|
|
|
|
|
if (exists($ref->{Error}->{Code})) { |
240
|
|
|
|
|
|
|
$code = $ref->{Error}->{Code}; |
241
|
|
|
|
|
|
|
$result = $ref->{Error}->{String}; |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
} else { |
245
|
|
|
|
|
|
|
print Dumper($ref); |
246
|
|
|
|
|
|
|
die "Unmatched response from Appliance. Cannot continue.\n"; |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
return ($code, $result); |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
sub _doRequest { |
253
|
|
|
|
|
|
|
my ($class, %args) = @_; |
254
|
|
|
|
|
|
|
my $cmd = $args{command}; |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
my @response; |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
my $precmd = $class->{server} . ":" . $class->{port} . "/cgi-bin/"; |
259
|
|
|
|
|
|
|
my $postcmd = "&password=" . $class->{api_key}; |
260
|
|
|
|
|
|
|
$cmd = $precmd . $cmd . $postcmd; |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
263
|
|
|
|
|
|
|
$ua->agent("Mail::Barracuda::API/$VERSION "); |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
my $req = HTTP::Request->new(GET => $cmd); |
266
|
|
|
|
|
|
|
my $res = $ua->request($req); |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
if ($res->is_success) { |
269
|
|
|
|
|
|
|
@response = $class->_parseResponse($res->content); |
270
|
|
|
|
|
|
|
} else { |
271
|
|
|
|
|
|
|
die "Error contacting Appliance: " . $res->status_line, "\n"; |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
$class->_doApply(); |
275
|
|
|
|
|
|
|
return @response; |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
sub _doApply { |
279
|
|
|
|
|
|
|
my ($class, %args) = @_; |
280
|
|
|
|
|
|
|
my $result = 0; |
281
|
|
|
|
|
|
|
my @response; |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
my $cmd = "config_reload.cgi?"; |
284
|
|
|
|
|
|
|
my $precmd = $class->{server} . ":" . $class->{port} . "/cgi-bin/"; |
285
|
|
|
|
|
|
|
my $postcmd = "password=" . $class->{api_key}; |
286
|
|
|
|
|
|
|
$cmd = $precmd . $cmd . $postcmd; |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
289
|
|
|
|
|
|
|
$ua->agent("Mail::Barracuda::API/$VERSION "); |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
my $req = HTTP::Request->new(GET => $cmd); |
292
|
|
|
|
|
|
|
my $res = $ua->request($req); |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
if ($res->is_success) { |
295
|
|
|
|
|
|
|
@response = $class->_parseResponse($res->content); |
296
|
|
|
|
|
|
|
} else { |
297
|
|
|
|
|
|
|
$result = 1; |
298
|
|
|
|
|
|
|
die "Error contacting Appliance: " . $res->status_line, "\n"; |
299
|
|
|
|
|
|
|
} |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
return $result; |
302
|
|
|
|
|
|
|
} |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=head1 SEE ALSO |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
Barracuda API For 3.x firmware. |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
http://www.barracudanetworks.com/ns/downloads/BarracudaAPI-v3x.pdf |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=head1 AUTHOR |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
Jonathan Auer, Ejda@tapodi.netE |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
Copyright (C) 2008 by Jonathan Auer |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
319
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.8 or, |
320
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=cut |