| 1 | package Net::ACME2::Challenge::http_01::Handler; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Net::ACME2::Challenge::http_01::Handler - http-01 challenge handler | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | This module handles the creation and removal of a domain control | ||||||||||||||||||||||||||||
| 12 | validation (DCV) file for http-01 challenges. Creation happens on | ||||||||||||||||||||||||||||
| 13 | instantiation; removal happens when the object is destroyed. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | See L<Net::ACME2::Challenge::http_01> for a usage example. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Errno (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | our $ASSUME_UNIX_PATHS; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | my @required = qw( key_authorization challenge document_root ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub new { | ||||||||||||||||||||||||||||
| 30 | 2 | ▶ | my ( $class, %opts ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | #sanity | ||||||||||||||||||||||||||||
| 33 | 2 | my @missing = grep { !defined $opts{$_} } @required; | |||||||||||||||||||||||||||
| 34 | 2 | ▶ | die "Missing: [@missing]" if @missing; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | -d $opts{'document_root'} or die "Document root “$opts{'document_root'}” doesn’t exist!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | my ($file_path, $dir); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 2 | ▶ | if ($ASSUME_UNIX_PATHS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | $file_path = $opts{'document_root'} . $opts{'challenge'}->path(); | |||||||||||||||||||||||||||
| 42 | 0 | $dir = substr( $file_path, rindex( $file_path, '/' ) ); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | else { | ||||||||||||||||||||||||||||
| 45 | 2 | require File::Spec; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | my @relpath = split m</>, $opts{'challenge'}->path(); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | $file_path = File::Spec->catdir( | ||||||||||||||||||||||||||||
| 50 | 2 | $opts{'document_root'}, | |||||||||||||||||||||||||||
| 51 | @relpath, | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | $dir = File::Spec->catdir( | ||||||||||||||||||||||||||||
| 55 | 2 | $opts{'document_root'}, | |||||||||||||||||||||||||||
| 56 | @relpath[ 0 .. ($#relpath - 1) ], | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 2 | _mkdir_if_not_exists($dir); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | local ( $!, $^E ); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | open my $wfh, '>', $file_path; | |||||||||||||||||||||||||||
| 65 | 2 | chmod 0644, $file_path; | |||||||||||||||||||||||||||
| 66 | 2 | syswrite $wfh, $opts{'key_authorization'}; | |||||||||||||||||||||||||||
| 67 | 2 | close $wfh; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 2 | my $self = { | |||||||||||||||||||||||||||
| 70 | _euid => $>, | ||||||||||||||||||||||||||||
| 71 | _path => $file_path, | ||||||||||||||||||||||||||||
| 72 | #_docroot_relative_path => $docroot_relative_path, | ||||||||||||||||||||||||||||
| 73 | #_content => $opts{'key_authorization'}, | ||||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2 | return bless $self, $class; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | #sub expected_content { | ||||||||||||||||||||||||||||
| 80 | # my ($self) = @_; | ||||||||||||||||||||||||||||
| 81 | # | ||||||||||||||||||||||||||||
| 82 | # return $self->{'_content'}; | ||||||||||||||||||||||||||||
| 83 | #} | ||||||||||||||||||||||||||||
| 84 | # | ||||||||||||||||||||||||||||
| 85 | #sub verification_path { | ||||||||||||||||||||||||||||
| 86 | # my ($self) = @_; | ||||||||||||||||||||||||||||
| 87 | # | ||||||||||||||||||||||||||||
| 88 | # return "/$self->{'_docroot_relative_path'}"; | ||||||||||||||||||||||||||||
| 89 | #} | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub DESTROY { | ||||||||||||||||||||||||||||
| 92 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2 | ▶ | if ( $> != $self->{'_euid'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | die "XXX attempt to delete “$self->{'_path'}” with EUID $>; created with EUID $self->{'_euid'}!"; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | _unlink_if_exists( $self->{'_path'} ); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 2 | return; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub _mkdir_if_not_exists { | ||||||||||||||||||||||||||||
| 104 | 2 | ▶ | my ($dir) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_mkdir_if_not_exists: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2 | ▶ | if (!-d $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 1 | require File::Path; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | #cf. eval_bug.readme | ||||||||||||||||||||||||||||
| 110 | 1 | my $eval_err = $@; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | local ( $!, $^E ); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | File::Path::make_path($dir); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 1 | $@ = $eval_err; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 2 | return; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub _unlink_if_exists { | ||||||||||||||||||||||||||||
| 123 | 2 | ▶ | my ($path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_unlink_if_exists: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | #cf. eval_bug.readme | ||||||||||||||||||||||||||||
| 126 | 2 | my $eval_err = $@; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 2 | local ( $!, $^E ); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 2 | eval { unlink $path }; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 2 | ▶ | die if $@ && $@->errno() != Errno::ENOENT(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$@ and $@->errno != 2
MC/DC0%0/2
$@ and $@->errno != 2
$@
$@->errno != 2
Truth table66%2/3
A and B
A= $@B= $@->errno != 2
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | $@ = $eval_err; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | return; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | 1; | |||||||||||||||||||||||||||