| 1 | # ex:ts=8 sw=4: | ||||||||||||||||||||||||||||
| 2 | # $OpenBSD$ | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Copyright (c) 2026 Dick Olsson <hi@senzilla.io> | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Permission to use, copy, modify, and distribute this software for any | ||||||||||||||||||||||||||||
| 7 | # purpose with or without fee is hereby granted, provided that the above | ||||||||||||||||||||||||||||
| 8 | # copyright notice and this permission notice appear in all copies. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||||||||||||||||||||||||||||
| 11 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||||||||||||||||||||||||||||
| 12 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||||||||||||||||||||||||||||
| 13 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||||||||||||||||||||||||||||
| 14 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||||||||||||||||||||||||||||
| 15 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||||||||||||||||||||||||||||
| 16 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | package Protocol::HAP::Store::File; | ||||||||||||||||||||||||||||
| 21 | 2 | our $VERSION = '0.1.0'; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use Fcntl qw(O_CREAT O_EXCL O_TRUNC O_WRONLY); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use File::Path qw(make_path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use Protocol::HAP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # Protocol::HAP::Store::File - the store contract over files. | ||||||||||||||||||||||||||||
| 30 | # | ||||||||||||||||||||||||||||
| 31 | # This is the durable implementation of the twelve store methods that | ||||||||||||||||||||||||||||
| 32 | # Protocol/HAP/Store.pod documents. Everything here is HAP: the | ||||||||||||||||||||||||||||
| 33 | # long-term key files, the pairings format, the counters, and the rule | ||||||||||||||||||||||||||||
| 34 | # that every pairing change bumps the configuration number. | ||||||||||||||||||||||||||||
| 35 | # | ||||||||||||||||||||||||||||
| 36 | # The tier holds a sans-IO engine, and this module writes files. That | ||||||||||||||||||||||||||||
| 37 | # is the point: sans-IO describes Protocol::HAP::Server, and the store | ||||||||||||||||||||||||||||
| 38 | # is the injected seam that keeps the engine pure. | ||||||||||||||||||||||||||||
| 39 | # | ||||||||||||||||||||||||||||
| 40 | # The layout on disk is: | ||||||||||||||||||||||||||||
| 41 | # | ||||||||||||||||||||||||||||
| 42 | # <path>/accessory_ltsk mode 0600 | ||||||||||||||||||||||||||||
| 43 | # <path>/accessory_ltpk mode 0644 | ||||||||||||||||||||||||||||
| 44 | # <path>/pairings.db mode 0600 | ||||||||||||||||||||||||||||
| 45 | # <path>/state.json mode 0600, the counters | ||||||||||||||||||||||||||||
| 46 | # | ||||||||||||||||||||||||||||
| 47 | # Every write sets the mode at the open, before the first byte. A | ||||||||||||||||||||||||||||
| 48 | # chmod after the write leaves a window in which the identity of the | ||||||||||||||||||||||||||||
| 49 | # accessory is world-readable. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Protocol::HAP::Store::File->new(%args): | ||||||||||||||||||||||||||||
| 52 | # path => $dir the state directory (required) | ||||||||||||||||||||||||||||
| 53 | # logger => $obj a logger (default the null logger) | ||||||||||||||||||||||||||||
| 54 | # | ||||||||||||||||||||||||||||
| 55 | # The caller owns the path. This module holds no host policy, so | ||||||||||||||||||||||||||||
| 56 | # it carries no default location. | ||||||||||||||||||||||||||||
| 57 | 15 | ▶ | sub new ( $class, %args ) | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||
| 59 | 15 | my $path = $args{path}; | |||||||||||||||||||||||||||
| 60 | 15 | ▶ | croak 'path parameter required' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $path and length $path
MC/DC50%1/2
defined $path and length $path
defined $path
length $path
Truth table66%2/3
A and B
A= defined $pathB= length $path
| |||||||||||||||||||||||||||||
| 61 | unless defined $path && length $path; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | my $self = bless { | ||||||||||||||||||||||||||||
| 64 | path => $path, | ||||||||||||||||||||||||||||
| 65 | 14 | ▶ | logger => $args{logger} // Protocol::HAP->null_logger, | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{"logger"} // "Protocol::HAP"->null_logger
MC/DC0%0/2
$args{"logger"} // "Protocol::HAP"->null_logger
$args{"logger"}
"Protocol::HAP"->null_logger
Truth table33%1/3
A // B
A= $args{"logger"}B= "Protocol::HAP"->null_logger
| |||||||||||||||||||||||||||||
| 66 | pairings_file => "$path/pairings.db", | ||||||||||||||||||||||||||||
| 67 | accessory_ltsk_file => "$path/accessory_ltsk", | ||||||||||||||||||||||||||||
| 68 | accessory_ltpk_file => "$path/accessory_ltpk", | ||||||||||||||||||||||||||||
| 69 | state_file => "$path/state.json", | ||||||||||||||||||||||||||||
| 70 | state => {}, | ||||||||||||||||||||||||||||
| 71 | }, $class; | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 14 | ▶ | $self->_ensure_dir( $path, 0700 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | or croak "Cannot create the state directory $path"; | ||||||||||||||||||||||||||||
| 75 | 14 | $self->_load_state; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 14 | return $self; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # --- the accessory identity ----------------------------------------------- | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 4 | ▶ | sub load_accessory_keys ($self) | ||||||||||||||||||||||||||
|
Subroutine
load_accessory_keys: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||
| 84 | 4 | ▶ | if ( -f $self->{accessory_ltsk_file} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-f $self->{"accessory_ltsk_file"} and -f $self->{"accessory_ltpk_file"}
MC/DC50%1/2
-f $self->{"accessory_ltsk_file"} and -f $self->{"accessory_ltpk_file"}
-f $self->{"accessory_ltsk_file"}
-f $self->{"accessory_ltpk_file"}
Truth table66%2/3
A and B
A= -f $self->{"accessory_ltsk_file"}B= -f $self->{"accessory_ltpk_file"}
| |||||||||||||||||||||||||||||
| 85 | && -f $self->{accessory_ltpk_file} ) | ||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||
| 87 | 2 | $self->{logger}->debug('Loading accessory keys from storage'); | |||||||||||||||||||||||||||
| 88 | 2 | my $ltsk = $self->_read( $self->{accessory_ltsk_file} ); | |||||||||||||||||||||||||||
| 89 | 2 | my $ltpk = $self->_read( $self->{accessory_ltpk_file} ); | |||||||||||||||||||||||||||
| 90 | 2 | return ( $ltsk, $ltpk ); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 2 | $self->{logger}->debug('No existing accessory keys found'); | |||||||||||||||||||||||||||
| 94 | 2 | return (); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 4 | ▶ | sub save_accessory_keys ( $self, $ltsk, $ltpk ) | ||||||||||||||||||||||||||
|
Subroutine
save_accessory_keys: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | 4 | $self->{logger}->debug('Generating and saving new accessory keys'); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 4 | ▶ | $self->_write( $self->{accessory_ltsk_file}, $ltsk, 0600 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | or croak 'Cannot store the accessory secret key'; | ||||||||||||||||||||||||||||
| 103 | 4 | ▶ | $self->_write( $self->{accessory_ltpk_file}, $ltpk, 0644 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | or croak 'Cannot store the accessory public key'; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 4 | return; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # --- the pairings --------------------------------------------------------- | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 26 | ▶ | sub load_pairings ($self) | ||||||||||||||||||||||||||
|
Subroutine
load_pairings: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||
| 113 | 26 | ▶ | return {} unless -f $self->{pairings_file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 20 | $self->{logger}->debug('Loading pairings from storage'); | |||||||||||||||||||||||||||
| 116 | 20 | my %pairings; | |||||||||||||||||||||||||||
| 117 | open my $fh, '<', $self->{pairings_file} | ||||||||||||||||||||||||||||
| 118 | 20 | ▶ | or die 'Cannot open pairings file: ' . _reason($!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 20 | ▶ | while ( my $line = <$fh> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 113 | chomp $line; | |||||||||||||||||||||||||||
| 122 | 113 | ▶ | next if $line =~ /^#/ || $line =~ /^\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$line =~ /^#/u or $line =~ /^\s*$/u
MC/DC100%2/2
$line =~ /^#/u or $line =~ /^\s*$/u
$line =~ /^#/u
$line =~ /^\s*$/u
Truth table100%3/3
A or B
A= $line =~ /^#/uB= $line =~ /^\s*$/u
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Format: controller_id:ltpk_hex:permissions | ||||||||||||||||||||||||||||
| 125 | 33 | ▶ | if ( $line =~ /^([^:]+):([^:]+):([01])$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 33 | my ( $id, $ltpk_hex, $perms ) = ( $1, $2, $3 ); | |||||||||||||||||||||||||||
| 127 | 33 | $pairings{$id} = { | |||||||||||||||||||||||||||
| 128 | ltpk => pack( 'H*', $ltpk_hex ), | ||||||||||||||||||||||||||||
| 129 | permissions => $perms, | ||||||||||||||||||||||||||||
| 130 | }; | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 20 | close $fh; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 20 | return \%pairings; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 11 | ▶ | sub save_pairing ( $self, $controller_id, $ltpk, $permissions = 1 ) | ||||||||||||||||||||||||||
|
Subroutine
save_pairing: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | { | ||||||||||||||||||||||||||||
| 141 | $self->{logger} | ||||||||||||||||||||||||||||
| 142 | 11 | ->debug( 'Saving pairing for controller: %s', $controller_id ); | |||||||||||||||||||||||||||
| 143 | 11 | my $pairings = $self->load_pairings; | |||||||||||||||||||||||||||
| 144 | 11 | $pairings->{$controller_id} = { | |||||||||||||||||||||||||||
| 145 | ltpk => $ltpk, | ||||||||||||||||||||||||||||
| 146 | permissions => $permissions, | ||||||||||||||||||||||||||||
| 147 | }; | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 11 | $self->_save_pairings($pairings); | |||||||||||||||||||||||||||
| 150 | 11 | $self->increment_config_number(); | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 11 | return; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 5 | ▶ | sub remove_pairing ( $self, $controller_id ) | ||||||||||||||||||||||||||
|
Subroutine
remove_pairing: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 156 | { | ||||||||||||||||||||||||||||
| 157 | $self->{logger} | ||||||||||||||||||||||||||||
| 158 | 5 | ->debug( 'Removing pairing for controller: %s', $controller_id ); | |||||||||||||||||||||||||||
| 159 | 5 | my $pairings = $self->load_pairings; | |||||||||||||||||||||||||||
| 160 | 5 | delete $pairings->{$controller_id}; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 5 | $self->_save_pairings($pairings); | |||||||||||||||||||||||||||
| 163 | 5 | $self->increment_config_number(); | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 5 | return; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # remove_all_pairings() - Remove all pairings. This is a factory | ||||||||||||||||||||||||||||
| 169 | # reset. The engine calls this method when it removes the last admin | ||||||||||||||||||||||||||||
| 170 | # pairing (HAP-Pairing.md ยง7.2). | ||||||||||||||||||||||||||||
| 171 | 2 | ▶ | sub remove_all_pairings ($self) | ||||||||||||||||||||||||||
|
Subroutine
remove_all_pairings: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 172 | { | ||||||||||||||||||||||||||||
| 173 | 2 | $self->{logger}->debug('Removing all pairings'); | |||||||||||||||||||||||||||
| 174 | 2 | $self->_save_pairings( {} ); | |||||||||||||||||||||||||||
| 175 | 2 | $self->increment_config_number(); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 2 | return; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 18 | ▶ | sub _save_pairings ( $self, $pairings ) | ||||||||||||||||||||||||||
|
Subroutine
_save_pairings: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 181 | { | ||||||||||||||||||||||||||||
| 182 | 18 | my $text = | |||||||||||||||||||||||||||
| 183 | "# OpenHAP Pairings Database\n" | ||||||||||||||||||||||||||||
| 184 | . "# Format: controller_id:ltpk_hex:permissions\n" | ||||||||||||||||||||||||||||
| 185 | . "# Permissions: 1=admin, 0=regular\n\n"; | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 18 | for my $id ( sort keys %$pairings ) { | |||||||||||||||||||||||||||
| 188 | 24 | my $ltpk_hex = unpack( 'H*', $pairings->{$id}{ltpk} ); | |||||||||||||||||||||||||||
| 189 | 24 | my $perms = $pairings->{$id}{permissions}; | |||||||||||||||||||||||||||
| 190 | 24 | $text .= "$id:$ltpk_hex:$perms\n"; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # The file is 0600 from its first byte. A pairing record names | ||||||||||||||||||||||||||||
| 194 | # every controller that may reach the accessory. | ||||||||||||||||||||||||||||
| 195 | 18 | ▶ | $self->_write( $self->{pairings_file}, $text, 0600 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | or croak 'Cannot write the pairings file'; | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 18 | return; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | # --- the counters --------------------------------------------------------- | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # The configuration number tells a controller that the accessory | ||||||||||||||||||||||||||||
| 204 | # database changed [HAP-Accessories]. It only ever goes up: a | ||||||||||||||||||||||||||||
| 205 | # controller that sees it go backwards drops the accessory and pairs | ||||||||||||||||||||||||||||
| 206 | # again. Thus the value starts at 1, not at 0. | ||||||||||||||||||||||||||||
| 207 | 35 | ▶ | sub get_config_number ($self) | ||||||||||||||||||||||||||
|
Subroutine
get_config_number: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 208 | { | ||||||||||||||||||||||||||||
| 209 | 35 | my $number = $self->{state}{config_number}; | |||||||||||||||||||||||||||
| 210 | 35 | ▶ | return 1 unless defined $number && $number =~ /^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $number and $number =~ /^\d+$/u
MC/DC50%1/2
defined $number and $number =~ /^\d+$/u
defined $number
$number =~ /^\d+$/u
Truth table66%2/3
A and B
A= defined $numberB= $number =~ /^\d+$/u
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 25 | return $number; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 20 | ▶ | sub increment_config_number ($self) | ||||||||||||||||||||||||||
|
Subroutine
increment_config_number: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 216 | { | ||||||||||||||||||||||||||||
| 217 | 20 | my $number = $self->get_config_number + 1; | |||||||||||||||||||||||||||
| 218 | 20 | $self->_set( config_number => $number ); | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 20 | return $number; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 4 | ▶ | sub get_config_digest ($self) | ||||||||||||||||||||||||||
|
Subroutine
get_config_digest: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 224 | { | ||||||||||||||||||||||||||||
| 225 | 4 | return $self->{state}{config_digest}; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 2 | ▶ | sub save_config_digest ( $self, $digest ) | ||||||||||||||||||||||||||
|
Subroutine
save_config_digest: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 229 | { | ||||||||||||||||||||||||||||
| 230 | 2 | $self->_set( config_digest => $digest ); | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 2 | return; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 7 | ▶ | sub get_auth_attempts ($self) | ||||||||||||||||||||||||||
|
Subroutine
get_auth_attempts: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 236 | { | ||||||||||||||||||||||||||||
| 237 | 7 | my $count = $self->{state}{auth_attempts}; | |||||||||||||||||||||||||||
| 238 | 7 | ▶ | return 0 unless defined $count && $count =~ /^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $count and $count =~ /^\d+$/u
MC/DC50%1/2
defined $count and $count =~ /^\d+$/u
defined $count
$count =~ /^\d+$/u
Truth table66%2/3
A and B
A= defined $countB= $count =~ /^\d+$/u
| |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 4 | return $count; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 6 | ▶ | sub set_auth_attempts ( $self, $count ) | ||||||||||||||||||||||||||
|
Subroutine
set_auth_attempts: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 244 | { | ||||||||||||||||||||||||||||
| 245 | 6 | $self->_set( auth_attempts => $count ); | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 6 | return; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # --- the state file ------------------------------------------------------- | ||||||||||||||||||||||||||||
| 251 | # | ||||||||||||||||||||||||||||
| 252 | # The counters live in one JSON file. The load tolerates a missing file | ||||||||||||||||||||||||||||
| 253 | # and a corrupt one: a state file that a crash truncated must not stop | ||||||||||||||||||||||||||||
| 254 | # the daemon that would rewrite it. The save writes through a temporary | ||||||||||||||||||||||||||||
| 255 | # file and renames over the target, so the file is never the corrupt | ||||||||||||||||||||||||||||
| 256 | # one that the next load has to tolerate. | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 14 | ▶ | sub _load_state ($self) | ||||||||||||||||||||||||||
|
Subroutine
_load_state: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||
| 259 | { | ||||||||||||||||||||||||||||
| 260 | 14 | $self->{state} = {}; | |||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 14 | ▶ | return $self unless -f $self->{state_file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 3 | my $content = $self->_read( $self->{state_file} ); | |||||||||||||||||||||||||||
| 265 | 3 | ▶ | return $self if !defined $content || $content eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $content or $content eq ""
MC/DC0%0/2
not defined $content or $content eq ""
not defined $content
$content eq ""
Truth table33%1/3
not(A) or B
A= not defined $contentB= $content eq ""
| |||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 3 | my $data = eval { JSON::PP->new->utf8->decode($content) }; | |||||||||||||||||||||||||||
| 268 | 3 | ▶ | unless ( defined $data && ref $data eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $data and ref $data eq "HASH"
MC/DC50%1/2
defined $data and ref $data eq "HASH"
defined $data
ref $data eq "HASH"
Truth table66%2/3
A and B
A= defined $dataB= ref $data eq "HASH"
| |||||||||||||||||||||||||||||
| 269 | $self->{logger}->warning( 'Cannot read state from %s: %s', | ||||||||||||||||||||||||||||
| 270 | 1 | $self->{state_file}, _reason($@) ); | |||||||||||||||||||||||||||
| 271 | 1 | return $self; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 2 | $self->{state} = $data; | |||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 2 | return $self; | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 28 | ▶ | sub _save_state ($self) | ||||||||||||||||||||||||||
|
Subroutine
_save_state: called CC=2 SCAR=7.9
| |||||||||||||||||||||||||||||
| 280 | { | ||||||||||||||||||||||||||||
| 281 | # The encoding is canonical, so the same data always gives the | ||||||||||||||||||||||||||||
| 282 | # same bytes and a diff of two state files is readable. | ||||||||||||||||||||||||||||
| 283 | my $json = | ||||||||||||||||||||||||||||
| 284 | 28 | eval { JSON::PP->new->utf8->canonical->encode( $self->{state} ) }; | |||||||||||||||||||||||||||
| 285 | 28 | ▶ | unless ( defined $json ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | $self->{logger}->error( 'Cannot encode %s: %s', | ||||||||||||||||||||||||||||
| 287 | 0 | $self->{state_file}, _reason($@) ); | |||||||||||||||||||||||||||
| 288 | 0 | return; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 28 | return $self->_write_atomic( $self->{state_file}, $json, 0600 ); | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 28 | ▶ | sub _set ( $self, $key, $value ) | ||||||||||||||||||||||||||
|
Subroutine
_set: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 295 | { | ||||||||||||||||||||||||||||
| 296 | 28 | $self->{state}{$key} = $value; | |||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 28 | return $self->_save_state; | |||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | # --- the file operations -------------------------------------------------- | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | # $self->_read($path): | ||||||||||||||||||||||||||||
| 304 | # Read a whole file and return its bytes, or undef. | ||||||||||||||||||||||||||||
| 305 | 7 | ▶ | sub _read ( $self, $path ) | ||||||||||||||||||||||||||
|
Subroutine
_read: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||
| 306 | { | ||||||||||||||||||||||||||||
| 307 | 7 | ▶ | open my $fh, '<', $path or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 0 | $self->{logger}->debug( 'Cannot read %s: %s', $path, $! ); | |||||||||||||||||||||||||||
| 309 | 0 | return; | |||||||||||||||||||||||||||
| 310 | }; | ||||||||||||||||||||||||||||
| 311 | 7 | binmode $fh; | |||||||||||||||||||||||||||
| 312 | 7 | my $content = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 313 | 7 | close $fh; | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 7 | ▶ | return $content // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$content // ""
MC/DC0%0/1
$content // ""
$content
Truth table50%1/2
A
A= $content
| |||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | # $self->_write($path, $data, $mode): | ||||||||||||||||||||||||||||
| 319 | # Write the bytes with the mode applied at the open. An existing | ||||||||||||||||||||||||||||
| 320 | # file keeps its own mode through the open, so the method removes | ||||||||||||||||||||||||||||
| 321 | # it first and the mode argument means what it says. | ||||||||||||||||||||||||||||
| 322 | 26 | ▶ | sub _write ( $self, $path, $data, $mode ) | ||||||||||||||||||||||||||
|
Subroutine
_write: called CC=4 SCAR=14.8
| |||||||||||||||||||||||||||||
| 323 | { | ||||||||||||||||||||||||||||
| 324 | 26 | ▶ | unlink $path if -e $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 26 | ▶ | sysopen my $fh, $path, O_CREAT | O_WRONLY | O_TRUNC, $mode or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | 0 | $self->{logger}->error( 'Cannot write %s: %s', $path, $! ); | |||||||||||||||||||||||||||
| 328 | 0 | return; | |||||||||||||||||||||||||||
| 329 | }; | ||||||||||||||||||||||||||||
| 330 | 26 | binmode $fh; | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 26 | my $ok = $self->_write_all( $fh, $data, $path ); | |||||||||||||||||||||||||||
| 333 | 26 | ▶ | close $fh or $ok = undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 26 | return $ok; | |||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | # $self->_write_atomic($path, $data, $mode): | ||||||||||||||||||||||||||||
| 339 | # Write through a temporary file in the same directory, then | ||||||||||||||||||||||||||||
| 340 | # rename over the target. A reader sees the old content or the new | ||||||||||||||||||||||||||||
| 341 | # content, never a half-written file. The rename must stay inside | ||||||||||||||||||||||||||||
| 342 | # one filesystem, so the temporary name is a sibling. | ||||||||||||||||||||||||||||
| 343 | 28 | ▶ | sub _write_atomic ( $self, $path, $data, $mode ) | ||||||||||||||||||||||||||
|
Subroutine
_write_atomic: called CC=6 SCAR=24.5
| |||||||||||||||||||||||||||||
| 344 | { | ||||||||||||||||||||||||||||
| 345 | # A hidden sibling, as the host helper used before the move. The | ||||||||||||||||||||||||||||
| 346 | # state directory thus looks the same to an operator. | ||||||||||||||||||||||||||||
| 347 | 28 | my $temp = $path =~ s{(^|/)([^/]+)$}{${1}.$2.$$.tmp}r; | |||||||||||||||||||||||||||
| 348 | 28 | ▶ | unlink $temp if -e $temp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 28 | ▶ | sysopen my $fh, $temp, O_CREAT | O_EXCL | O_WRONLY, $mode or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | 0 | $self->{logger}->error( 'Cannot write %s: %s', $temp, $! ); | |||||||||||||||||||||||||||
| 352 | 0 | return; | |||||||||||||||||||||||||||
| 353 | }; | ||||||||||||||||||||||||||||
| 354 | 28 | binmode $fh; | |||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 28 | ▶ | unless ( $self->_write_all( $fh, $data, $temp ) && close $fh ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->_write_all($fh, $data, $temp) and close $fh
MC/DC0%0/2
$self->_write_all($fh, $data, $temp) and close $fh
$self->_write_all($fh, $data, $temp)
close $fh
Truth table33%1/3
A and B
A= $self->_write_all($fh, $data, $temp)B= close $fh
| |||||||||||||||||||||||||||||
| 357 | 0 | close $fh; | |||||||||||||||||||||||||||
| 358 | 0 | unlink $temp; | |||||||||||||||||||||||||||
| 359 | 0 | return; | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 28 | ▶ | unless ( rename $temp, $path ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 363 | $self->{logger} | ||||||||||||||||||||||||||||
| 364 | 0 | ->error( 'Cannot rename %s to %s: %s', $temp, $path, $! ); | |||||||||||||||||||||||||||
| 365 | 0 | unlink $temp; | |||||||||||||||||||||||||||
| 366 | 0 | return; | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 28 | return 1; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | # $self->_write_all($fh, $data, $path): | ||||||||||||||||||||||||||||
| 373 | # Write every byte. A short syswrite is not an error by itself, so | ||||||||||||||||||||||||||||
| 374 | # the loop continues until the data is gone. | ||||||||||||||||||||||||||||
| 375 | 54 | ▶ | sub _write_all ( $self, $fh, $data, $path ) | ||||||||||||||||||||||||||
|
Subroutine
_write_all: called CC=4 SCAR=16.1
| |||||||||||||||||||||||||||||
| 376 | { | ||||||||||||||||||||||||||||
| 377 | 54 | my $offset = 0; | |||||||||||||||||||||||||||
| 378 | 54 | ▶ | while ( $offset < length $data ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | 54 | my $n = syswrite $fh, $data, length($data) - $offset, $offset; | |||||||||||||||||||||||||||
| 380 | 54 | ▶ | unless ( defined $n ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 381 | 0 | ▶ | next if $!{EINTR}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 382 | $self->{logger} | ||||||||||||||||||||||||||||
| 383 | 0 | ->error( 'Cannot write %s: %s', $path, $! ); | |||||||||||||||||||||||||||
| 384 | 0 | return; | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | 54 | $offset += $n; | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 54 | return 1; | |||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | # $self->_ensure_dir($path, $mode): | ||||||||||||||||||||||||||||
| 393 | # Make sure the directory exists. The method refuses a symlink and | ||||||||||||||||||||||||||||
| 394 | # refuses a path that exists as something else. Both are conditions | ||||||||||||||||||||||||||||
| 395 | # that a daemon must not write through. | ||||||||||||||||||||||||||||
| 396 | 14 | ▶ | sub _ensure_dir ( $self, $path, $mode ) | ||||||||||||||||||||||||||
|
Subroutine
_ensure_dir: called CC=6 SCAR=21.2
| |||||||||||||||||||||||||||||
| 397 | { | ||||||||||||||||||||||||||||
| 398 | 14 | ▶ | if ( -l $path ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 399 | 0 | $self->{logger}->error( 'Directory is a symlink: %s', $path ); | |||||||||||||||||||||||||||
| 400 | 0 | return; | |||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | 14 | ▶ | if ( -e $path && !-d $path ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-e $path and not -d $path
MC/DC0%0/2
-e $path and not -d $path
-e $path
not -d $path
Truth table66%2/3
A and not(B)
A= -e $pathB= not -d $path
| |||||||||||||||||||||||||||||
| 403 | 0 | $self->{logger}->error( 'Path is not a directory: %s', $path ); | |||||||||||||||||||||||||||
| 404 | 0 | return; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | 14 | ▶ | return 1 if -d $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 1 | eval { make_path( $path, { mode => $mode } ); 1 }; | |||||||||||||||||||||||||||
| 409 | 1 | ▶ | unless ( -d $path ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | # make_path dies with a message that names the module and | ||||||||||||||||||||||||||||
| 412 | # the line. A daemon log wants the reason only. | ||||||||||||||||||||||||||||
| 413 | $self->{logger} | ||||||||||||||||||||||||||||
| 414 | 0 | ->error( 'Cannot create %s: %s', $path, _reason($@) ); | |||||||||||||||||||||||||||
| 415 | 0 | return; | |||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 1 | return 1; | |||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | # _reason($error): | ||||||||||||||||||||||||||||
| 422 | # Reduce a die message to one line with no file and line number. | ||||||||||||||||||||||||||||
| 423 | # A daemon log is not the place for a Perl backtrace. | ||||||||||||||||||||||||||||
| 424 | 1 | ▶ | sub _reason ($error) | ||||||||||||||||||||||||||
|
Subroutine
_reason: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 425 | { | ||||||||||||||||||||||||||||
| 426 | 1 | ▶ | my $why = $error // 'unknown error'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$error // "unknown error"
MC/DC0%0/1
$error // "unknown error"
$error
Truth table50%1/2
A
A= $error
| |||||||||||||||||||||||||||||
| 427 | 1 | $why =~ s/ at \S+ line \d+.*//s; | |||||||||||||||||||||||||||
| 428 | 1 | $why =~ s/\s+$//; | |||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 1 | ▶ | return length($why) ? $why : 'unknown error'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 2 | 1; | |||||||||||||||||||||||||||