lib/EMDIS/ECS.pm

filter: Statementstmt 23.4% 155 / 661 Branchbran 9.4% 44 / 466 Conditioncond 5.8% 6 / 102 MC/DCmcdc 6.1% 4 / 65 Subroutinesub 70.9% 39 / 55 Podpod 0.0% 0 / 37 totaltotal 17.8% 248 / 1386 CC 276 SCAR 64.3
CC
276
Cov
17%
send_amqp_message
1806.0
send_email
1474.6
send_ecs_message
1092.4
CRAP 621.1
« lib/EMDIS/ECS/FileBackedMessage.pm ↑ Summary script/ecs_status.pl »
1 #!/usr/bin/perl -w
2 #
3 # Copyright (C) 2002-2026 National Marrow Donor Program. All rights reserved.
4 #
5 # For a description of this module, please refer to the POD documentation
6 # embedded at the bottom of the file (e.g. perldoc EMDIS::ECS).
7  
8 package EMDIS::ECS;
9  
10 6 ▶use Authen::SASL qw(Perl);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use CPAN::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Fcntl qw(:DEFAULT :flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use IPC::Open2;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use Net::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS
Subroutine BEGIN: called CC=1 SCAR=0.0
24 $ECS_CFG $ECS_NODE_TBL $FILEMODE @LOG_LEVEL
25 6 $configured $pidfile $cmd_output $pid_saved);
26  
27 # load OS specific modules at compile time, in BEGIN block
28 BEGIN
29 6 ▶{
Subroutine BEGIN: called CC=2 SCAR=9.2
30 6 ▶ if( $^O =~ /MSWin32/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /MSWin32/) 0 6 6
31 {
32 # Win32 only modules
33 0 eval "require Win32::Process";
34 }
35 }
36  
37 # module/package version
38 6 $VERSION = '0.50';
39  
40 # file creation mode (octal, a la chmod)
41 6 $FILEMODE = 0660;
42  
43 # subclass Exporter and define Exporter set up
44 6 require Exporter;
45 6 @ISA = qw(Exporter);
46 6 @EXPORT = (); # items exported by default
47 6 @EXPORT_OK = (); # items exported by request
48 6 %EXPORT_TAGS = ( # tags for groups of items
49 ALL => [ qw($ECS_CFG $ECS_NODE_TBL $FILEMODE $VERSION
50 load_ecs_config delete_old_files dequote ecs_is_configured
51 log log_debug log_info log_warn log_error log_fatal
52 copy_to_dir move_to_dir read_ecs_message_id
53 send_admin_email send_amqp_message send_ecs_message
54 send_email send_encrypted_message format_datetime
55 format_doc_filename
56 format_msg_filename openpgp_decrypt openpgp_encrypt
57 pgp2_decrypt pgp2_encrypt check_pid save_pid
58 timelimit_cmd remove_pidfile trim valid_encr_typ EOL
59 is_yes is_no get_oauth_token) ] );
60 6 Exporter::export_ok_tags('ALL'); # use tag handling fn to define EXPORT_OK
61  
62 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
63 6 $configured = ''; # boolean indicates whether ECS has been configured
64 6 @LOG_LEVEL = ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'FATAL');
65 6 $pid_saved = '';
66 }
67  
68 # ----------------------------------------------------------------------
69 # Return platform specific end-of-line string
70 sub EOL
71 0 ▶{
Subroutine EOL: not called CC=2 SCAR=17.9
Pod undocumented
72 0 ▶ return "\r\n" if $^O =~ /MSWin32/;
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /MSWin32/ 0 0 0
73 0 return "\n";
74 }
75  
76 # ----------------------------------------------------------------------
77 # test for YES or TRUE
78 sub is_yes
79 95 ▶{
Subroutine is_yes: called CC=4 SCAR=13.9
Pod undocumented
80 95 my $val = shift;
81 95 ▶ return 0 if not defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 95 95
82 95 ▶ return 1 if $val =~ /^\s*YES\s*$/io or $val =~ /^\s*TRUE\s*$/io;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^\s*YES\s*$/io or $val =~ /^\s*TRUE\s*$/io 24 71 95
Condition100%3/3
$val =~ /^\s*YES\s*$/io or $val =~ /^\s*TRUE\s*$/io
l!l&&r!l&&!r
23171
MC/DC100%2/2
$val =~ /^\s*YES\s*$/io or $val =~ /^\s*TRUE\s*$/io
$val =~ /^\s*YES\s*$/io $val =~ /^\s*TRUE\s*$/io
Truth table100%3/3
A or B
A= $val =~ /^\s*YES\s*$/ioB= $val =~ /^\s*TRUE\s*$/io
ABresult
1X1
011
000
83 71 return 0;
84 }
85  
86 # ----------------------------------------------------------------------
87 # test for NO or FALSE
88 sub is_no
89 32 ▶{
Subroutine is_no: called CC=4 SCAR=13.9
Pod undocumented
90 32 my $val = shift;
91 32 ▶ return 0 if not defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 32 32
92 32 ▶ return 1 if $val =~ /^\s*NO\s*$/io or $val =~ /^\s*FALSE\s*$/io;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^\s*NO\s*$/io or $val =~ /^\s*FALSE\s*$/io 25 7 32
Condition100%3/3
$val =~ /^\s*NO\s*$/io or $val =~ /^\s*FALSE\s*$/io
l!l&&r!l&&!r
2417
MC/DC100%2/2
$val =~ /^\s*NO\s*$/io or $val =~ /^\s*FALSE\s*$/io
$val =~ /^\s*NO\s*$/io $val =~ /^\s*FALSE\s*$/io
Truth table100%3/3
A or B
A= $val =~ /^\s*NO\s*$/ioB= $val =~ /^\s*FALSE\s*$/io
ABresult
1X1
011
000
93 7 return 0;
94 }
95  
96 # ----------------------------------------------------------------------
97 # Load ECS configuration into global variables.
98 # returns empty string if successful or error message if error encountered
99 sub load_ecs_config
100 0 ▶{
Subroutine load_ecs_config: not called CC=3 SCAR=24.8
Pod undocumented
101 0 my $cfg_file = shift;
102  
103 0 require EMDIS::ECS::Config;
104 0 my $cfg = new EMDIS::ECS::Config($cfg_file);
105 0 ▶ return "Unable to load ECS configuration ($cfg_file): $cfg"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cfg 0 0 0
106 unless ref $cfg;
107  
108 0 require EMDIS::ECS::LockedHash;
109 0 my $node_tbl = new EMDIS::ECS::LockedHash($cfg->NODE_TBL, $cfg->NODE_TBL_LCK);
110 0 ▶ return "Unable to open ECS node_tbl (" . $cfg->NODE_TBL .
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node_tbl 0 0 0
111 "): $node_tbl"
112 unless ref $node_tbl;
113  
114 0 $pidfile = catfile($cfg->ECS_DAT_DIR, basename($0) . ".pid");
115  
116 # assign values to global config variables
117 0 $ECS_CFG = $cfg;
118 0 $ECS_NODE_TBL = $node_tbl;
119 0 $configured = 1;
120  
121 # successful
122 0 return '';
123 }
124  
125 # ----------------------------------------------------------------------
126 # delete old files (mtime < cutoff time) from specified directory
127 # no recursion
128 sub delete_old_files
129 0 ▶{
Subroutine delete_old_files: not called CC=7 SCAR=40.3
Pod undocumented
130 0 my $dirname = shift;
131 0 my $cutoff_time = shift;
132  
133 0 ▶ if(! -d $dirname)
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dirname) 0 0 0
134 {
135 0 warn "Not a directory name: $dirname";
136 0 return;
137 }
138 0 ▶ if($cutoff_time !~ /^\d+$/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($cutoff_time =~ /^\d+$/) 0 0 0
139 {
140 0 warn "Cutoff time not numeric: $cutoff_time";
141 0 return;
142 }
143 0 opendir DELDIR, $dirname;
144 0 my @names = readdir DELDIR;
145 0 closedir DELDIR;
146 0 foreach my $name (@names)
147 {
148 0 my $filename = catfile($dirname, $name);
149 0 ▶ next unless -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 0 0
150 # delete file if mtime < $cutoff_time
151 0 my @stat = stat $filename;
152 0 ▶ if($stat[9] < $cutoff_time)
Branch: 1 branch
BranchTrueFalseTotal
if ($stat[9] < $cutoff_time) 0 0 0
153 {
154 0 ▶ unlink $filename
Branch: 1 branch
BranchTrueFalseTotal
unlink $filename or warn "Unable to delete file: $filename" 0 0 0
155 or warn "Unable to delete file: $filename";
156 }
157 }
158 }
159  
160 # ----------------------------------------------------------------------
161 # Return string value with enclosing single or double quotes removed.
162 12 ▶sub dequote {
Subroutine dequote: called CC=4 SCAR=13.9
Pod undocumented
163 12 my $str = shift;
164 12 ▶ return if not defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 12 12
165 12 ▶ if($str =~ /^"(.*)"$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /^"(.*)"$/) { } 5 7 12
elsif ($str =~ /^'(.*)'$/) { } 1 6 7
166 5 $str = $1;
167 }
168 elsif($str =~ /^'(.*)'$/) {
169 1 $str = $1;
170 }
171 12 return $str;
172 }
173  
174 # ----------------------------------------------------------------------
175 # Return boolean indicating whether ECS has been configured.
176 64 ▶sub ecs_is_configured {
Subroutine ecs_is_configured: called CC=1 SCAR=0.0
Pod undocumented
177 64 return $configured;
178 }
179  
180 # ----------------------------------------------------------------------
181 # Write message to ECS log file. Takes two arguments: a level which is
182 # used to classify logged messages and the text to be logged.
183 # Push an aditional email to admin if the error is encountering
184 # the MAIL_LEVEL.
185 # Returns empty string if successful or error message if error encountered.
186 6 ▶sub log {
Subroutine log: called CC=12 SCAR=47.6
Pod undocumented
187 6 ▶ if(not ecs_is_configured()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ecs_is_configured()) 6 0 6
188 6 my $warning = "EMDIS::ECS::log(): ECS has not been configured.";
189 6 warn "$warning\n";
190 6 return $warning;
191 }
192 0 my $cfg = $ECS_CFG;
193 0 my $level = shift;
194 0 ▶ $level = '1' if (not defined $level) or
Branch: 1 branch
BranchTrueFalseTotal
if not defined $level or $level < 0 or $level > $#EMDIS::ECS::LOG_LEVEL 0 0 0
Condition0%0/6
not defined $level or $level < 0 or $level > $#EMDIS::ECS::LOG_LEVEL
l!l&&r!l&&!r
000
not defined $level or $level < 0
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $level or $level < 0 or $level > $#EMDIS::ECS::LOG_LEVEL
not defined $level $level < 0 $level > $#EMDIS::ECS::LOG_LEVEL
Truth table0%0/4
not(A) or B or C
A= not defined $levelB= $level < 0C= $level > $#EMDIS::ECS::LOG_LEVEL
ABCresult
1XX1
01X1
0011
0000
195 ($level < 0) or ($level > $#LOG_LEVEL);
196 0 ▶ return if $level < $cfg->LOG_LEVEL && ! $cfg->ECS_DEBUG; # check log-level
Branch: 1 branch
BranchTrueFalseTotal
if $level < $cfg->LOG_LEVEL and not $cfg->ECS_DEBUG 0 0 0
Condition0%0/3
$level < $cfg->LOG_LEVEL and not $cfg->ECS_DEBUG
!ll&&!rl&&r
000
MC/DC0%0/2
$level < $cfg->LOG_LEVEL and not $cfg->ECS_DEBUG
$level < $cfg->LOG_LEVEL not $cfg->ECS_DEBUG
Truth table0%0/3
A and not(B)
A= $level < $cfg->LOG_LEVELB= not $cfg->ECS_DEBUG
ABresult
0X0
100
111
197 0 my $text = join("\n ", @_);
198 0 ▶ $text = '' if not defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
199 0 my $timestamp = localtime;
200 0 my $origin = $0;
201  
202 0 my $log_msg = join("|",$timestamp,$origin,$LOG_LEVEL[$level],$text);
203 0 ▶ if('__STDOUT__' eq $cfg->LOG_FILE) {
Branch: 1 branch
BranchTrueFalseTotal
if ('__STDOUT__' eq $cfg->LOG_FILE) { } 0 0 0
204 0 print "$log_msg\n";
205 }
206 else {
207 0 my $setmode = not -e $cfg->LOG_FILE;
208 0 ▶ if(open LOG, ">>" . $cfg->LOG_FILE) {
Branch: 1 branch
BranchTrueFalseTotal
if (open LOG, '>>' . $cfg->LOG_FILE) { } 0 0 0
209 0 print LOG "$log_msg\n";
210 0 close LOG;
211 0 ▶ chmod $FILEMODE, $cfg->LOG_FILE if $setmode;
Branch: 1 branch
BranchTrueFalseTotal
if $setmode 0 0 0
212 }
213 else {
214 0 warn "Error within ECS library: $! " . $cfg->LOG_FILE;
215 0 print "$log_msg\n";
216 };
217 }
218 0 ▶ if ( $level >= $cfg->MAIL_LEVEL )
Branch: 1 branch
BranchTrueFalseTotal
if ($level >= $cfg->MAIL_LEVEL) 0 0 0
219 {
220 0 send_admin_email ($log_msg);
221 }
222 0 return '';
223 }
224 # logging subroutines for specific logging levels
225 1 ▶sub log_debug { return &log(0, @_); }
Subroutine log_debug: called CC=1 SCAR=0.0
Pod undocumented
226 1 ▶sub log_info { return &log(1, @_); }
Subroutine log_info: called CC=1 SCAR=0.0
Pod undocumented
227 1 ▶sub log_warn { return &log(2, @_); }
Subroutine log_warn: called CC=1 SCAR=0.0
Pod undocumented
228 1 ▶sub log_error { return &log(3, @_); }
Subroutine log_error: called CC=1 SCAR=0.0
Pod undocumented
229 1 ▶sub log_fatal { return &log(4, @_); }
Subroutine log_fatal: called CC=1 SCAR=0.0
Pod undocumented
230  
231 # ----------------------------------------------------------------------
232 # Copy file to specified directory. If necessary, rename file to avoid
233 # filename collision.
234 # Returns empty string if successful or error message if error encountered.
235 0 ▶sub copy_to_dir {
Subroutine copy_to_dir: not called CC=6 SCAR=37.4
Pod undocumented
236 0 my $filename = shift;
237 0 my $targetdir = shift;
238 0 my $err;
239  
240 0 ▶ return "file not found: $filename" unless -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 0 0
241 0 ▶ return "directory not found: $targetdir" unless -d $targetdir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $targetdir 0 0 0
242  
243 # do some fancy footwork to avoid name collision in target dir,
244 # then copy file
245 0 my $basename = basename($filename);
246 0 my $template = $basename;
247 0 my $suffix = '';
248 0 ▶ if($basename =~ /^(\d{8}_\d{6}_(.+_)?).{4}(\..{3})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($basename =~ /^(\d{8}_\d{6}_(.+_)?).{4}(\..{3})$/) { } 0 0 0
249 0 $template = "$1XXXX";
250 0 $suffix = $3;
251 }
252 else {
253 0 $template .= '_XXXX';
254 }
255 0 my ($fh, $tempfilename) = tempfile($template,
256 DIR => $targetdir,
257 SUFFIX => $suffix);
258 0 ▶ return "unable to open tempfile in directory $targetdir: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
259 unless $fh;
260 0 ▶ $err = "unable to copy $filename to $tempfilename: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless copy($filename, $fh) 0 0 0
261 unless copy($filename, $fh);
262 0 close $fh;
263 0 chmod $FILEMODE, $tempfilename;
264 0 return $err;
265 }
266  
267 # ----------------------------------------------------------------------
268 # Move file to specified directory. If necessary, rename file to avoid
269 # filename collision.
270 # Returns empty string if successful or error message if error encountered.
271 0 ▶sub move_to_dir {
Subroutine move_to_dir: not called CC=2 SCAR=17.9
Pod undocumented
272 0 my $filename = shift;
273 0 my $targetdir = shift;
274  
275 0 my $err = copy_to_dir($filename, $targetdir);
276 0 ▶ unlink $filename unless $err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 0 0 0
277 0 return $err;
278 }
279  
280 # ----------------------------------------------------------------------
281 # Execute AUTHN_OAUTH_TOKEN_CMD to get OAuth access token
282 # Returns two-element list: ($err, $access_token)
283 0 ▶sub get_oauth_token {
Subroutine get_oauth_token: not called CC=3 SCAR=24.8
Pod undocumented
284 0 my $timelimit = shift;
285 0 my $token_cmd = shift;
286 0 my $desc = shift;
287  
288 # execute command
289 0 my $err = timelimit_cmd($timelimit, $token_cmd);
290 0 ▶ return("EMDIS::ECS::get_oauth_token(): $desc command execution failed: $err", undef)
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
291 if $err;
292  
293 # get command output from module-level variable
294 0 ▶ my $access_token = $EMDIS::ECS::cmd_output
Branch: 1 branch
BranchTrueFalseTotal
my $access_token = $EMDIS::ECS::cmd_output or return "EMDIS::ECS::get_oauth_token(): $desc command returned no access token", undef 0 0 0
295 or return("EMDIS::ECS::get_oauth_token(): $desc command returned no access token", undef);
296  
297 0 chomp $access_token; # remove any trailing EOL, just in case
298 0 return('', $access_token);
299 }
300  
301 # ----------------------------------------------------------------------
302 # Read ECS message id from specified file. File is presumed to be in the
303 # format of an email message; message id is comprised of node_id and seq_num,
304 # with optional $part_num and $num_parts or DOC suffix.
305 # Returns empty array if unable to retrieve ECS message id from file.
306 sub read_ecs_message_id
307 10 ▶{
Subroutine read_ecs_message_id: called CC=9 SCAR=22.0
Pod undocumented
308 10 my $filename = shift;
309  
310 10 ▶ return "EMDIS::ECS::read_ecs_message_id(): ECS has not been configured."
Branch: 1 branch
BranchTrueFalseTotal
unless ecs_is_configured() 1 9 10
311 unless ecs_is_configured();
312 9 my $mail_mrk = $ECS_CFG->MAIL_MRK;
313  
314 9 my $fh = new IO::File;
315 9 ▶ return () unless $fh->open("< $filename");
Branch: 1 branch
BranchTrueFalseTotal
unless $fh->open("< $filename") 0 9 9
316 9 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 11 0 11
317 11 ▶ /^Subject:.*$mail_mrk:(\S+?):(\d+):(\d+)\/(\d+)\s*$/io and do {
Branch: 1 branch
BranchTrueFalseTotal
m[^Subject:.*$mail_mrk:(\S+?):(\d+):(\d+)/(\d+)\s*$]io and do { return $1, $2, $3, $4, 0 } 1 10 11
318 1 return ($1,$2,$3,$4,0);
319 };
320 10 ▶ /^Subject:.*$mail_mrk:(\S+?):(\d+)\s*$/io and do {
Branch: 1 branch
BranchTrueFalseTotal
/^Subject:.*$mail_mrk:(\S+?):(\d+)\s*$/io and do { return $1, $2, 1, 1, 0 } 4 6 10
321 4 return ($1,$2,1,1,0);
322 };
323 6 ▶ /^Subject:.*$mail_mrk:(\S+?):(\d+):DOC\s*$/io and do {
Branch: 1 branch
BranchTrueFalseTotal
/^Subject:.*$mail_mrk:(\S+?):(\d+):DOC\s*$/io and do { return $1, $2, 1, 1, 1 } 1 5 6
324 1 return ($1,$2,1,1,1);
325 };
326 5 ▶ /^Subject:.*$mail_mrk:(\S+)\s*$/io and do {
Branch: 1 branch
BranchTrueFalseTotal
/^Subject:.*$mail_mrk:(\S+)\s*$/io and do { return $1, undef, undef, undef, 0 } 1 4 5
327 1 return ($1,undef,undef,undef,0);
328 };
329 4 ▶ /^$/ and last; # blank line marks end of mail headers
Branch: 1 branch
BranchTrueFalseTotal
/^$/ and last 2 2 4
330 }
331 2 close $fh;
332 2 return (); # return empty array
333 }
334  
335 # ----------------------------------------------------------------------
336 # Send email to administrator and also archive the email message in the
337 # mboxes/out directory. Takes one or more arguments: the body lines to
338 # be emailed.
339 # Returns empty string if successful or error message if error encountered.
340 # Also logs error if error encountered.
341 1 ▶sub send_admin_email {
Subroutine send_admin_email: called CC=9 SCAR=36.7
Pod undocumented
342  
343 1 my $err = '';
344 1 ▶ $err = "EMDIS::ECS::send_admin_email(): ECS has not been configured."
Branch: 1 branch
BranchTrueFalseTotal
unless ecs_is_configured() 1 0 1
345 unless ecs_is_configured();
346 1 my $cfg = $ECS_CFG;
347  
348 # record message contents in 'out' file
349 1 ▶ if(not $err) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err) 0 1 1
350 0 my $template = format_datetime(time, '%04d%02d%02d_%02d%02d%02d_XXXX');
351 0 my ($fh, $filename) = tempfile($template,
352 DIR => $cfg->ECS_MBX_OUT_DIR,
353 SUFFIX => '.msg');
354 0 ▶ $err = "EMDIS::ECS::send_admin_email(): unable to create 'out' file"
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
355 unless $fh;
356 0 ▶ if($fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) 0 0 0
357 0 print $fh @_;
358 0 close $fh;
359 0 chmod $FILEMODE, $filename;
360 }
361 }
362  
363 1 ▶ if(not $err)
Branch: 1 branch
BranchTrueFalseTotal
unless ($err) 0 1 1
364 {
365 0 my @recipients = split /,/, $cfg->ADM_ADDR;
366 0 foreach my $recipient (@recipients)
367 {
368 0 $err = send_email($recipient, '[' . $cfg->MAIL_MRK . '] ECS Error',
369 undef, "Origin: $0\n", @_);
370  
371 0 ▶ log_error("Unable to send admin email to $recipient: $err")
Branch: 1 branch
BranchTrueFalseTotal
if $err and not $_[$#_] =~ /Unable to send admin email/iso 0 0 0
Condition0%0/3
$err and not $_[$#_] =~ /Unable to send admin email/iso
!ll&&!rl&&r
000
MC/DC0%0/2
$err and not $_[$#_] =~ /Unable to send admin email/iso
$err not $_[$#_] =~ /Unable to send admin email/iso
Truth table0%0/3
A and not(B)
A= $errB= not $_[$#_] =~ /Unable to send admin email/iso
ABresult
0X0
100
111
372 if $err and $_[$#_] !~ /Unable to send admin email/iso;
373 }
374 }
375  
376 1 return $err;
377 }
378  
379 # ----------------------------------------------------------------------
380 # Send ECS email message.
381 # Returns empty string if successful or error message if error encountered.
382 1 ▶sub send_ecs_message {
Subroutine send_ecs_message: called CC=34 SCAR=70.0
Pod undocumented
383 1 my $node_id = shift;
384 1 my $seq_num = shift;
385 # @_ now contains message body
386  
387 # initialize
388 1 ▶ return "EMDIS::ECS::send_ecs_message(): ECS has not been configured."
Branch: 1 branch
BranchTrueFalseTotal
unless ecs_is_configured() 1 0 1
389 unless ecs_is_configured();
390 0 my $cfg = $ECS_CFG;
391 0 my $node_tbl = $ECS_NODE_TBL;
392 0 my $err = '';
393  
394 # do some validation
395 0 my ($hub_rcv, $hub_snd);
396 0 ▶ if($seq_num && !$node_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq_num and not $node_id) 0 0 0
Condition0%0/3
$seq_num and not $node_id
!ll&&!rl&&r
000
MC/DC0%0/2
$seq_num and not $node_id
$seq_num not $node_id
Truth table0%0/3
A and not(B)
A= $seq_numB= not $node_id
ABresult
0X0
100
111
397 # parse FML to determing $node_id:
398 # do some cursory validation, extract HUB_RCV and HUB_SND
399 0 my $fml = join('', @_);
400 0 ▶ return "EMDIS::ECS::send_ecs_message(): message does not contain valid FML"
Branch: 1 branch
BranchTrueFalseTotal
unless $fml =~ /^.+:.+;/s 0 0 0
401 unless $fml =~ /^.+:.+;/s;
402 0 ▶ if($fml =~ /HUB_RCV\s*=\s*([^,;]+)/is) { # presumes [^,;] in HUB_RCV
Branch: 1 branch
BranchTrueFalseTotal
if ($fml =~ /HUB_RCV\s*=\s*([^,;]+)/is) { } 0 0 0
403 0 $hub_rcv = dequote(trim($1));
404 }
405 else {
406 0 return "EMDIS::ECS::send_ecs_message(): message does not specify " .
407 "HUB_RCV";
408 }
409 0 ▶ if($fml =~ /HUB_SND\s*=\s*([^,;]+)/is) { # presumes [^,;] in HUB_SND
Branch: 1 branch
BranchTrueFalseTotal
if ($fml =~ /HUB_SND\s*=\s*([^,;]+)/is) { } 0 0 0
410 0 $hub_snd = dequote(trim($1));
411 }
412 else {
413 0 return "EMDIS::ECS::send_ecs_message(): message does not specify " .
414 "HUB_SND";
415 }
416 0 ▶ return "EMDIS::ECS::send_ecs_message(): HUB_SND is incorrect: $hub_snd"
Branch: 1 branch
BranchTrueFalseTotal
unless $hub_snd eq $EMDIS::ECS::ECS_CFG->THIS_NODE 0 0 0
417 unless $hub_snd eq $ECS_CFG->THIS_NODE;
418 0 ▶ $node_id = $hub_rcv unless $node_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $node_id 0 0 0
419 0 ▶ return "EMDIS::ECS::send_ecs_message(): node_id ($node_id) and FML " .
Branch: 1 branch
BranchTrueFalseTotal
unless $node_id eq $hub_rcv 0 0 0
420 "HUB_RCV ($hub_rcv) do not match"
421 unless $node_id eq $hub_rcv;
422 }
423  
424 # look up specified node in node_tbl
425 0 my $was_locked = $node_tbl->LOCK;
426 0 ▶ if(not $was_locked) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($was_locked) 0 0 0
427 0 ▶ $node_tbl->lock() # lock node_tbl if needed
Branch: 1 branch
BranchTrueFalseTotal
$node_tbl->lock or return 'EMDIS::ECS::send_ecs_message(): unable to lock node_tbl: ' . $node_tbl->ERROR 0 0 0
428 or return "EMDIS::ECS::send_ecs_message(): unable to lock node_tbl: " .
429 $node_tbl->ERROR;
430 }
431 0 my $node = $node_tbl->read($node_id);
432 0 ▶ if(not $node) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node) 0 0 0
433 0 ▶ $node_tbl->unlock() unless $was_locked; # unlock node_tbl if needed
Branch: 1 branch
BranchTrueFalseTotal
unless $was_locked 0 0 0
434 0 return "EMDIS::ECS::send_ecs_message(): node not found: " . $node_id;
435 }
436 0 ▶ if(not $node->{addr}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->{"addr"}) 0 0 0
437 0 ▶ $node_tbl->unlock() unless $was_locked; # unlock node_tbl if needed
Branch: 1 branch
BranchTrueFalseTotal
unless $was_locked 0 0 0
438 0 return "EMDIS::ECS::send_ecs_message(): addr not defined for node: $node_id";
439 }
440 0 ▶ if($seq_num =~ /auto/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq_num =~ /auto/i) 0 0 0
441 # automatically get next sequence number
442 0 $node->{out_seq}++;
443 0 $seq_num = $node->{out_seq};
444 }
445  
446 0 my $subject = $cfg->MAIL_MRK . ':' . $cfg->THIS_NODE;
447 0 ▶ $subject .= ":$seq_num" if $seq_num;
Branch: 1 branch
BranchTrueFalseTotal
if $seq_num 0 0 0
448  
449 0 my $filename;
450  
451 # if not meta-message, copy to mboxes/out_NODE subdirectory
452 0 ▶ if($seq_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq_num) { } 0 0 0
453 0 $filename = format_msg_filename($node_id,$seq_num);
454 # create directory if it doesn't already exist
455 0 my $dirname = dirname($filename);
456 0 ▶ mkdir $dirname unless -e $dirname;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dirname 0 0 0
457 }
458 else {
459 # if meta-message, copy to mboxes/out subdirectory
460 0 $filename = sprintf("%s_%s_%s.msg",
461 $cfg->THIS_NODE, $node_id, "META");
462 0 my $dirname = $cfg->ECS_MBX_OUT_DIR;
463 # create directory if it doesn't already exist
464 0 ▶ mkdir $dirname unless -e $dirname;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dirname 0 0 0
465 0 $filename = catfile($dirname, $filename);
466 }
467  
468 # don't overwrite $filename file if it already exists
469 0 my $fh;
470 0 ▶ if(-e $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename) { } 0 0 0
471 0 my $template = $filename . "_XXXX";
472 0 ($fh, $filename) = tempfile($template);
473 0 ▶ return "EMDIS::ECS::send_ecs_message(): unable to open _XXXX file: " .
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
474 "$filename"
475 unless $fh;
476 }
477 else {
478 0 $fh = new IO::File;
479 0 ▶ return "EMDIS::ECS::send_ecs_message(): unable to open file: " .
Branch: 1 branch
BranchTrueFalseTotal
unless $fh->open("> $filename") 0 0 0
480 "$filename"
481 unless $fh->open("> $filename");
482 }
483  
484 0 $fh->print("Subject: $subject\n");
485 0 $fh->print("To: $node->{addr}\n");
486 0 $fh->print("From: " . $cfg->SMTP_FROM . "\n\n");
487 0 $fh->print(@_);
488 0 $fh->close();
489 0 chmod $FILEMODE, $filename;
490  
491 0 ▶ if ( $err ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err) { } 0 0 0
elsif (not $seq_num || $node->{'encr_meta'} =~ /true/i) { } 0 0 0
492 0 $err = "EMDIS::ECS::send_ecs_message(): unable to update node $node_id: $err";
493 }
494 ▶ elsif ( not $seq_num and ($node->{encr_meta} !~ /true/i) ) {
Condition0%0/3
$seq_num || $node->{'encr_meta'} =~ /true/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$seq_num || $node->{'encr_meta'} =~ /true/i
$seq_num $node->{'encr_meta'} =~ /true/i
Truth table0%0/3
A || B
A= $seq_numB= $node->{'encr_meta'} =~ /true/i
ABresult
1X1
011
000
495 # if indicated, don't encrypt meta-message
496 0 ▶ if(is_yes($cfg->ENABLE_AMQP) and exists $node->{amqp_addr_meta} and $node->{amqp_addr_meta}) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_yes($cfg->ENABLE_AMQP) and exists $node->{'amqp_addr_meta'} and $node->{'amqp_addr_meta'}) { } 0 0 0
elsif (is_yes($node->{'amqp_only'})) { } 0 0 0
Condition0%0/6
is_yes($cfg->ENABLE_AMQP) and exists $node->{"amqp_addr_meta"} and $node->{"amqp_addr_meta"}
!ll&&!rl&&r
000
is_yes($cfg->ENABLE_AMQP) and exists $node->{"amqp_addr_meta"}
!ll&&!rl&&r
000
MC/DC0%0/3
is_yes($cfg->ENABLE_AMQP) and exists $node->{"amqp_addr_meta"} and $node->{"amqp_addr_meta"}
is_yes($cfg->ENABLE_AMQP) exists $node->{"amqp_addr_meta"} $node->{"amqp_addr_meta"}
Truth table0%0/4
A and B and C
A= is_yes($cfg->ENABLE_AMQP)B= exists $node->{"amqp_addr_meta"}C= $node->{"amqp_addr_meta"}
ABCresult
0XX0
10X0
1100
1111
497 # send meta-message via AMQP (if indicated by node config)
498 $err = send_amqp_message(
499 $node->{amqp_addr_meta},
500 0 $subject,
501 $node,
502 undef,
503 @_);
504 }
505 elsif(is_yes($node->{amqp_only})) {
506 $err = "EMDIS::ECS::send_ecs_message(): unable to send " .
507 "email META message to node " . $node->{node} .
508 0 ": amqp_only selected.";
509 }
510 else {
511 0 $err = send_email($node->{addr}, $subject, undef, @_);
512 }
513 }
514 else {
515 # otherwise, send encrypted message
516 $err = send_encrypted_message(
517 $node->{encr_typ},
518 $node->{addr_r},
519 $node->{addr},
520 $node->{encr_out_keyid},
521 $node->{encr_out_passphrase},
522 0 $node,
523 $subject,
524 undef,
525 @_);
526 }
527  
528 0 ▶ if ( ! $err ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err) 0 0 0
529 # update node last_out, possibly out_seq
530 0 $node->{last_out} = time();
531 0 ▶ $err = $node_tbl->ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless $node_tbl->write($node_id, $node) 0 0 0
532 unless $node_tbl->write($node_id, $node);
533 }
534 $node_tbl->unlock() # unlock node_tbl
535 0 ▶ unless $was_locked;
Branch: 1 branch
BranchTrueFalseTotal
unless $was_locked 0 0 0
536  
537 0 return $err;
538 }
539  
540 # ----------------------------------------------------------------------
541 # Send email message. Takes four or more arguments: the recipient,
542 # subject line, custom headers (hash ref), and body lines to be emailed.
543 # Returns empty string if successful or error message if error encountered.
544 1 ▶sub send_email {
Subroutine send_email: called CC=40 SCAR=73.0
Pod undocumented
545 1 my $recipient = shift;
546 1 my $subject = shift;
547 1 my $custom_headers = shift;
548 # @_ now contains message body
549  
550 1 ▶ return "EMDIS::ECS::send_email(): ECS has not been configured."
Branch: 1 branch
BranchTrueFalseTotal
unless ecs_is_configured() 1 0 1
551 unless ecs_is_configured();
552 0 my $cfg = $ECS_CFG;
553  
554 0 ▶ return "EMDIS::ECS::send_email(): custom_headers must be undef or HASH ref (found " .
Branch: 1 branch
BranchTrueFalseTotal
if defined $custom_headers and not "HASH" eq ref $custom_headers 0 0 0
Condition0%0/3
defined $custom_headers and not "HASH" eq ref $custom_headers
!ll&&!rl&&r
000
MC/DC0%0/2
defined $custom_headers and not "HASH" eq ref $custom_headers
defined $custom_headers not "HASH" eq ref $custom_headers
Truth table0%0/3
A and not(B)
A= defined $custom_headersB= not "HASH" eq ref $custom_headers
ABresult
0X0
100
111
555 ref($custom_headers) . ")"
556 if defined $custom_headers and not 'HASH' eq ref $custom_headers;
557  
558 0 my $smtp;
559 0 ▶ if(is_yes($cfg->SMTP_USE_SSL) or is_yes($cfg->SMTP_USE_STARTTLS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_yes($cfg->SMTP_USE_SSL) or is_yes($cfg->SMTP_USE_STARTTLS)) 0 0 0
Condition0%0/3
is_yes($cfg->SMTP_USE_SSL) or is_yes($cfg->SMTP_USE_STARTTLS)
l!l&&r!l&&!r
000
MC/DC0%0/2
is_yes($cfg->SMTP_USE_SSL) or is_yes($cfg->SMTP_USE_STARTTLS)
is_yes($cfg->SMTP_USE_SSL) is_yes($cfg->SMTP_USE_STARTTLS)
Truth table0%0/3
A or B
A= is_yes($cfg->SMTP_USE_SSL)B= is_yes($cfg->SMTP_USE_STARTTLS)
ABresult
1X1
011
000
560 0 ▶ return "To use SSL or TLS please install Net::SMTP with version >= 3.05"
Branch: 1 branch
BranchTrueFalseTotal
if "CPAN::Version"->vlt($Net::SMTP::VERSION, "3.05") 0 0 0
561 if CPAN::Version->vlt($Net::SMTP::VERSION, '3.05');
562 }
563 0 ▶ if(is_yes($cfg->SMTP_USE_SSL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_yes($cfg->SMTP_USE_SSL)) { } 0 0 0
564 0 ▶ $smtp = Net::SMTP->new($cfg->SMTP_HOST,
Branch: 1 branch
BranchTrueFalseTotal
$smtp = "Net::SMTP"->new($cfg->SMTP_HOST, "Hello", $cfg->SMTP_DOMAIN, "Timeout", $cfg->SMTP_TIMEOUT, "Debug", $cfg->SMTP_DEBUG, "Port", $cfg->SMTP_PORT, "SSL", 1) or return 'Unable to open SMTP connection to ' . $cfg->SMTP_HOST . ": $@" 0 0 0
565 Hello => $cfg->SMTP_DOMAIN,
566 Timeout => $cfg->SMTP_TIMEOUT,
567 Debug => $cfg->SMTP_DEBUG,
568 Port => $cfg->SMTP_PORT,
569 SSL => 1)
570 or return "Unable to open SMTP connection to " .
571 $cfg->SMTP_HOST . ": $@";
572 }
573 else {
574 0 ▶ $smtp = Net::SMTP->new($cfg->SMTP_HOST,
Branch: 1 branch
BranchTrueFalseTotal
$smtp = "Net::SMTP"->new($cfg->SMTP_HOST, "Hello", $cfg->SMTP_DOMAIN, "Timeout", $cfg->SMTP_TIMEOUT, "Debug", $cfg->SMTP_DEBUG, "Port", $cfg->SMTP_PORT) or return 'Unable to open SMTP connection to ' . $cfg->SMTP_HOST . ": $@" 0 0 0
575 Hello => $cfg->SMTP_DOMAIN,
576 Timeout => $cfg->SMTP_TIMEOUT,
577 Debug => $cfg->SMTP_DEBUG,
578 Port => $cfg->SMTP_PORT)
579 or return "Unable to open SMTP connection to " .
580 $cfg->SMTP_HOST . ": $@";
581 0 ▶ if(is_yes($cfg->SMTP_USE_STARTTLS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_yes($cfg->SMTP_USE_STARTTLS)) 0 0 0
582 0 ▶ if(not $smtp->starttls()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($smtp->starttls) 0 0 0
583 0 my $err = "STARTTLS failed: " . $smtp->message();
584 0 $smtp->quit();
585 0 return $err;
586 }
587 }
588 }
589 0 ▶ if($cfg->SMTP_OAUTH_TOKEN_CMD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->SMTP_OAUTH_TOKEN_CMD) { } 0 0 0
590 0 ▶ if(!is_yes($ECS_CFG->SMTP_USE_SSL) and !is_yes($ECS_CFG->SMTP_USE_STARTTLS)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_yes($EMDIS::ECS::ECS_CFG->SMTP_USE_SSL) or is_yes($EMDIS::ECS::ECS_CFG->SMTP_USE_STARTTLS)) 0 0 0
Condition0%0/3
is_yes($EMDIS::ECS::ECS_CFG->SMTP_USE_SSL) or is_yes($EMDIS::ECS::ECS_CFG->SMTP_USE_STARTTLS)
l!l&&r!l&&!r
000
MC/DC0%0/2
is_yes($EMDIS::ECS::ECS_CFG->SMTP_USE_SSL) or is_yes($EMDIS::ECS::ECS_CFG->SMTP_USE_STARTTLS)
is_yes($EMDIS::ECS::ECS_CFG->SMTP_USE_SSL) is_yes($EMDIS::ECS::ECS_CFG->SMTP_USE_STARTTLS)
Truth table0%0/3
A or B
A= is_yes($EMDIS::ECS::ECS_CFG->SMTP_USE_SSL)B= is_yes($EMDIS::ECS::ECS_CFG->SMTP_USE_STARTTLS)
ABresult
1X1
011
000
591 0 $smtp->quit();
592 0 return "Unable to use SMTP SASL OAuth authentication without SSL/TLS.";
593 }
594  
595 0 ▶ return "To use SASL authentication mechanisms XOAUTH2 or OAUTHBEARER " .
Branch: 1 branch
BranchTrueFalseTotal
if "CPAN::Version"->vlt($Authen::SASL::Perl::VERSION, "2.1800") 0 0 0
596 "please install Authen::SASL::Perl with version >= 2.1800"
597 if CPAN::Version->vlt($Authen::SASL::Perl::VERSION, '2.1800');
598  
599 # using SASL XOAUTH2 or OAUTHBEARER authentication
600 0 my ($err, $access_token) = get_oauth_token(
601 $cfg->SMTP_OAUTH_TOKEN_CMD_TIMELIMIT,
602 $cfg->SMTP_OAUTH_TOKEN_CMD,
603 'SMTP_OAUTH_TOKEN_CMD');
604 0 ▶ if($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
605 0 $smtp->quit();
606 0 return "Unable to get access token: $err";
607 }
608  
609 0 my $sasl = Authen::SASL->new(
610 mechanism => $ECS_CFG->SMTP_OAUTH_SASL_MECHANISM,
611 callback => {
612 user => $ECS_CFG->SMTP_USERNAME,
613 pass => $access_token,
614 }
615 );
616 0 ▶ if(not $sasl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sasl) 0 0 0
617 0 $smtp->quit();
618 0 return("Unable to construct Authen::SASL object (SMTP).", undef);
619 }
620  
621 0 ▶ if(not $smtp->auth($sasl)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($smtp->auth($sasl)) 0 0 0
622 0 my $err = $smtp->message();
623 0 $smtp->quit();
624 0 return "Unable to authenticate via SASL OAuth method to " . $cfg->SMTP_DOMAIN .
625 " SMTP server: $err";
626 }
627 }
628 else {
629 # using username/password authentication
630 0 ▶ if($cfg->SMTP_USERNAME and $cfg->SMTP_PASSWORD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->SMTP_USERNAME and $cfg->SMTP_PASSWORD) 0 0 0
Condition0%0/3
$cfg->SMTP_USERNAME and $cfg->SMTP_PASSWORD
!ll&&!rl&&r
000
MC/DC0%0/2
$cfg->SMTP_USERNAME and $cfg->SMTP_PASSWORD
$cfg->SMTP_USERNAME $cfg->SMTP_PASSWORD
Truth table0%0/3
A and B
A= $cfg->SMTP_USERNAMEB= $cfg->SMTP_PASSWORD
ABresult
0X0
100
111
631 0 ▶ if(not $smtp->auth($cfg->SMTP_USERNAME, $cfg->SMTP_PASSWORD)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($smtp->auth($cfg->SMTP_USERNAME, $cfg->SMTP_PASSWORD)) 0 0 0
632 0 my $err = "Unable to authenticate with " . $cfg->SMTP_DOMAIN .
633 " SMTP server as user " . $cfg->SMTP_USERNAME . ": " .
634 $smtp->message();
635 0 $smtp->quit();
636 0 return $err;
637 }
638 }
639 }
640 0 ▶ $smtp->mail($cfg->SMTP_FROM)
Branch: 1 branch
BranchTrueFalseTotal
$smtp->mail($cfg->SMTP_FROM) or return "Unable to initiate sending of email message." 0 0 0
641 or return "Unable to initiate sending of email message.";
642 0 ▶ $smtp->to($recipient)
Branch: 1 branch
BranchTrueFalseTotal
$smtp->to($recipient) or return "Unable to define email recipient." 0 0 0
643 or return "Unable to define email recipient.";
644 0 ▶ $smtp->data()
Branch: 1 branch
BranchTrueFalseTotal
$smtp->data or return "Unable to start sending of email data." 0 0 0
645 or return "Unable to start sending of email data.";
646 0 ▶ if(defined $custom_headers)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $custom_headers) 0 0 0
647 {
648 0 for my $key (keys %$custom_headers)
649 {
650 0 my $value = $custom_headers->{$key};
651 0 ▶ $smtp->datasend("$key: $value\n")
Branch: 1 branch
BranchTrueFalseTotal
$smtp->datasend("${key}: $value\n") or return "Unable to send email data." 0 0 0
652 or return "Unable to send email data.";
653 }
654 }
655 0 my $from_addr = $cfg->SMTP_FROM;
656 0 ▶ $smtp->datasend("From: $from_addr\n")
Branch: 1 branch
BranchTrueFalseTotal
$smtp->datasend("From: $from_addr\n") or return "Unable to send email data." 0 0 0
657 or return "Unable to send email data.";
658 0 ▶ $smtp->datasend("Subject: $subject\n")
Branch: 1 branch
BranchTrueFalseTotal
$smtp->datasend("Subject: $subject\n") or return "Unable to send email data." 0 0 0
659 or return "Unable to send email data.";
660 0 ▶ $smtp->datasend("To: $recipient\n")
Branch: 1 branch
BranchTrueFalseTotal
$smtp->datasend("To: $recipient\n") or return "Unable to send email data." 0 0 0
661 or return "Unable to send email data.";
662 0 ▶ if($cfg->ADM_ADDR =~ /\b$recipient\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->ADM_ADDR =~ /\b$recipient\b/) 0 0 0
663 # set reply-to header when sending mail to admin
664 0 ▶ $smtp->datasend("Reply-To: $recipient\n")
Branch: 1 branch
BranchTrueFalseTotal
$smtp->datasend("Reply-To: $recipient\n") or return "Unable to send email data." 0 0 0
665 or return "Unable to send email data.";
666 }
667 0 ▶ $smtp->datasend("MIME-Version: 1.0\n")
Branch: 1 branch
BranchTrueFalseTotal
$smtp->datasend("MIME-Version: 1.0\n") or return "Unable to send email data." 0 0 0
668 or return "Unable to send email data.";
669 0 ▶ $smtp->datasend("Content-Type: text/plain\n")
Branch: 1 branch
BranchTrueFalseTotal
$smtp->datasend("Content-Type: text/plain\n") or return "Unable to send email data." 0 0 0
670 or return "Unable to send email data.";
671 0 ▶ $smtp->datasend("Content-Transfer-Encoding: 7bit\n")
Branch: 1 branch
BranchTrueFalseTotal
$smtp->datasend("Content-Transfer-Encoding: 7bit\n") or return "Unable to send email data." 0 0 0
672 or return "Unable to send email data.";
673 0 ▶ $smtp->datasend("\n")
Branch: 1 branch
BranchTrueFalseTotal
$smtp->datasend("\n") or return "Unable to send email data." 0 0 0
674 or return "Unable to send email data.";
675 0 ▶ $smtp->datasend(@_)
Branch: 1 branch
BranchTrueFalseTotal
$smtp->datasend(@_) or return "Unable to send email data." 0 0 0
676 or return "Unable to send email data.";
677 0 ▶ $smtp->dataend()
Branch: 1 branch
BranchTrueFalseTotal
$smtp->dataend or return "Unable to end sending of email data." 0 0 0
678 or return "Unable to end sending of email data.";
679 0 ▶ $smtp->quit()
Branch: 1 branch
BranchTrueFalseTotal
$smtp->quit or return "Unable to close the SMTP connection." 0 0 0
680 or return "Unable to close the SMTP connection.";
681 0 return ''; # successful
682 }
683  
684 # ----------------------------------------------------------------------
685 # Send AMQP message. AMQP analog for send_email(). Takes five or more
686 # arguments: the AMQP address (queue name), subject line, node_info
687 # (hash ref), custom properties (hash ref), and body lines to be
688 # emailed. Returns empty string if successful or error message if
689 # error encountered.
690 0 ▶sub send_amqp_message {
Subroutine send_amqp_message: not called CC=42 SCAR=75.0
Pod undocumented
691 0 my $amqp_addr = shift;
692 0 my $subject = shift;
693 0 my $node = shift;
694 0 my $custom_properties = shift;
695 # @_ now contains message body
696  
697 0 ▶ if(not defined $amqp_addr) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $amqp_addr) 0 0 0
698 0 return 'send_amqp_message(): Missing amqp_addr (required).';
699 }
700  
701 0 ▶ if(not defined $subject) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $subject) 0 0 0
702 0 return 'send_amqp_message(): Missing subject (required).';
703 }
704  
705 0 ▶ if(not defined $node) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $node) { } 0 0 0
elsif (not 'HASH' eq ref $node) { } 0 0 0
706 0 return 'send_amqp_message(): Missing node details (required).';
707 }
708 elsif(not 'HASH' eq ref $node) {
709 0 ▶ return 'send_amqp_message(): unexpected node details; expected HASH ref, found ' .
Branch: 1 branch
BranchTrueFalseTotal
ref $custom_properties ? : 0 0 0
710 (ref $custom_properties ? ref $custom_properties . ' ref' : '(non reference)');
711 }
712  
713 0 ▶ if(defined $custom_properties and not 'HASH' eq ref $custom_properties) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $custom_properties and not "HASH" eq ref $custom_properties) 0 0 0
Condition0%0/3
defined $custom_properties and not "HASH" eq ref $custom_properties
!ll&&!rl&&r
000
MC/DC0%0/2
defined $custom_properties and not "HASH" eq ref $custom_properties
defined $custom_properties not "HASH" eq ref $custom_properties
Truth table0%0/3
A and not(B)
A= defined $custom_propertiesB= not "HASH" eq ref $custom_properties
ABresult
0X0
100
111
714 0 ▶ return 'send_amqp_message(): unexpected custom_properties value; expected undef or HASH ref, found ' .
Branch: 1 branch
BranchTrueFalseTotal
ref $custom_properties ? : 0 0 0
715 (ref $custom_properties ? ref $custom_properties . ' ref' : '(non reference)');
716 }
717  
718 0 ▶ if ((exists $node->{node_disabled}) and is_yes($node->{node_disabled}) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{"node_disabled"} and is_yes($node->{"node_disabled"})) 0 0 0
Condition0%0/3
exists $node->{"node_disabled"} and is_yes($node->{"node_disabled"})
!ll&&!rl&&r
000
MC/DC0%0/2
exists $node->{"node_disabled"} and is_yes($node->{"node_disabled"})
exists $node->{"node_disabled"} is_yes($node->{"node_disabled"})
Truth table0%0/3
A and B
A= exists $node->{"node_disabled"}B= is_yes($node->{"node_disabled"})
ABresult
0X0
100
111
719 {
720 return('send_amqp_message(): node_disabled is set for node ' .
721 0 $node->{node} . '. Message not sent.');
722 0 next;
723 }
724  
725 # default send_opts
726 my $send_opts = {
727 'amqp_broker_url' => $ECS_CFG->AMQP_BROKER_URL,
728 'amqp_cmd_send' => $ECS_CFG->AMQP_CMD_SEND,
729 # 'amqp_content_type' => 'text/plain',
730 'amqp_debug_level' => $ECS_CFG->AMQP_DEBUG_LEVEL,
731 # 'amqp_encoding' => 'utf-8',
732 'amqp_password' => (exists $ECS_CFG->{AMQP_PASSWORD} ? $ECS_CFG->AMQP_PASSWORD : ''),
733 'amqp_sslcert' => (exists $ECS_CFG->{AMQP_SSLCERT} ? $ECS_CFG->AMQP_SSLCERT : ''),
734 'amqp_sslkey' => (exists $ECS_CFG->{AMQP_SSLKEY} ? $ECS_CFG->AMQP_SSLKEY : ''),
735 'amqp_sslpass' => (exists $ECS_CFG->{AMQP_SSLPASS} ? $ECS_CFG->AMQP_SSLPASS : ''),
736 'amqp_truststore' => (exists $ECS_CFG->{AMQP_TRUSTSTORE} ? $ECS_CFG->AMQP_TRUSTSTORE : ''),
737 'amqp_username' => (exists $ECS_CFG->{AMQP_USERNAME} ? $ECS_CFG->AMQP_USERNAME : ''),
738 0 ▶ 'amqp_vhost' => (exists $ECS_CFG->{AMQP_VHOST} ? $ECS_CFG->AMQP_VHOST : '')
Branch: 7 branches
BranchTrueFalseTotal
exists $EMDIS::ECS::ECS_CFG->{'AMQP_PASSWORD'} ? : 0 0 0
exists $EMDIS::ECS::ECS_CFG->{'AMQP_SSLCERT'} ? : 0 0 0
exists $EMDIS::ECS::ECS_CFG->{'AMQP_SSLKEY'} ? : 0 0 0
exists $EMDIS::ECS::ECS_CFG->{'AMQP_SSLPASS'} ? : 0 0 0
exists $EMDIS::ECS::ECS_CFG->{'AMQP_TRUSTSTORE'} ? : 0 0 0
exists $EMDIS::ECS::ECS_CFG->{'AMQP_USERNAME'} ? : 0 0 0
exists $EMDIS::ECS::ECS_CFG->{'AMQP_VHOST'} ? : 0 0 0
739 };
740  
741 # override default send_opts with node-specific opts (where indicated)
742 0 foreach my $opt (keys %$send_opts) {
743 $send_opts->{$opt} = $node->{$opt}
744 0 ▶ if exists $node->{$opt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $node->{$opt} 0 0 0
745 }
746  
747 # default send_props
748 0 my $mail_mrk = $ECS_CFG->MAIL_MRK;
749 0 my $hub_snd = '';
750 0 my $seq_num = '';
751 0 ▶ if($subject =~ /$mail_mrk:(\S+?):(\d+):(\d+)\/(\d+)\s*$/io) {
Branch: 4 branches
BranchTrueFalseTotal
if ($subject =~ m[$mail_mrk:(\S+?):(\d+):(\d+)/(\d+)\s*$]io) { } 0 0 0
elsif ($subject =~ /$mail_mrk:(\S+?):(\d+)\s*$/io) { } 0 0 0
elsif ($subject =~ /$mail_mrk:(\S+?):(\d+):DOC\s*$/io) { } 0 0 0
elsif ($subject =~ /$mail_mrk:(\S+)\s*$/io) { } 0 0 0
752 0 $hub_snd = $1;
753 0 $seq_num = "$2:$3/$4";
754 }
755 elsif($subject =~ /$mail_mrk:(\S+?):(\d+)\s*$/io) {
756 0 $hub_snd = $1;
757 0 $seq_num = $2;
758 }
759 elsif($subject =~ /$mail_mrk:(\S+?):(\d+):DOC\s*$/io) {
760 0 $hub_snd = $1;
761 0 $seq_num = $2;
762 }
763 elsif($subject =~ /$mail_mrk:(\S+)\s*$/io) {
764 0 $hub_snd = $1;
765 }
766 # sanity check
767 0 ▶ if($ECS_CFG->THIS_NODE ne $hub_snd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($EMDIS::ECS::ECS_CFG->THIS_NODE ne $hub_snd) 0 0 0
768 0 return "send_amqp_message(): hub_snd ($hub_snd) ne THIS_NODE (" . $ECS_CFG->THIS_NODE . ")";
769 }
770 my $send_props = {
771 'x-emdis-hub-snd' => $ECS_CFG->THIS_NODE,
772 0 ▶ 'x-emdis-hub-rcv' => ($node->{node} ? $node->{node} : ''),
Branch: 2 branches
BranchTrueFalseTotal
$node->{'node'} ? : 0 0 0
$seq_num ? : 0 0 0
773 'x-emdis-sequential-number' => ($seq_num ? $seq_num : '')
774 };
775  
776 # add custom properties to send_props (where indicated)
777 0 ▶ if(defined $custom_properties) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $custom_properties) 0 0 0
778 0 foreach my $prop (keys %$custom_properties) {
779 0 $send_props->{$prop} = $custom_properties->{$prop};
780 }
781 }
782  
783 # construct AMQP send command
784 my $cmd = sprintf('%s --inputfile - --debug %s --address %s --broker %s ' .
785 '--subject %s',
786 $send_opts->{amqp_cmd_send},
787 $send_opts->{amqp_debug_level},
788 $amqp_addr,
789 $send_opts->{amqp_broker_url},
790 0 $subject);
791 $cmd .= sprintf(' --type %s', $send_opts->{amqp_content_type})
792 0 ▶ if $send_opts->{amqp_content_type};
Branch: 1 branch
BranchTrueFalseTotal
if $send_opts->{"amqp_content_type"} 0 0 0
793 $cmd .= sprintf(' --encoding %s', $send_opts->{amqp_encoding})
794 0 ▶ if $send_opts->{amqp_encoding};
Branch: 1 branch
BranchTrueFalseTotal
if $send_opts->{"amqp_encoding"} 0 0 0
795 $cmd .= sprintf(' --vhost %s', $send_opts->{amqp_vhost})
796 0 ▶ if $send_opts->{amqp_vhost};
Branch: 1 branch
BranchTrueFalseTotal
if $send_opts->{"amqp_vhost"} 0 0 0
797 $cmd .= sprintf(' --truststore %s', $send_opts->{amqp_truststore})
798 0 ▶ if $send_opts->{amqp_truststore};
Branch: 1 branch
BranchTrueFalseTotal
if $send_opts->{"amqp_truststore"} 0 0 0
799 $cmd .= sprintf(' --sslcert %s --sslkey %s',
800 $send_opts->{amqp_sslcert},
801 $send_opts->{amqp_sslkey})
802 0 ▶ if $send_opts->{amqp_sslcert} and $send_opts->{amqp_sslkey};
Branch: 1 branch
BranchTrueFalseTotal
if $send_opts->{"amqp_sslcert"} and $send_opts->{"amqp_sslkey"} 0 0 0
Condition0%0/3
$send_opts->{"amqp_sslcert"} and $send_opts->{"amqp_sslkey"}
!ll&&!rl&&r
000
MC/DC0%0/2
$send_opts->{"amqp_sslcert"} and $send_opts->{"amqp_sslkey"}
$send_opts->{"amqp_sslcert"} $send_opts->{"amqp_sslkey"}
Truth table0%0/3
A and B
A= $send_opts->{"amqp_sslcert"}B= $send_opts->{"amqp_sslkey"}
ABresult
0X0
100
111
803 $cmd .= sprintf(' --username %s', $send_opts->{amqp_username})
804 0 ▶ if $send_opts->{amqp_username};
Branch: 1 branch
BranchTrueFalseTotal
if $send_opts->{"amqp_username"} 0 0 0
805 0 foreach my $prop (keys %$send_props) {
806 $cmd .= sprintf(' --property %s=%s', $prop, $send_props->{$prop})
807 0 ▶ if $send_props->{$prop};
Branch: 1 branch
BranchTrueFalseTotal
if $send_props->{$prop} 0 0 0
808 }
809  
810 # set environment variables containing passwords:
811 # ECS_AMQP_PASSWORD and ECS_AMQP_SSLPASS
812 $ENV{ECS_AMQP_PASSWORD} = $send_opts->{amqp_password}
813 0 ▶ if $send_opts->{amqp_password};
Branch: 1 branch
BranchTrueFalseTotal
if $send_opts->{"amqp_password"} 0 0 0
814 $ENV{ECS_AMQP_SSLPASS} = $send_opts->{amqp_sslpass}
815 0 ▶ if $send_opts->{amqp_sslpass};
Branch: 1 branch
BranchTrueFalseTotal
if $send_opts->{"amqp_sslpass"} 0 0 0
816  
817 # execute command to send AMQP message
818 0 ▶ print "<DEBUG>: AMQP send command: $cmd\n"
Branch: 1 branch
BranchTrueFalseTotal
if $EMDIS::ECS::ECS_CFG->ECS_DEBUG > 0 0 0 0
819 if $ECS_CFG->ECS_DEBUG > 0;
820 0 my $err = timelimit_cmd($ECS_CFG->AMQP_SEND_TIMELIMIT, $cmd, join('', @_));
821 0 ▶ if($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
822 0 return "send_amqp_message(): unable to send AMQP message to $amqp_addr: $err";
823 }
824  
825 0 return '';
826 }
827  
828 # ----------------------------------------------------------------------
829 # Send encrypted email message.
830 # Returns empty string if successful or error message if error encountered.
831 sub send_encrypted_message
832 1 ▶{
Subroutine send_encrypted_message: called CC=25 SCAR=61.8
Pod undocumented
833 1 my $encr_typ = shift;
834 1 my $encr_recip = shift;
835 1 my $recipient = shift;
836 1 my $encr_out_keyid = shift;
837 1 my $encr_out_passphrase = shift;
838 1 my $node = shift;
839 1 my $subject = shift;
840 1 my $custom_headers = shift;
841 # @_ now contains message body
842  
843 1 ▶ return "EMDIS::ECS::send_encrypted_message(): ECS has not been configured."
Branch: 1 branch
BranchTrueFalseTotal
unless ecs_is_configured() 1 0 1
844 unless ecs_is_configured();
845 0 my $cfg = $ECS_CFG;
846  
847 # compose template for name of temp file
848 0 my $template = format_datetime(time, '%04d%02d%02d_%02d%02d%02d_XXXX');
849  
850 # write message body to temp file
851 0 my ($fh, $filename) = tempfile($template,
852 DIR => $cfg->ECS_TMP_DIR,
853 SUFFIX => '.tmp');
854 0 ▶ return "EMDIS::ECS::send_encrypted_message(): unable to create temporary file"
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
855 unless $fh;
856 0 print $fh @_;
857 0 close $fh;
858 0 chmod $FILEMODE, $filename;
859
860 # create file containing encrypted message
861 0 my $encr_filename = "$filename.pgp";
862 0 my $result = '';
863 0 for ($encr_typ) {
864 0 ▶ /PGP2/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/PGP2/i and do { $result = pgp2_encrypt($filename, $encr_filename, $encr_recip, $encr_out_keyid, $encr_out_passphrase); last } 0 0 0
865 0 $result = pgp2_encrypt($filename, $encr_filename, $encr_recip,
866 $encr_out_keyid, $encr_out_passphrase);
867 0 last;
868 };
869 0 ▶ /OpenPGP/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/OpenPGP/i and do { $result = openpgp_encrypt($filename, $encr_filename, $encr_recip, $encr_out_keyid, $encr_out_passphrase); last } 0 0 0
870 0 $result = openpgp_encrypt($filename, $encr_filename, $encr_recip,
871 $encr_out_keyid, $encr_out_passphrase);
872 0 last;
873 };
874 0 $result = "unrecognized encr_typ: $encr_typ";
875 }
876  
877 # delete first temp file
878 0 unlink $filename;
879  
880 # check for error
881 0 ▶ return "EMDIS::ECS::send_encrypted_message(): $result" if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 0 0 0
882  
883 # read contents of encrypted file
884 0 $fh = new IO::File;
885 0 ▶ return "EMDIS::ECS::send_encrypted_message(): unable to open file: " .
Branch: 1 branch
BranchTrueFalseTotal
unless $fh->open("< $encr_filename") 0 0 0
886 "$encr_filename"
887 unless $fh->open("< $encr_filename");
888 0 my @body = $fh->getlines();
889 0 $fh->close();
890  
891 # delete encrypted (temp) file
892 0 unlink $encr_filename;
893  
894 0 ▶ if(is_yes($cfg->ENABLE_AMQP)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_yes($cfg->ENABLE_AMQP)) 0 0 0
895 # send message via AMQP, if indicated by node config
896 0 my $amqp_addr = '';
897 0 ▶ if($subject =~ /^[^:]+:[^:]+$/io) {
Branch: 4 branches
BranchTrueFalseTotal
if ($subject =~ /^[^:]+:[^:]+$/io) { } 0 0 0
elsif ($subject =~ /^[^:]+:[^:]+:[0123456789]+:DOC/io) { } 0 0 0
elsif ($subject =~ /^[^:]+:[^:]+:[0123456789]+/io) { } 0 0 0
elsif (is_yes($node->{'amqp_only'})) { } 0 0 0
898 return "EMDIS::ECS::send_encrypted_message(): unable to send " .
899 "AMQP META message to node " . $node->{node} . ": amqp_only " .
900 "selected, but amqp_addr_meta not configured."
901 0 ▶ if not $node->{amqp_addr_meta} and is_yes($node->{amqp_only});
Branch: 1 branch
BranchTrueFalseTotal
if not $node->{"amqp_addr_meta"} and is_yes($node->{"amqp_only"}) 0 0 0
Condition0%0/3
not $node->{"amqp_addr_meta"} and is_yes($node->{"amqp_only"})
!ll&&!rl&&r
000
MC/DC0%0/2
not $node->{"amqp_addr_meta"} and is_yes($node->{"amqp_only"})
not $node->{"amqp_addr_meta"} is_yes($node->{"amqp_only"})
Truth table0%0/3
not(A) and B
A= not $node->{"amqp_addr_meta"}B= is_yes($node->{"amqp_only"})
ABresult
0X0
100
111
902 0 $amqp_addr = $node->{amqp_addr_meta};
903 }
904 elsif($subject =~ /^[^:]+:[^:]+:[0123456789]+:DOC/io) {
905 return "EMDIS::ECS::send_encrypted_message(): unable to send " .
906 "AMQP document to node " . $node->{node} . ": amqp_only " .
907 "selected, but amqp_addr_doc not configured."
908 0 ▶ if not $node->{amqp_addr_doc} and is_yes($node->{amqp_only});
Branch: 1 branch
BranchTrueFalseTotal
if not $node->{"amqp_addr_doc"} and is_yes($node->{"amqp_only"}) 0 0 0
Condition0%0/3
not $node->{"amqp_addr_doc"} and is_yes($node->{"amqp_only"})
!ll&&!rl&&r
000
MC/DC0%0/2
not $node->{"amqp_addr_doc"} and is_yes($node->{"amqp_only"})
not $node->{"amqp_addr_doc"} is_yes($node->{"amqp_only"})
Truth table0%0/3
not(A) and B
A= not $node->{"amqp_addr_doc"}B= is_yes($node->{"amqp_only"})
ABresult
0X0
100
111
909 0 $amqp_addr = $node->{amqp_addr_doc};
910 }
911 elsif($subject =~ /^[^:]+:[^:]+:[0123456789]+/io) {
912 return "EMDIS::ECS::send_encrypted_message(): unable to send " .
913 "AMQP regular message to node " . $node->{node} . ": amqp_only " .
914 "selected, but amqp_addr_msg not configured."
915 0 ▶ if not $node->{amqp_addr_msg} and is_yes($node->{amqp_only});
Branch: 1 branch
BranchTrueFalseTotal
if not $node->{"amqp_addr_msg"} and is_yes($node->{"amqp_only"}) 0 0 0
Condition0%0/3
not $node->{"amqp_addr_msg"} and is_yes($node->{"amqp_only"})
!ll&&!rl&&r
000
MC/DC0%0/2
not $node->{"amqp_addr_msg"} and is_yes($node->{"amqp_only"})
not $node->{"amqp_addr_msg"} is_yes($node->{"amqp_only"})
Truth table0%0/3
not(A) and B
A= not $node->{"amqp_addr_msg"}B= is_yes($node->{"amqp_only"})
ABresult
0X0
100
111
916 0 $amqp_addr = $node->{amqp_addr_msg};
917 }
918 elsif(is_yes($node->{amqp_only})) {
919 return "EMDIS::ECS::send_encrypted_message(): unable to send " .
920 0 "via AMQP to node " . $node->{node} . ": amqp_only selected, " .
921 "but unable to determine amqp_addr from Subject: $subject"
922 }
923 0 ▶ if($amqp_addr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amqp_addr) 0 0 0
924 0 return send_amqp_message(
925 $amqp_addr,
926 $subject,
927 $node,
928 $custom_headers,
929 @body);
930 }
931 }
932  
933 0 ▶ if(is_yes($node->{amqp_only})) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_yes($node->{"amqp_only"})) 0 0 0
934 return "EMDIS::ECS::send_encrypted_message(): unable to send " .
935 0 "via email to node " . $node->{node} . ": amqp_only selected"
936 }
937  
938 0 ▶ if($node->{amqp_addr_meta} or $node->{amqp_addr_msg} or $node->{amqp_addr_doc}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"amqp_addr_meta"} or $node->{"amqp_addr_msg"} or $node->{"amqp_addr_doc"}) 0 0 0
Condition0%0/6
$node->{"amqp_addr_meta"} or $node->{"amqp_addr_msg"} or $node->{"amqp_addr_doc"}
l!l&&r!l&&!r
000
$node->{"amqp_addr_meta"} or $node->{"amqp_addr_msg"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$node->{"amqp_addr_meta"} or $node->{"amqp_addr_msg"} or $node->{"amqp_addr_doc"}
$node->{"amqp_addr_meta"} $node->{"amqp_addr_msg"} $node->{"amqp_addr_doc"}
Truth table0%0/4
A or B or C
A= $node->{"amqp_addr_meta"}B= $node->{"amqp_addr_msg"}C= $node->{"amqp_addr_doc"}
ABCresult
1XX1
01X1
0011
0000
939 # print debug message if AMQP is only partially configured for recipient node
940 print "<DEBUG> EMDIS::ECS::send_encrypted_message(): sending via " .
941 0 ▶ "email (not AMQP) to node " . $node->{node} . ": $subject\n"
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->ECS_DEBUG > 0 0 0 0
942 if $cfg->ECS_DEBUG > 0;
943 }
944  
945 # send message via email
946 0 return send_email($recipient, $subject, $custom_headers, @body);
947 }
948  
949 # ----------------------------------------------------------------------
950 # Format a datetime value
951 sub format_datetime
952 2 ▶{
Subroutine format_datetime: called CC=2 SCAR=6.9
Pod undocumented
953 2 my $datetime = shift;
954 2 my $format = shift;
955 2 ▶ $format = '%04d-%02d-%02d %02d:%02d:%02d'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 1 1 2
956 unless defined $format;
957 2 my ($seconds, $minutes, $hours, $mday, $month, $year, $wday, $yday,
958 $isdst) = localtime($datetime);
959 2 return sprintf($format, $year + 1900, $month + 1, $mday,
960 $hours, $minutes, $seconds);
961 }
962  
963 # ----------------------------------------------------------------------
964 # Format filename for document.
965 sub format_doc_filename
966 0 ▶{
Subroutine format_doc_filename: not called CC=2 SCAR=17.9
Pod undocumented
967 0 ▶ return "EMDIS::ECS::format_doc_filename(): ECS has not been configured."
Branch: 1 branch
BranchTrueFalseTotal
unless ecs_is_configured() 0 0 0
968 unless ecs_is_configured();
969 0 my $cfg = $ECS_CFG;
970 0 my $node_id = shift;
971 0 my $seq_num = shift;
972 0 my $template = sprintf("%s_%s_d%010d",
973 $cfg->THIS_NODE, $node_id, $seq_num);
974 0 my $dirname = $cfg->ECS_MBX_OUT_DIR . "_$node_id";
975 0 return catfile($dirname, "$template.doc");
976 }
977  
978 # ----------------------------------------------------------------------
979 # Format filename for regular message.
980 sub format_msg_filename
981 1 ▶{
Subroutine format_msg_filename: called CC=2 SCAR=13.6
Pod undocumented
982 1 ▶ return "EMDIS::ECS::format_msg_filename(): ECS has not been configured."
Branch: 1 branch
BranchTrueFalseTotal
unless ecs_is_configured() 1 0 1
983 unless ecs_is_configured();
984 0 my $cfg = $ECS_CFG;
985 0 my $node_id = shift;
986 0 my $seq_num = shift;
987 0 my $template = sprintf("%s_%s_%010d",
988 $cfg->THIS_NODE, $node_id, $seq_num);
989 0 my $dirname = $cfg->ECS_MBX_OUT_DIR . "_$node_id";
990 0 return catfile($dirname, "$template.msg");
991 }
992  
993 # ----------------------------------------------------------------------
994 # Use OpenPGP (GnuPG) to decrypt a file.
995 # Returns empty string if successful or error message if error encountered.
996 sub openpgp_decrypt
997 1 ▶{
Subroutine openpgp_decrypt: called CC=11 SCAR=44.5
Pod undocumented
998 1 my $input_filename = shift;
999 1 my $output_filename = shift;
1000 1 my $required_signature = shift;
1001 1 my $encr_out_passphrase = shift;
1002  
1003 # initialize
1004 1 ▶ return "EMDIS::ECS::openpgp_decrypt(): ECS has not been configured."
Branch: 1 branch
BranchTrueFalseTotal
unless ecs_is_configured() 1 0 1
1005 unless ecs_is_configured();
1006 0 my $cfg = $ECS_CFG;
1007  
1008 # compose command
1009 0 my $cmd = $cfg->OPENPGP_CMD_DECRYPT;
1010 0 $cmd =~ s/__INPUT__/$input_filename/g;
1011 0 $cmd =~ s/__OUTPUT__/$output_filename/g;
1012 0 ▶ print "<DEBUG> openpgp_decrypt() command: $cmd\n"
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->ECS_DEBUG > 0 0 0 0
1013 if $cfg->ECS_DEBUG > 0;
1014  
1015 # set GNUPGHOME environment variable
1016 0 $ENV{GNUPGHOME} = $cfg->GPG_HOMEDIR;
1017  
1018 # attempt to execute command
1019 0 ▶ my $result = timelimit_cmd($cfg->T_MSG_PROC, $cmd,
Branch: 2 branches
BranchTrueFalseTotal
defined $encr_out_passphrase && 0 < length $encr_out_passphrase ? : 0 0 0
0 < length $cfg->GPG_PASSPHRASE ? : 0 0 0
Condition0%0/6
defined $encr_out_passphrase and 0 < length $encr_out_passphrase
!ll&&!rl&&r
000
defined $cfg->GPG_PASSPHRASE and 0 < length $cfg->GPG_PASSPHRASE ? $cfg->GPG_PASSPHRASE : undef
!ll&&!rl&&r
000
MC/DC0%0/2
defined $encr_out_passphrase and 0 < length $encr_out_passphrase
defined $encr_out_passphrase 0 < length $encr_out_passphrase
MC/DC0%0/2
defined $cfg->GPG_PASSPHRASE and 0 < length $cfg->GPG_PASSPHRASE ? $cfg->GPG_PASSPHRASE : undef
defined $cfg->GPG_PASSPHRASE 0 < length $cfg->GPG_PASSPHRASE ? $cfg->GPG_PASSPHRASE : undef
Truth table0%0/3
A and B
A= defined $encr_out_passphraseB= 0 < length $encr_out_passphrase
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $cfg->GPG_PASSPHRASEB= 0 < length $cfg->GPG_PASSPHRASE ? $cfg->GPG_PASSPHRASE : undef
ABresult
0X0
100
111
1020 (defined $encr_out_passphrase and 0 < length $encr_out_passphrase) ?
1021 $encr_out_passphrase :
1022 (defined $cfg->GPG_PASSPHRASE and 0 < length $cfg->GPG_PASSPHRASE ?
1023 $cfg->GPG_PASSPHRASE : undef));
1024 0 ▶ $result = "EMDIS::ECS::openpgp_decrypt(): $result" if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 0 0 0
1025  
1026 # check signature, if indicated
1027 0 ▶ if(defined($required_signature) and not $result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $required_signature and not $result) 0 0 0
Condition0%0/3
defined $required_signature and not $result
!ll&&!rl&&r
000
MC/DC0%0/2
defined $required_signature and not $result
defined $required_signature not $result
Truth table0%0/3
A and not(B)
A= defined $required_signatureB= not $result
ABresult
0X0
100
111
1028 0 ▶ if($cmd_output !~ /Good signature from[^\n]+$required_signature/is) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($EMDIS::ECS::cmd_output =~ /Good signature from[^\n]+$required_signature/is) 0 0 0
1029 0 $result = "EMDIS::ECS::openpgp_decrypt(): required signature not " .
1030 "present: $required_signature";
1031 }
1032 }
1033  
1034 0 return $result;
1035 }
1036  
1037 # ----------------------------------------------------------------------
1038 # Use OpenPGP (GnuPG) to encrypt a file.
1039 # Returns empty string if successful or error message if error encountered.
1040 sub openpgp_encrypt
1041 1 ▶{
Subroutine openpgp_encrypt: called CC=10 SCAR=41.8
Pod undocumented
1042 1 my $input_filename = shift;
1043 1 my $output_filename = shift;
1044 1 my $recipient = shift;
1045 1 my $encr_out_keyid = shift;
1046 1 my $encr_out_passphrase = shift;
1047  
1048 # initialize
1049 1 ▶ return "EMDIS::ECS::openpgp_encrypt(): ECS has not been configured."
Branch: 1 branch
BranchTrueFalseTotal
unless ecs_is_configured() 1 0 1
1050 unless ecs_is_configured();
1051 0 my $cfg = $ECS_CFG;
1052  
1053 # compose command
1054 0 ▶ my $keyid = (defined $encr_out_keyid and 0 < length $encr_out_keyid) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $encr_out_keyid && 0 < length $encr_out_keyid ? : 0 0 0
Condition0%0/3
defined $encr_out_keyid and 0 < length $encr_out_keyid
!ll&&!rl&&r
000
MC/DC0%0/2
defined $encr_out_keyid and 0 < length $encr_out_keyid
defined $encr_out_keyid 0 < length $encr_out_keyid
Truth table0%0/3
A and B
A= defined $encr_out_keyidB= 0 < length $encr_out_keyid
ABresult
0X0
100
111
1055 $encr_out_keyid : $cfg->GPG_KEYID;
1056 0 my $cmd = $cfg->OPENPGP_CMD_ENCRYPT;
1057 0 $cmd =~ s/__INPUT__/$input_filename/g;
1058 0 $cmd =~ s/__OUTPUT__/$output_filename/g;
1059 0 $cmd =~ s/__RECIPIENT__/$recipient/g;
1060 0 $cmd =~ s/__SELF__/$keyid/g;
1061 0 ▶ print "<DEBUG> openpgp_encrypt() command: $cmd\n"
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->ECS_DEBUG > 0 0 0 0
1062 if $cfg->ECS_DEBUG > 0;
1063  
1064 # set GNUPGHOME environment variable
1065 0 $ENV{GNUPGHOME} = $cfg->GPG_HOMEDIR;
1066  
1067 # attempt to execute command
1068 0 ▶ my $result = timelimit_cmd($cfg->T_MSG_PROC, $cmd,
Branch: 2 branches
BranchTrueFalseTotal
defined $encr_out_passphrase && 0 < length $encr_out_passphrase ? : 0 0 0
0 < length $cfg->GPG_PASSPHRASE ? : 0 0 0
Condition0%0/6
defined $encr_out_passphrase and 0 < length $encr_out_passphrase
!ll&&!rl&&r
000
defined $cfg->GPG_PASSPHRASE and 0 < length $cfg->GPG_PASSPHRASE ? $cfg->GPG_PASSPHRASE : undef
!ll&&!rl&&r
000
MC/DC0%0/2
defined $encr_out_passphrase and 0 < length $encr_out_passphrase
defined $encr_out_passphrase 0 < length $encr_out_passphrase
MC/DC0%0/2
defined $cfg->GPG_PASSPHRASE and 0 < length $cfg->GPG_PASSPHRASE ? $cfg->GPG_PASSPHRASE : undef
defined $cfg->GPG_PASSPHRASE 0 < length $cfg->GPG_PASSPHRASE ? $cfg->GPG_PASSPHRASE : undef
Truth table0%0/3
A and B
A= defined $encr_out_passphraseB= 0 < length $encr_out_passphrase
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $cfg->GPG_PASSPHRASEB= 0 < length $cfg->GPG_PASSPHRASE ? $cfg->GPG_PASSPHRASE : undef
ABresult
0X0
100
111
1069 (defined $encr_out_passphrase and 0 < length $encr_out_passphrase) ?
1070 $encr_out_passphrase :
1071 (defined $cfg->GPG_PASSPHRASE and 0 < length $cfg->GPG_PASSPHRASE ?
1072 $cfg->GPG_PASSPHRASE : undef));
1073 0 ▶ $result = "EMDIS::ECS::openpgp_encrypt(): $result" if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 0 0 0
1074 0 return $result;
1075 }
1076  
1077 # ----------------------------------------------------------------------
1078 # Use PGP2 (PGP) to decrypt a file.
1079 # Returns empty string if successful or error message if error encountered.
1080 sub pgp2_decrypt
1081 1 ▶{
Subroutine pgp2_decrypt: called CC=10 SCAR=42.7
Pod undocumented
1082 1 my $input_filename = shift;
1083 1 my $output_filename = shift;
1084 1 my $required_signature = shift;
1085 1 my $encr_out_passphrase = shift;
1086  
1087 # initialize
1088 1 ▶ return "EMDIS::ECS::pgp2_decrypt(): ECS has not been configured."
Branch: 1 branch
BranchTrueFalseTotal
unless ecs_is_configured() 1 0 1
1089 unless ecs_is_configured();
1090 0 my $cfg = $ECS_CFG;
1091  
1092 # compose command
1093 0 my $cmd = $cfg->PGP2_CMD_DECRYPT;
1094 0 $cmd =~ s/__INPUT__/$input_filename/g;
1095 0 $cmd =~ s/__OUTPUT__/$output_filename/g;
1096 0 ▶ print "<DEBUG> pgp2_decrypt() command: $cmd\n"
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->ECS_DEBUG > 0 0 0 0
1097 if $cfg->ECS_DEBUG > 0;
1098  
1099 # set PGPPATH and PGPPASS environment variables
1100 0 $ENV{PGPPATH} = $cfg->PGP_HOMEDIR;
1101 0 ▶ my $passphrase = (defined $encr_out_passphrase and 0 < length $encr_out_passphrase) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $encr_out_passphrase && 0 < length $encr_out_passphrase ? : 0 0 0
Condition0%0/3
defined $encr_out_passphrase and 0 < length $encr_out_passphrase
!ll&&!rl&&r
000
MC/DC0%0/2
defined $encr_out_passphrase and 0 < length $encr_out_passphrase
defined $encr_out_passphrase 0 < length $encr_out_passphrase
Truth table0%0/3
A and B
A= defined $encr_out_passphraseB= 0 < length $encr_out_passphrase
ABresult
0X0
100
111
1102 $encr_out_passphrase : $cfg->PGP_PASSPHRASE;
1103 0 $ENV{PGPPASS} = $passphrase;
1104  
1105 # attempt to execute command - pipe passphrase to cmd, to support usage of gpg1 in place of pgp2
1106 0 my $result = timelimit_cmd($cfg->T_MSG_PROC, $cmd, $passphrase);
1107 0 ▶ $result = '' if($result =~ /^Status 0x0100/); # ignore exit value = 1
Branch: 1 branch
BranchTrueFalseTotal
if $result =~ /^Status 0x0100/ 0 0 0
1108 0 ▶ $result = "EMDIS::ECS::pgp2_decrypt(): $result" if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 0 0 0
1109  
1110 # check signature, if indicated
1111 0 ▶ if(defined($required_signature) and not $result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $required_signature and not $result) 0 0 0
Condition0%0/3
defined $required_signature and not $result
!ll&&!rl&&r
000
MC/DC0%0/2
defined $required_signature and not $result
defined $required_signature not $result
Truth table0%0/3
A and not(B)
A= defined $required_signatureB= not $result
ABresult
0X0
100
111
1112 0 ▶ if($cmd_output !~ /Good signature from[^\n]+$required_signature/is) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($EMDIS::ECS::cmd_output =~ /Good signature from[^\n]+$required_signature/is) 0 0 0
1113 0 $result = "EMDIS::ECS::pgp2_decrypt(): required signature not " .
1114 "present: $required_signature";
1115 }
1116 }
1117  
1118 0 return $result;
1119 }
1120  
1121 # ----------------------------------------------------------------------
1122 # Use PGP to encrypt a file.
1123 # Returns empty string if successful or error message if error encountered.
1124 sub pgp2_encrypt
1125 1 ▶{
Subroutine pgp2_encrypt: called CC=8 SCAR=37.2
Pod undocumented
1126 1 my $input_filename = shift;
1127 1 my $output_filename = shift;
1128 1 my $recipient = shift;
1129 1 my $encr_out_keyid = shift;
1130 1 my $encr_out_passphrase = shift;
1131  
1132 # initialize
1133 1 ▶ return "EMDIS::ECS::pgp2_encrypt(): ECS has not been configured."
Branch: 1 branch
BranchTrueFalseTotal
unless ecs_is_configured() 1 0 1
1134 unless ecs_is_configured();
1135 0 my $cfg = $ECS_CFG;
1136  
1137 # compose command
1138 0 ▶ my $keyid = (defined $encr_out_keyid and 0 < length $encr_out_keyid) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $encr_out_keyid && 0 < length $encr_out_keyid ? : 0 0 0
Condition0%0/3
defined $encr_out_keyid and 0 < length $encr_out_keyid
!ll&&!rl&&r
000
MC/DC0%0/2
defined $encr_out_keyid and 0 < length $encr_out_keyid
defined $encr_out_keyid 0 < length $encr_out_keyid
Truth table0%0/3
A and B
A= defined $encr_out_keyidB= 0 < length $encr_out_keyid
ABresult
0X0
100
111
1139 $encr_out_keyid : $cfg->PGP_KEYID;
1140 0 my $cmd = $cfg->PGP2_CMD_ENCRYPT;
1141 0 $cmd =~ s/__INPUT__/$input_filename/g;
1142 0 $cmd =~ s/__OUTPUT__/$output_filename/g;
1143 0 $cmd =~ s/__RECIPIENT__/$recipient/g;
1144 0 $cmd =~ s/__SELF__/$keyid/g;
1145 0 ▶ print "<DEBUG> pgp2_encrypt() command: $cmd\n"
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->ECS_DEBUG > 0 0 0 0
1146 if $cfg->ECS_DEBUG > 0;
1147  
1148 # set PGPPATH and PGPPASS environment variables
1149 0 $ENV{PGPPATH} = $cfg->PGP_HOMEDIR;
1150 0 ▶ my $passphrase = (defined $encr_out_passphrase and 0 < length $encr_out_passphrase) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $encr_out_passphrase && 0 < length $encr_out_passphrase ? : 0 0 0
Condition0%0/3
defined $encr_out_passphrase and 0 < length $encr_out_passphrase
!ll&&!rl&&r
000
MC/DC0%0/2
defined $encr_out_passphrase and 0 < length $encr_out_passphrase
defined $encr_out_passphrase 0 < length $encr_out_passphrase
Truth table0%0/3
A and B
A= defined $encr_out_passphraseB= 0 < length $encr_out_passphrase
ABresult
0X0
100
111
1151 $encr_out_passphrase : $cfg->PGP_PASSPHRASE;
1152 0 $ENV{PGPPASS} = $passphrase;
1153
1154 # attempt to execute command - pipe passphrase to cmd, to support usage of gpg1 in place of pgp2
1155 0 my $result = timelimit_cmd($cfg->T_MSG_PROC, $cmd, $passphrase);
1156 0 ▶ $result = "EMDIS::ECS::pgp2_encrypt(): $result" if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 0 0 0
1157 0 return $result;
1158 }
1159  
1160 # ----------------------------------------------------------------------
1161 # Check whether another copy of the program is already running.
1162 # If so, this one dies.
1163 sub check_pid
1164 0 ▶{
Subroutine check_pid: not called CC=4 SCAR=30.0
Pod undocumented
1165 0 ▶ die "EMDIS::ECS::check_pid(): ECS has not been configured."
Branch: 1 branch
BranchTrueFalseTotal
unless ecs_is_configured() 0 0 0
1166 unless ecs_is_configured();
1167  
1168 0 ▶ if(open PIDFILE, $pidfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (open PIDFILE, $EMDIS::ECS::pidfile) 0 0 0
1169 0 my $pid = <PIDFILE>;
1170 0 $pid =~ s/\s+//g;
1171 0 ▶ die "Error: $0 is already running (pid $pid).\n"
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $pid 0 0 0
1172 if kill(0, $pid);
1173 0 close PIDFILE;
1174 }
1175  
1176 0 save_pid();
1177 }
1178  
1179 # ----------------------------------------------------------------------
1180 # Update PID file.
1181 sub save_pid
1182 0 ▶{
Subroutine save_pid: not called CC=2 SCAR=17.9
Pod undocumented
1183 0 ▶ die "EMDIS::ECS::save_pid(): ECS has not been configured."
Branch: 1 branch
BranchTrueFalseTotal
unless ecs_is_configured() 0 0 0
1184 unless ecs_is_configured();
1185  
1186 0 open PIDFILE, ">$pidfile";
1187 0 print PIDFILE "$$\n";
1188 0 close PIDFILE;
1189 0 chmod $FILEMODE, $pidfile;
1190 0 $pid_saved = 1;
1191 }
1192  
1193 # ----------------------------------------------------------------------
1194 # Select the Win32 or Unix version of timelimit_cmd
1195 sub timelimit_cmd
1196 0 ▶{
Subroutine timelimit_cmd: not called CC=2 SCAR=17.9
Pod undocumented
1197 0 ▶ $^O =~ /MSWin32/ ? timelimit_cmd_win32(@_) : timelimit_cmd_unix(@_);
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /MSWin32/ ? : 0 0 0
1198 }
1199  
1200  
1201  
1202 # Returns empty string if successful or error message if error encountered.
1203 sub timelimit_cmd_win32
1204 0 ▶{
Subroutine timelimit_cmd_win32: not called CC=22 SCAR=62.3
Pod undocumented
1205 0 my $timelimit = shift;
1206 0 my $cmd = shift;
1207 0 my $input_data = shift;
1208 0 my $cfg = $ECS_CFG;
1209 0 my @msgs = ();
1210 0 my $result = "";
1211 0 my ($ProcessObj, $rc, $appname, $cmdline);
1212  
1213 # reset module-level variable containing command output
1214 0 $cmd_output = '';
1215  
1216 0 pipe(READ, WRITE);
1217 0 select(WRITE);
1218 0 $| = 1;
1219 0 select(STDOUT);
1220 0 ▶ open(OLDIN, "< &STDIN") || die "Can not save STDIN\n";
Branch: 1 branch
BranchTrueFalseTotal
open OLDIN, "< &STDIN" or die "Can not save STDIN\n" 0 0 0
1221 0 ▶ open(STDIN, "< &READ") || die "Can not redirect STDIN\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "< &READ" or die "Can not redirect STDIN\n" 0 0 0
1222  
1223 0 ▶ open(OLDOUT, ">&STDOUT") || die "Can not save STDOUT\n";
Branch: 1 branch
BranchTrueFalseTotal
open OLDOUT, ">&STDOUT" or die "Can not save STDOUT\n" 0 0 0
1224 0 ▶ open(STDOUT, ">$$.txt" ) || die( "Unable to redirect STDOUT ");
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">${$}.txt" or die "Unable to redirect STDOUT " 0 0 0
1225  
1226 0 ▶ open(OLDERR, ">&STDERR" ) || die "Can not redirect STDERR\n";
Branch: 1 branch
BranchTrueFalseTotal
open OLDERR, ">&STDERR" or die "Can not redirect STDERR\n" 0 0 0
1227 0 ▶ open(STDERR, ">&STDOUT" ) || die( "Unable to dup STDOUT to STDERR" );
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&STDOUT" or die "Unable to dup STDOUT to STDERR" 0 0 0
1228  
1229 0 select(STDERR);
1230 0 $| = 1;
1231 0 select(STDIN);
1232 0 $| = 1;
1233 0 select(STDOUT);
1234  
1235 0 ▶ if(! defined $input_data) { $input_data = ""; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input_data) 0 0 0
1236  
1237 # compute $appname and $cmdline
1238 0 $cmd =~ /\s*(\S+)\s*(.*)/;
1239 0 $appname = $1;
1240 0 $cmdline = "$1 $2";
1241 # if applicable, append .exe or .bat extension to $appname
1242 0 ▶ if(-x "$appname.exe")
Branch: 2 branches
BranchTrueFalseTotal
if (-x "$appname.exe") { } 0 0 0
elsif (-x "$appname.bat") { } 0 0 0
1243 {
1244 0 $appname = "$appname.exe";
1245 }
1246 elsif(-x "$appname.bat")
1247 {
1248 0 $appname = "$appname.bat";
1249 }
1250
1251 0 ▶ print "\n<DEBUG>: Running External Command" .
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->ECS_DEBUG > 0 0 0 0
1252 "\nappname=" . $appname .
1253 "\ncmdline=" . $cmdline .
1254 # "\nSTDIN=" . $input_data . # (don't print out PGP passphrase)
1255 "\nTimelimit=" . $timelimit . "\n"
1256 if $cfg->ECS_DEBUG > 0;
1257  
1258 0 $rc = Win32::Process::Create(
1259 $ProcessObj,
1260 $appname,
1261 $cmdline,
1262 1,
1263 Win32::Process::constant('NORMAL_PRIORITY_CLASS'),
1264 ".");
1265  
1266 0 ▶ if ($rc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) { } 0 0 0
1267 0 ▶ print "<DEBUG>: PID = " . $ProcessObj->GetProcessID() . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->ECS_DEBUG > 0 0 0 0
1268 if $cfg->ECS_DEBUG > 0;
1269 }
1270 else {
1271 0 my $winMsg = Win32::FormatMessage(Win32::GetLastError());
1272 0 ▶ if (defined $winMsg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $winMsg) { } 0 0 0
1273 0 $result = $winMsg;
1274 } else {
1275 0 ▶ print "<DEBUG>: Windows error\n"
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->ECS_DEBUG > 0 0 0 0
1276 if $cfg->ECS_DEBUG > 0;
1277 0 $result = "Windows error";
1278 }
1279 }
1280  
1281 0 ▶ if($rc)
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) 0 0 0
1282 {
1283 0 print WRITE "$input_data\n";
1284 0 close(WRITE);
1285  
1286 0 ▶ print "<DEBUG>: Waiting\n"
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->ECS_DEBUG > 0 0 0 0
1287 if $cfg->ECS_DEBUG > 0;
1288 0 $rc = $ProcessObj->Wait($timelimit * 1000);
1289  
1290 # Check for return code
1291 0 ▶ if ($rc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) { } 0 0 0
1292 0 my $ret;
1293 0 $ProcessObj->GetExitCode($ret);
1294 0 ▶ print "<DEBUG>: Process OK ($ret)\n\n"
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->ECS_DEBUG > 0 0 0 0
1295 if $cfg->ECS_DEBUG > 0;
1296 } else {
1297 0 Win32::Process::KillProcess($ProcessObj->GetProcessID(), 0);
1298 0 ▶ print "<DEBUG>: Process Timeout\n\n"
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->ECS_DEBUG > 0 0 0 0
1299 if $cfg->ECS_DEBUG > 0;
1300 0 $result = "Process Timeout";
1301 }
1302 }
1303  
1304 # Restore STDIN, STDOUT, STDERR
1305 0 open(STDIN, "<&OLDIN");
1306 0 open(STDOUT, ">&OLDOUT" );
1307 0 open(STDERR, ">&OLDERR" );
1308  
1309 0 if(0)
1310 {
1311 # just leave these hanging until next time around ...
1312 # (avoid potential deadlock waiting for child process to end)
1313 close(READ);
1314 close(OLDIN);
1315 close(OLDOUT);
1316 close(OLDERR);
1317 }
1318  
1319  
1320 0 ▶ if(open FILETEMP, "< $$.txt")
Branch: 1 branch
BranchTrueFalseTotal
if (open FILETEMP, "< ${$}.txt") 0 0 0
1321 {
1322 0 @msgs = <FILETEMP>;
1323 0 close FILETEMP;
1324 0 unlink "$$.txt";
1325 0 print "\n======== EXTERNAL BEGIN =============\n";
1326 0 print @msgs;
1327 0 print "========= EXTERNAL END ==============\n";
1328 }
1329  
1330 # set module-level variable containing command output
1331 0 ▶ if($#msgs >= 0) { $cmd_output = join('', @msgs); }
Branch: 1 branch
BranchTrueFalseTotal
if ($#msgs >= 0) { } 0 0 0
1332 0 else { $cmd_output = ''; }
1333  
1334 0 return $result;
1335 }
1336  
1337  
1338 # ----------------------------------------------------------------------
1339 # Unix version
1340 # Execute specified command, with time limit and optional input data.
1341 # Returns empty string if successful or error message if error encountered.
1342 sub timelimit_cmd_unix
1343 0 ▶{
Subroutine timelimit_cmd_unix: not called CC=16 SCAR=56.1
Pod undocumented
1344 0 my $timelimit = shift;
1345 0 my $cmd = shift;
1346 0 my $input_data = shift;
1347  
1348 # reset module-level variable containing command output
1349 0 $cmd_output = '';
1350  
1351 # initialize
1352 0 my ($reader, $writer) = (IO::Handle->new, IO::Handle->new);
1353 0 my ($pid, @msgs, $status);
1354 0 my $result = '';
1355  
1356 # set up "local" SIG_PIPE and SIG_ALRM handlers
1357 # (Note: not using "local $SIG{PIPE}" because it ignores die())
1358 0 my $broken_pipe = '';
1359 0 my $oldsigpipe = $SIG{PIPE};
1360 0 ▶ $SIG{PIPE} = sub { $broken_pipe = 1; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1361 0 my $oldsigalrm = $SIG{ALRM};
1362 0 ▶ $SIG{ALRM} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1363 0 die "timeout - $timelimit second processing time limit exceeded\n";
1364 0 };
1365  
1366 # use eval {}; to enforce time limit (see Perl Cookbook, 16.21)
1367 0 eval {
1368 0 alarm($timelimit); # set time limit
1369 0 $broken_pipe = '';
1370 0 $pid = open2($reader, $writer, $cmd);
1371 0 ▶ print $writer $input_data if defined $input_data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $input_data 0 0 0
1372 0 close $writer;
1373 0 @msgs = $reader->getlines();
1374 0 close $reader;
1375 0 waitpid $pid, 0;
1376 0 $status = $?;
1377 0 ▶ die "broken pipe\n" if $broken_pipe;
Branch: 1 branch
BranchTrueFalseTotal
if $broken_pipe 0 0 0
1378 0 alarm(0);
1379 };
1380 0 ▶ if($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif ($status) { } 0 0 0
1381 0 alarm(0);
1382 # detect runaway child from open2() fork/exec
1383 0 ▶ die "runaway child, probably caused by bad command\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $pid and $@ =~ /^open2/ 0 0 0
Condition0%0/3
not defined $pid and $@ =~ /^open2/
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $pid and $@ =~ /^open2/
not defined $pid $@ =~ /^open2/
Truth table0%0/3
not(A) and B
A= not defined $pidB= $@ =~ /^open2/
ABresult
0X0
100
111
1384 if (not defined $pid) and ($@ =~ /^open2/);
1385 # construct error message
1386 0 chomp $@;
1387 0 $result = "$@: $cmd\n";
1388 }
1389 elsif ($status) {
1390 0 my $exit_value = $status >> 8;
1391 0 my $signal_num = $status & 127;
1392 0 my $dumped_core = $status & 128;
1393 # construct error message
1394 0 ▶ $result = sprintf("Status 0x%04x (exit %d%s%s)",
Branch: 2 branches
BranchTrueFalseTotal
$signal_num ? : 0 0 0
$dumped_core ? : 0 0 0
1395 $status, $exit_value,
1396 ($signal_num ? ", signal $signal_num" : ''),
1397 ($dumped_core ? ', core dumped' : ''));
1398 }
1399 0 ▶ $writer->close if $writer->opened;
Branch: 1 branch
BranchTrueFalseTotal
if $writer->opened 0 0 0
1400 0 ▶ $reader->close if $reader->opened;
Branch: 1 branch
BranchTrueFalseTotal
if $reader->opened 0 0 0
1401 0 ▶ if(defined $oldsigpipe) { $SIG{PIPE} = $oldsigpipe; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oldsigpipe) { } 0 0 0
1402 0 else { delete $SIG{PIPE}; }
1403 0 ▶ if(defined $oldsigalrm) { $SIG{ALRM} = $oldsigalrm; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oldsigalrm) { } 0 0 0
1404 0 else { delete $SIG{ALRM}; }
1405 0 ▶ $result .= "\n----------\n" . join("", @msgs) if($result and $#msgs >= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $result and $#msgs >= 0 0 0 0
Condition0%0/3
$result and $#msgs >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$result and $#msgs >= 0
$result $#msgs >= 0
Truth table0%0/3
A and B
A= $resultB= $#msgs >= 0
ABresult
0X0
100
111
1406 # set module-level variable containing command output
1407 0 ▶ if($#msgs >= 0) { $cmd_output = join('', @msgs); }
Branch: 1 branch
BranchTrueFalseTotal
if ($#msgs >= 0) { } 0 0 0
1408 0 else { $cmd_output = ''; }
1409 0 return $result;
1410 }
1411  
1412 # ----------------------------------------------------------------------
1413 # Unlink PID file.
1414 sub remove_pidfile
1415 0 ▶{
Subroutine remove_pidfile: not called CC=2 SCAR=17.9
Pod undocumented
1416 0 ▶ unlink $pidfile if $pidfile;
Branch: 1 branch
BranchTrueFalseTotal
if $EMDIS::ECS::pidfile 0 0 0
1417 }
1418  
1419 # ----------------------------------------------------------------------
1420 # Return string value with leading and trailing whitespace trimmed off.
1421 11 ▶sub trim {
Subroutine trim: called CC=2 SCAR=7.0
Pod undocumented
1422 11 my $str = shift;
1423 11 ▶ return if not defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 11 11
1424 11 $str =~ s/^\s+//;
1425 11 $str =~ s/\s+$//;
1426 11 return $str;
1427 }
1428  
1429 # ----------------------------------------------------------------------
1430 # Return boolean indicating whether specified encr_typ is valid.
1431 sub valid_encr_typ
1432 4 ▶{
Subroutine valid_encr_typ: called CC=4 SCAR=14.3
Pod undocumented
1433 4 my $encr_typ = shift;
1434 4 for ($encr_typ) {
1435 4 ▶ /PGP2/i and return 1;
Branch: 1 branch
BranchTrueFalseTotal
/PGP2/i and return 1 2 2 4
1436 2 ▶ /OpenPGP/i and return 1;
Branch: 1 branch
BranchTrueFalseTotal
/OpenPGP/i and return 1 2 0 2
1437 }
1438 0 return '';
1439 }
1440  
1441 6 1;
1442  
1443 __DATA__
« lib/EMDIS/ECS/FileBackedMessage.pm ↑ Summary script/ecs_status.pl »