blib/lib/Mail/Barracuda/API.pm

filter: Statementstmt 14.1% 18 / 127 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 40.0% 6 / 15 Podpod 100% 6 / 6 totaltotal 16.7% 30 / 179 CC 15 SCAR 23.4
CC
15
Cov
12%
_parseResponse
30.0
domainAdd
12.0
userAdd
12.0
CRAP 10.3
↑ Summary
1 package Mail::Barracuda::API;
2  
3 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 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 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
49 0 my $invocant = shift;
50 0 ▶ my $class = ref($invocant) || $invocant;
Condition0%0/3
ref $invocant || $invocant
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table0%0/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
51
52 0 my $self = {
53 port => 8000,
54 @_
55 };
56
57 0 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 0 ▶sub userChange {
Subroutine userChange: not called CC=2 SCAR=17.9
Pod documented
75 0 my ($class, %args) = @_;
76 0 my $result = 0;
77
78 0 my $email = $args{email};
79 0 my $var = $args{var};
80 0 my $val = $args{val};
81
82 0 my $cmd = "config_set.cgi?account=$email&variable=$var&value=$val";
83 0 my ($respcode, $status) = $class->_doRequest(command => $cmd);
84 0 ▶ if ($respcode != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($respcode != 200) 0 0 0
85 0 print STDERR "$respcode: Could not change user $email var $var to $val\n";
86 0 $result = 1;
87 }
88
89 0 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 0 ▶sub domainAdd {
Subroutine domainAdd: not called CC=3 SCAR=24.8
Pod documented
105 0 my ($class, %args) = @_;
106 0 my $result = 0;
107
108 0 my $domain = $args{domain};
109 0 my $mailhost = $args{mailhost};
110
111 0 my $cmd = "add_domain.cgi?domain=$domain";
112 0 my ($respcode, $status) = $class->_doRequest(command => $cmd);
113
114 0 ▶ if ($respcode != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($respcode != 200) { } 0 0 0
115 0 print STDERR "Could not add domain $domain: $status\n";
116 0 $result = 1;
117 } else {
118 0 $cmd = "config_set.cgi?variable=mta_relay_advanced_host";
119 0 $cmd .= "&domain=$domain&value=$mailhost";
120 0 $class->_doRequest(command => $cmd);
121 0 ▶ if ($respcode != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($respcode != 200) 0 0 0
122 0 print STDERR "$respcode: Could not set mailhost for $domain: $status\n";
123 0 $result = 1;
124 }
125 }
126
127 0 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 0 ▶sub domainRemove {
Subroutine domainRemove: not called CC=2 SCAR=17.9
Pod documented
140 0 my ($class, %args) = @_;
141 0 my $result = 0;
142
143 0 my $domain = $args{domain};
144
145 0 my $cmd = "delete_domain.cgi?domain=$domain";
146 0 my ($respcode, $status) = $class->_doRequest(command => $cmd);
147 0 ▶ if ($respcode != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($respcode != 200) 0 0 0
148 0 print STDERR "$respcode: Could not remove domain $domain: $status\n";
149 0 $result = 1;
150 }
151 0 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 0 ▶sub userAdd {
Subroutine userAdd: not called CC=3 SCAR=24.8
Pod documented
167 0 my ($class, %args) = @_;
168 0 my $result = 0;
169  
170 0 my $email = $args{email};
171 0 my $pass = $args{password};
172
173 0 my $cmd = "config_add.cgi?account=$email&create=1";
174 0 my ($respcode, $status) = $class->_doRequest(command => $cmd);
175
176 0 ▶ if ($respcode != 708) {
Branch: 1 branch
BranchTrueFalseTotal
if ($respcode != 708) { } 0 0 0
177 0 print STDERR "$respcode: Could not add user $email: $status\n";
178 0 $result = 1;
179 } else {
180 0 my $resp = $class->userChange(
181 email => $email,
182 var => 'user_password',
183 val => $pass,
184 );
185 0 ▶ if ($resp != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp != 0) 0 0 0
186 0 print STDERR "$respcode: Could not set password for user $email: $status\n";
187 0 $result = 1;
188 }
189 }
190
191 0 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 0 ▶sub userRemove {
Subroutine userRemove: not called CC=2 SCAR=17.9
Pod documented
204 0 my ($class, %args) = @_;
205 0 my $result = 0;
206
207 0 my $email = $args{email};
208
209 0 my $cmd = "config_delete.cgi?account=$email&remove=1";
210 0 my ($respcode, $status) = $class->_doRequest(command => $cmd);
211  
212 0 ▶ if ($respcode != 607) {
Branch: 1 branch
BranchTrueFalseTotal
if ($respcode != 607) 0 0 0
213 0 print STDERR "$respcode: Could not remove user $email: $status\n";
214 0 $result = 1;
215 }
216
217 0 return $result;
218 }
219  
220 # Extract response code from XML response, returns status number and
221 # a brief explanation
222 0 ▶sub _parseResponse{
Subroutine _parseResponse: not called CC=5 SCAR=34.0
223 0 my $res = pop(@_);
224
225 0 my $xs = XML::Simple->new();
226 0 my $ref = $xs->XMLin($res);
227
228 0 my ($code, $result);
229
230 0 ▶ if (exists($ref->{Result})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ref->{'Result'}) { } 0 0 0
elsif (exists $ref->{'Error'}) { } 0 0 0
231 0 ▶ if (ref($ref->{Result}) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref->{'Result'} eq 'HASH') { } 0 0 0
232 0 $code = $ref->{Result}->{Code};
233 0 $result = $ref->{Result}->{String};
234 } else {
235 0 ($code, $result) = split(/:/, $ref->{Result});
236 }
237
238 } elsif (exists($ref->{Error})) {
239 0 ▶ if (exists($ref->{Error}->{Code})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ref->{"Error"}{"Code"}) 0 0 0
240 0 $code = $ref->{Error}->{Code};
241 0 $result = $ref->{Error}->{String};
242 }
243
244 } else {
245 0 print Dumper($ref);
246 0 die "Unmatched response from Appliance. Cannot continue.\n";
247 }
248
249 0 return ($code, $result);
250 }
251  
252 0 ▶sub _doRequest {
Subroutine _doRequest: not called CC=2 SCAR=17.9
253 0 my ($class, %args) = @_;
254 0 my $cmd = $args{command};
255
256 0 my @response;
257
258 0 my $precmd = $class->{server} . ":" . $class->{port} . "/cgi-bin/";
259 0 my $postcmd = "&password=" . $class->{api_key};
260 0 $cmd = $precmd . $cmd . $postcmd;
261
262 0 my $ua = LWP::UserAgent->new;
263 0 $ua->agent("Mail::Barracuda::API/$VERSION ");
264
265 0 my $req = HTTP::Request->new(GET => $cmd);
266 0 my $res = $ua->request($req);
267
268 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
269 0 @response = $class->_parseResponse($res->content);
270 } else {
271 0 die "Error contacting Appliance: " . $res->status_line, "\n";
272 }
273
274 0 $class->_doApply();
275 0 return @response;
276 }
277  
278 0 ▶sub _doApply {
Subroutine _doApply: not called CC=2 SCAR=17.9
279 0 my ($class, %args) = @_;
280 0 my $result = 0;
281 0 my @response;
282
283 0 my $cmd = "config_reload.cgi?";
284 0 my $precmd = $class->{server} . ":" . $class->{port} . "/cgi-bin/";
285 0 my $postcmd = "password=" . $class->{api_key};
286 0 $cmd = $precmd . $cmd . $postcmd;
287
288 0 my $ua = LWP::UserAgent->new;
289 0 $ua->agent("Mail::Barracuda::API/$VERSION ");
290
291 0 my $req = HTTP::Request->new(GET => $cmd);
292 0 my $res = $ua->request($req);
293
294 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
295 0 @response = $class->_parseResponse($res->content);
296 } else {
297 0 $result = 1;
298 0 die "Error contacting Appliance: " . $res->status_line, "\n";
299 }
300
301 0 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, E<lt>jda@tapodi.netE<gt>
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
↑ Summary