JSONUnix.pm

filter: Statementstmt 53.6% 252 / 470 Branchbran 29.9% 76 / 254 Conditioncond 32.6% 34 / 104 MC/DCmcdc 18.0% 11 / 61 Subroutinesub 78.4% 51 / 65 Podpod 100% 4 / 4 totaltotal 44.6% 428 / 958 CC 178 SCAR 52.3
CC
178
Cov
44%
_permission_verdict
1122.0
_read_peer_cred
182.6
__ANON__
97.0
CRAP 186.2
↑ Summary JSONUnix/BlockingClient.pm »
1 package POE::Component::Server::JSONUnix;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Socket qw(PF_UNIX SOCK_STREAM SOL_SOCKET);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use IO::Socket::UNIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 ▶use POE qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 Wheel::SocketFactory
17 Wheel::ReadWrite
18 Filter::Line
19 Driver::SysRW
20 10 );
21  
22 10 our $VERSION = '0.2.1';
23  
24 =head1 NAME
25  
26 POE::Component::Server::JSONUnix - pluggable JSON-over-Unix-socket server for POE
27  
28 =head1 SYNOPSIS
29  
30 use POE;
31 use POE::Component::Server::JSONUnix;
32  
33 my $server = POE::Component::Server::JSONUnix->spawn(
34 socket_path => '/tmp/app.sock',
35 socket_mode => 0600,
36 commands => {
37 echo => sub {
38 my ($server, $request, $ctx) = @_;
39 return { echoed => $request->{args} };
40 },
41 },
42 );
43  
44 # Add more commands at any time.
45 $server->register(
46 add => sub {
47 my ($server, $request, $ctx) = @_;
48 my $sum = 0;
49 $sum += $_ for @{ $request->{args}{numbers} // [] };
50 return { sum => $sum };
51 },
52 );
53  
54 $poe_kernel->run;
55  
56 =head1 DESCRIPTION
57  
58 This module is a small, event-driven server that listens on a Unix domain
59 socket and speaks a simple JSON request/response protocol. It is built on
60 L<POE> and is designed to be extended: the set of commands it understands is a
61 plain dispatch table you can add to at construction time, at run time, or by
62 subclassing.
63  
64 It is suitable as a local control or RPC endpoint for a daemon -- the sort of
65 thing you talk to from a command-line tool, a cron job, or another process on
66 the same host.
67  
68 =head1 PROTOCOL
69  
70 The framing is newline-delimited JSON: each message is a single JSON object on
71 its own line, terminated by C<\n>.
72  
73 A request looks like:
74  
75 {"command":"add","args":{"numbers":[1,2,3]},"id":7}
76  
77 =over 4
78  
79 =item *
80  
81 C<command> (required) -- the name of the command to run. C<cmd> is accepted as
82 an alias.
83  
84 =item *
85  
86 C<args> (optional) -- an arbitrary payload passed straight through to the
87 handler.
88  
89 =item *
90  
91 C<id> (optional) -- an opaque value echoed back in the response so asynchronous
92 clients can correlate replies with requests.
93  
94 =back
95  
96 A successful response:
97  
98 {"id":7,"status":"ok","result":{"sum":6}}
99  
100 An error response:
101  
102 {"id":7,"status":"error","error":"unknown command: subtract"}
103  
104 Malformed JSON, a non-object request, a missing command, an unknown command, or
105 a handler that dies all produce an C<error> response rather than disturbing the
106 server or other clients.
107  
108 =head1 CONSTRUCTOR
109  
110 =head2 spawn
111  
112 my $server = POE::Component::Server::JSONUnix->spawn(%args);
113  
114 Creates the server's POE session and returns the server object. Recognised
115 arguments:
116  
117 =head3 socket_path
118  
119 Required. Filesystem path of the Unix domain socket to listen on. If a stale
120 socket file is present it is removed; if another process is actively
121 listening there, C<spawn> dies rather than clobber it.
122  
123 =head3 commands
124  
125 Hash reference of C<< name => \&handler >> pairs to register. See
126 L</"COMMAND HANDLERS">.
127  
128 =head3 socket_mode
129  
130 If set (e.g. C<0600>), C<chmod> the socket to these permissions after
131 binding. Unix socket permissions govern who may connect, so setting this is
132 recommended.
133  
134 =head3 alias
135  
136 POE session alias. Defaults to C<json_unix_server>. Set this if you run more
137 than one server in a single process.
138  
139 =head3 unlink_existing
140  
141 Whether to remove a stale (not-in-use) socket file on startup. Defaults to
142 true.
143  
144 =head3 on_error
145  
146 Code reference called as
147 C<< $cb->($operation, $errnum, $errstr [, $wheel_id]) >> on listen and
148 connection I/O errors. Normal client disconnects are not reported.
149  
150 =head3 auth_temp_dir
151  
152 Directory used for the cookie-file ownership challenge. Defaults to
153 C<File::Spec-E<gt>tmpdir> (usually C</tmp>).
154  
155 =head3 auth_required
156  
157 If set to a true value, all commands except C<auth_start> and C<auth_verify>
158 return an error until the client has successfully completed the ownership
159 challenge. Defaults to false.
160  
161 =head3 auth_method
162  
163 How clients prove their identity. One of:
164  
165 =over 4
166  
167 =item C<auto> (default)
168  
169 Use kernel peer credentials when the platform supports them (Linux, FreeBSD,
170 macOS, and other BSDs -- see L</"KERNEL PEER-CREDENTIAL AUTHENTICATION">),
171 falling back to the cookie-file challenge otherwise. C<auth_start> advertises
172 the kernel path with C<< peercred => 1 >> while still returning a
173 C<cookie>/C<temp_dir>, so a client may use either.
174  
175 =item C<peercred>
176  
177 Offer only kernel peer credentials. No cookie is issued; C<auth_verify>
178 requires no file. On a platform without support, authentication cannot
179 complete.
180  
181 =item C<cookie>
182  
183 Offer only the cookie-file challenge, exactly as earlier versions did, even
184 where kernel peer credentials are available.
185  
186 =back
187  
188 =head3 permissions
189  
190 Optional user/group permission policy, a hash reference of the form
191 C<< {default => 'allow'|'deny', commands => {name => $spec, ...}} >>. When
192 this argument is not given the server behaves exactly as it does without the
193 feature. See L</"PERMISSIONS">.
194  
195 =cut
196  
197 11 ▶sub spawn {
Subroutine spawn: called CC=13 SCAR=26.0
Pod documented
198 11 my ( $class, %args ) = @_;
199  
200 my $path = delete $args{socket_path}
201 11 ▶ or croak "spawn() requires a 'socket_path' argument";
Branch: 1 branch
BranchTrueFalseTotal
my $path = delete $args{"socket_path"} or croak("spawn() requires a 'socket_path' argument") 11 0 11
202  
203 my $self = bless {
204 socket_path => $path,
205 alias => ( delete $args{alias} ) // 'json_unix_server',
206 socket_mode => delete $args{socket_mode}, # e.g. 0600
207 unlink_existing => ( delete $args{unlink_existing} ) // 1,
208 on_error => delete $args{on_error}, # coderef (optional)
209 auth_temp_dir => ( delete $args{auth_temp_dir} ) // File::Spec->tmpdir,
210 auth_required => ( delete $args{auth_required} ) // 0,
211 11 ▶ auth_method => ( delete $args{auth_method} ) // 'auto',
Condition81%9/11
delete $args{"alias"} // "json_unix_server"
l!l
29
delete $args{"unlink_existing"} // 1
l!l
011
delete $args{"auth_temp_dir"} // "File::Spec"->tmpdir
l!l&&r!l&&!r
1100
delete $args{"auth_required"} // 0
l!l
110
delete $args{"auth_method"} // "auto"
l!l
110
MC/DC100%1/1
delete $args{"alias"} // "json_unix_server"
delete $args{"alias"}
MC/DC0%0/1
delete $args{"unlink_existing"} // 1
delete $args{"unlink_existing"}
MC/DC0%0/2
delete $args{"auth_temp_dir"} // "File::Spec"->tmpdir
delete $args{"auth_temp_dir"} "File::Spec"->tmpdir
MC/DC100%1/1
delete $args{"auth_required"} // 0
delete $args{"auth_required"}
MC/DC100%1/1
delete $args{"auth_method"} // "auto"
delete $args{"auth_method"}
Truth table100%2/2
A
A= delete $args{"alias"}
Aresult
11
00
Truth table50%1/2
A
A= delete $args{"unlink_existing"}
Aresult
11
00
Truth table66%2/3
A // B
A= delete $args{"auth_temp_dir"}B= "File::Spec"->tmpdir
ABresult
1X1
011
000
Truth table100%2/2
A
A= delete $args{"auth_required"}
Aresult
11
00
Truth table100%2/2
A
A= delete $args{"auth_method"}
Aresult
11
00
212 permissions => undef, # stays undef unless a policy is passed to spawn
213 commands => {},
214 clients => {},
215 json => JSON::MaybeXS->new(
216 utf8 => 1,
217 canonical => 1,
218 allow_nonref => 0,
219 ),
220 }, $class;
221  
222 croak "'auth_method' must be 'auto', 'cookie', or 'peercred'"
223 unless $self->{auth_method} eq 'auto'
224 || $self->{auth_method} eq 'cookie'
225 11 ▶ || $self->{auth_method} eq 'peercred';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"auth_method"} eq "auto" or $self->{"auth_method"} eq "cookie" or $self->{"auth_method"} eq "peercred" 1 10 11
Condition66%4/6
$self->{"auth_method"} eq "auto" or $self->{"auth_method"} eq "cookie" or $self->{"auth_method"} eq "peercred"
l!l&&r!l&&!r
1001
$self->{"auth_method"} eq "auto" or $self->{"auth_method"} eq "cookie"
l!l&&r!l&&!r
1001
MC/DC33%1/3
$self->{"auth_method"} eq "auto" or $self->{"auth_method"} eq "cookie" or $self->{"auth_method"} eq "peercred"
$self->{"auth_method"} eq "auto" $self->{"auth_method"} eq "cookie" $self->{"auth_method"} eq "peercred"
Truth table50%2/4
A or B or C
A= $self->{"auth_method"} eq "auto"B= $self->{"auth_method"} eq "cookie"C= $self->{"auth_method"} eq "peercred"
ABCresult
1XX1
01X1
0011
0000
226  
227 # Precedence (later overrides earlier): built-ins < cmd_* methods < arg.
228 10 $self->_register_builtins;
229 10 $self->_register_cmd_methods;
230  
231 10 ▶ if ( defined( my $cmds = delete $args{commands} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cmds = delete $args{"commands"})) 2 8 10
232 2 ▶ croak "'commands' must be a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cmds eq "HASH" 0 2 2
233 unless ref $cmds eq 'HASH';
234 2 $self->register(%$cmds);
235 }
236  
237 # Optional user/group permission policy. Validated here so a bad policy
238 # fails spawn() synchronously; when the argument is absent the server
239 # behaves exactly as it did without the feature.
240 10 ▶ if ( defined( my $perms = delete $args{permissions} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $perms = delete $args{"permissions"})) 8 2 10
241 8 $self->{permissions} = _build_permissions($perms);
242 }
243  
244 # Fail fast and synchronously on a busy or unusable socket path, so the
245 # caller of spawn() gets the error rather than a dead session later.
246 2 $self->_prepare_socket_path;
247  
248 2 POE::Session->create(
249 object_states => [
250 $self => {
251 _start => '_poe_start',
252 _stop => '_poe_stop',
253 shutdown => '_poe_shutdown',
254 register_command => '_poe_register_command',
255 got_connection => '_poe_got_connection',
256 listen_error => '_poe_listen_error',
257 client_input => '_poe_client_input',
258 client_error => '_poe_client_error',
259 client_flushed => '_poe_client_flushed',
260 },
261 ],
262 );
263  
264 2 return $self;
265 } ## end sub spawn
266  
267 #--- command registration --------------------------------------------------
268  
269 =head1 METHODS
270  
271 =head2 register
272  
273 $server->register(name => \&handler, ...);
274  
275 Add or replace commands. Returns the server object. Croaks if a handler is not a
276 code reference.
277  
278 =cut
279  
280 # register(name => \&handler, ...) — add or replace commands at any time.
281 26 ▶sub register {
Subroutine register: called CC=3 SCAR=11.0
Pod documented
282 26 my ( $self, %cmds ) = @_;
283 26 for my $name ( sort keys %cmds ) {
284 49 my $code = $cmds{$name};
285 49 ▶ croak "Handler for command '$name' must be a code reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 1 48 49
286 unless ref $code eq 'CODE';
287 48 $self->{commands}{$name} = $code;
288 }
289 25 return $self;
290 } ## end sub register
291  
292 =head2 command_names
293  
294 my $names = $server->command_names; # array reference, sorted
295  
296 The names of all currently registered commands. (Also available to clients as
297 the built-in C<commands> command.)
298  
299 =cut
300  
301 2 ▶sub command_names { return [ sort keys %{ $_[0]->{commands} } ] }
Subroutine command_names: called CC=1 SCAR=0.0
Pod documented
302  
303 10 ▶sub _register_builtins {
Subroutine _register_builtins: called CC=1 SCAR=1.5
304 10 my ($self) = @_;
305 $self->register(
306 2 ▶ ping => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
307 2 my ( $server, $req, $ctx ) = @_;
308 2 return { pong => 1, time => time() };
309 },
310 0 ▶ commands => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
311 0 my ( $server, $req, $ctx ) = @_;
312 0 my $names = $server->command_names;
313 0 $names = [ grep { $ctx->may($_) } @$names ]
314 0 ▶ if $server->{permissions};
Branch: 1 branch
BranchTrueFalseTotal
if $server->{"permissions"} 0 0 0
315 0 return { commands => $names };
316 },
317 10 );
318 10 $self->_register_auth_builtins;
319 10 return;
320 } ## end sub _register_builtins
321  
322 # Discover cmd_<name> methods anywhere in the class hierarchy so a server can
323 # be built simply by subclassing this module and adding methods.
324 11 ▶sub _register_cmd_methods {
Subroutine _register_cmd_methods: called CC=6 SCAR=17.9
325 11 my ($self) = @_;
326 11 my %names;
327 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
328 11 for my $pkg ( @{ mro::get_linear_isa( ref $self ) } ) {
329 12 for my $sym ( keys %{"${pkg}::"} ) {
330 707 ▶ $names{$1} = 1 if $sym =~ /\Acmd_(.+)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $sym =~ /\Acmd_(.+)\z/ 2 705 707
331 }
332 }
333 11 for my $name ( keys %names ) {
334 2 my $method = "cmd_$name";
335 2 ▶ next unless $self->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($method) 0 2 2
336 1 ▶ $self->{commands}{$name} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
337 1 my ( $server, $req, $ctx ) = @_;
338 1 return $server->$method( $req, $ctx );
339 2 };
340 }
341 11 return;
342 } ## end sub _register_cmd_methods
343  
344 10 ▶sub _register_auth_builtins {
Subroutine _register_auth_builtins: called CC=1 SCAR=3.0
345 10 my ($self) = @_;
346  
347 $self->register(
348 2 ▶ auth_start => sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
349 2 my ( $server, $req, $ctx ) = @_;
350 2 my $id = $ctx->{wheel_id};
351 2 my $client = $server->{clients}{$id};
352 2 my $method = $server->{auth_method};
353  
354 2 my %result;
355  
356 # Advertise the kernel peer-credential path when the platform gave
357 # us the peer's uid at connect and the configured method allows it.
358 # A client seeing this may skip the cookie file entirely and call
359 # auth_verify with no path.
360 $result{peercred} = 1
361 2 ▶ if $method ne 'cookie' && defined $client->{peer_uid};
Branch: 1 branch
BranchTrueFalseTotal
if $method ne "cookie" and defined $client->{"peer_uid"} 2 0 2
Condition33%1/3
$method ne "cookie" and defined $client->{"peer_uid"}
!ll&&!rl&&r
002
MC/DC0%0/2
$method ne "cookie" and defined $client->{"peer_uid"}
$method ne "cookie" defined $client->{"peer_uid"}
Truth table33%1/3
A and B
A= $method ne "cookie"B= defined $client->{"peer_uid"}
ABresult
0X0
100
111
362  
363 # Unless the server is peercred-only, also hand out a cookie and
364 # temp_dir so an older client -- or one on a host where peercred is
365 # unavailable -- can fall back to the file-ownership challenge.
366 2 ▶ if ( $method ne 'peercred' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method ne "peercred") 2 0 2
367 2 my $cookie = _random_cookie();
368 2 $client->{auth_cookie} = $cookie;
369 2 $client->{auth_time} = time();
370 2 $result{cookie} = $cookie;
371 2 $result{temp_dir} = $server->{auth_temp_dir};
372 }
373  
374 2 return \%result;
375 },
376  
377 2 ▶ auth_verify => sub {
Subroutine __ANON__: called CC=11 SCAR=45.7
378 2 my ( $server, $req, $ctx ) = @_;
379 2 my $id = $ctx->{wheel_id};
380 2 my $client = $server->{clients}{$id};
381  
382 # No path given: authenticate from the kernel-supplied peer
383 # credentials captured at connect, when the platform provided them
384 # and the server permits that method.
385 2 my $path = $req->{args}{path};
386 2 ▶ return $server->_auth_verify_peercred( $ctx, $client )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 2 0 2
Condition33%1/3
defined $path and length $path
!ll&&!rl&&r
200
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
387 unless defined $path && length $path;
388  
389 0 ▶ unless ( defined $client->{auth_cookie} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $client->{"auth_cookie"}) 0 0 0
390 0 $ctx->error('no pending auth challenge: call auth_start first');
391 0 return;
392 }
393  
394 0 ▶ unless ( File::Spec->file_name_is_absolute($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($path)) 0 0 0
395 0 $ctx->error('args.path must be an absolute file path');
396 0 return;
397 }
398  
399 # Must be directly inside auth_temp_dir — no subdirectories.
400 0 my $temp_dir = $server->{auth_temp_dir};
401 0 my $dirname = File::Basename::dirname($path);
402 0 ▶ unless ( $dirname eq $temp_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dirname eq $temp_dir) 0 0 0
403 0 $ctx->error("path must be a file directly inside $temp_dir");
404 0 return;
405 }
406  
407 # lstat so we never follow symlinks.
408 0 my @st = lstat($path);
409 0 ▶ unless (@st) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@st) 0 0 0
410 0 delete $client->{auth_cookie};
411 0 $ctx->error('verification file not found');
412 0 return;
413 }
414  
415 0 ▶ if ( -l _ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l _) 0 0 0
416 0 unlink $path;
417 0 delete $client->{auth_cookie};
418 0 $ctx->error('verification file must not be a symbolic link');
419 0 return;
420 }
421  
422 0 ▶ unless ( -f _ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f _) 0 0 0
423 0 delete $client->{auth_cookie};
424 0 $ctx->error('verification file must be a regular file');
425 0 return;
426 }
427  
428 # Read the file contents (limit to avoid reading huge files).
429 0 my $content = '';
430 0 ▶ if ( open( my $fh, '<:raw', $path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<:raw", $path) 0 0 0
431 0 read( $fh, $content, 256 );
432 0 close($fh);
433 }
434 0 unlink($path); # clean up regardless of outcome
435 0 $content =~ s/\s+\z//; # strip trailing whitespace the client may have added
436  
437 0 my $expected = $client->{auth_cookie};
438 0 delete $client->{auth_cookie};
439 0 delete $client->{auth_time};
440  
441 0 ▶ unless ( $content eq $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content eq $expected) 0 0 0
442 0 $ctx->error('cookie mismatch: verification failed');
443 0 return;
444 }
445  
446 0 my $uid = $st[4];
447 0 return $server->_auth_success_result( $ctx, $client, $uid );
448 },
449 10 );
450 10 return;
451 } ## end sub _register_auth_builtins
452  
453 # Authenticate a connection from the kernel-verified peer credentials captured
454 # at connect time (see _read_peer_cred). No cookie, no temp file: the uid comes
455 # straight from the OS, so it cannot be forged. Called for an auth_verify with
456 # no args.path.
457 2 ▶sub _auth_verify_peercred {
Subroutine _auth_verify_peercred: called CC=3 SCAR=13.1
458 2 my ( $self, $ctx, $client ) = @_;
459  
460 2 ▶ if ( $self->{auth_method} eq 'cookie' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"auth_method"} eq "cookie") 0 2 2
461 0 $ctx->error(
462 'args.path is required: this server uses the cookie-file challenge');
463 0 return;
464 }
465  
466 2 ▶ unless ( defined $client->{peer_uid} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $client->{"peer_uid"}) 0 2 2
467 0 $ctx->error( 'kernel peer-credential authentication is unavailable here; '
468 . 'provide args.path to use the cookie-file challenge' );
469 0 return;
470 }
471  
472 # A pending cookie challenge, if any, is moot once we authenticate by
473 # peer credentials.
474 2 delete $client->{auth_cookie};
475 2 delete $client->{auth_time};
476  
477 2 return $self->_auth_success_result( $ctx, $client, $client->{peer_uid} );
478 } ## end sub _auth_verify_peercred
479  
480 # Mark a connection authenticated as $uid and build the auth_verify success
481 # response. Shared by the cookie-file and peer-credential paths so both report
482 # identically. With a permission policy in force, the user's groups are
483 # resolved and cached now (so the first gated request does not pay for it) and
484 # echoed back; without one the response is just uid and username.
485 2 ▶sub _auth_success_result {
Subroutine _auth_success_result: called CC=4 SCAR=16.6
486 2 my ( $self, $ctx, $client, $uid ) = @_;
487  
488 2 ▶ my $username = ( getpwuid($uid) )[0] // '';
Condition50%1/2
(getpwuid $uid)[0] // ""
l!l
20
MC/DC0%0/1
(getpwuid $uid)[0] // ""
(getpwuid $uid)[0]
Truth table50%1/2
A
A= (getpwuid $uid)[0]
Aresult
11
00
489  
490 2 $client->{auth_uid} = $uid;
491 2 $client->{auth_username} = $username;
492  
493 2 ▶ if ( $self->{permissions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"permissions"}) 0 2 2
494 0 my $groups = $self->_client_groups( $ctx->{wheel_id} );
495 return {
496 uid => $uid + 0,
497 username => $username,
498 0 ▶ groups => $groups ? $groups->{list} : [],
Branch: 1 branch
BranchTrueFalseTotal
$groups ? : 0 0 0
499 };
500 }
501  
502 2 return { uid => $uid + 0, username => $username };
503 } ## end sub _auth_success_result
504  
505 # Read the peer's kernel-verified credentials from a connected AF_UNIX socket,
506 # returning (uid, gid) on the platforms that support it or an empty list
507 # everywhere else (and on error). This is the identity the kernel recorded at
508 # connect(2) time -- unforgeable and needing no cookie file. Callers treat an
509 # empty return as "peercred unavailable" and fall back to the cookie challenge.
510 4 ▶sub _read_peer_cred {
Subroutine _read_peer_cred: called CC=17 SCAR=52.1
511 4 my ( $self, $socket ) = @_;
512  
513 4 my $os = $^O;
514  
515 4 ▶ if ( $os eq 'linux' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os eq "linux") 4 0 4
516 # struct ucred { pid_t pid; uid_t uid; gid_t gid; } -- three 32-bit ints.
517 4 ▶ my $packed = getsockopt( $socket, SOL_SOCKET, 17 ) # SO_PEERCRED
Branch: 1 branch
BranchTrueFalseTotal
my $packed = getsockopt($socket, 1, 17) or return 4 0 4
518 or return;
519 4 my ( $pid, $uid, $gid ) = unpack( 'l L L', $packed );
520 4 ▶ return unless defined $uid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uid 0 4 4
521 4 return ( $uid, $gid );
522 }
523  
524 0 ▶ if ( $os eq 'freebsd' || $os eq 'darwin' || $os eq 'dragonfly' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os eq "freebsd" or $os eq "darwin" or $os eq "dragonfly") 0 0 0
Condition0%0/6
$os eq "freebsd" or $os eq "darwin" or $os eq "dragonfly"
l!l&&r!l&&!r
000
$os eq "freebsd" or $os eq "darwin"
l!l&&r!l&&!r
000
MC/DC0%0/3
$os eq "freebsd" or $os eq "darwin" or $os eq "dragonfly"
$os eq "freebsd" $os eq "darwin" $os eq "dragonfly"
Truth table0%0/4
A or B or C
A= $os eq "freebsd"B= $os eq "darwin"C= $os eq "dragonfly"
ABCresult
1XX1
01X1
0011
0000
525 # struct xucred { u_int cr_version; uid_t cr_uid; short cr_ngroups;
526 # gid_t cr_groups[16]; ... }. Level is SOL_LOCAL (0),
527 # option LOCAL_PEERCRED (1). cr_groups[0] is the effective gid.
528 0 ▶ my $packed = getsockopt( $socket, 0, 1 )
Branch: 1 branch
BranchTrueFalseTotal
my $packed = getsockopt($socket, 0, 1) or return 0 0 0
529 or return;
530 0 my ( $version, $uid, $ngroups, @groups ) =
531 unpack( 'L L s x![L] L16', $packed );
532 0 ▶ return unless defined $uid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uid 0 0 0
533 0 ▶ my $gid = ( defined $ngroups && $ngroups > 0 ) ? $groups[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $ngroups && $ngroups > 0 ? : 0 0 0
Condition0%0/3
defined $ngroups and $ngroups > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ngroups and $ngroups > 0
defined $ngroups $ngroups > 0
Truth table0%0/3
A and B
A= defined $ngroupsB= $ngroups > 0
ABresult
0X0
100
111
534 0 return ( $uid, $gid );
535 }
536  
537 0 ▶ if ( $os eq 'netbsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os eq "netbsd") 0 0 0
538 # struct unpcbid { pid_t unp_pid; uid_t unp_euid; gid_t unp_egid; }.
539 # Level 0, option LOCAL_PEERCRED (1).
540 0 ▶ my $packed = getsockopt( $socket, 0, 1 )
Branch: 1 branch
BranchTrueFalseTotal
my $packed = getsockopt($socket, 0, 1) or return 0 0 0
541 or return;
542 0 my ( $pid, $uid, $gid ) = unpack( 'l L L', $packed );
543 0 ▶ return unless defined $uid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uid 0 0 0
544 0 return ( $uid, $gid );
545 }
546  
547 0 ▶ if ( $os eq 'openbsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os eq "openbsd") 0 0 0
548 # struct sockpeercred { uid_t uid; gid_t gid; pid_t pid; }, via
549 # getsockopt(SOL_SOCKET, SO_PEERCRED=0x1022).
550 0 ▶ my $packed = getsockopt( $socket, SOL_SOCKET, 0x1022 )
Branch: 1 branch
BranchTrueFalseTotal
my $packed = getsockopt($socket, 1, 4130) or return 0 0 0
551 or return;
552 0 my ( $uid, $gid, $pid ) = unpack( 'L L l', $packed );
553 0 ▶ return unless defined $uid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uid 0 0 0
554 0 return ( $uid, $gid );
555 }
556  
557 0 return; # unsupported platform -- caller falls back to the cookie challenge
558 } ## end sub _read_peer_cred
559  
560 #--- permissions -------------------------------------------------------------
561  
562 # Validate and normalise the 'permissions' spawn argument into the internal
563 # form used by _permission_verdict.
564 8 ▶sub _build_permissions {
Subroutine _build_permissions: called CC=9 SCAR=23.3
565 8 my ($perms) = @_;
566  
567 8 ▶ croak "'permissions' must be a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $perms eq "HASH" 1 7 8
568 unless ref $perms eq 'HASH';
569  
570 7 my %in = %$perms;
571 7 my %out = ( default => 'allow', commands => {} );
572  
573 7 ▶ if ( defined( my $default = delete $in{default} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $default = delete $in{"default"})) 1 6 7
574 1 ▶ croak "permissions 'default' must be 'allow' or 'deny'"
Branch: 1 branch
BranchTrueFalseTotal
unless $default eq "allow" or $default eq "deny" 1 0 1
Condition33%1/3
$default eq "allow" or $default eq "deny"
l!l&&r!l&&!r
001
MC/DC0%0/2
$default eq "allow" or $default eq "deny"
$default eq "allow" $default eq "deny"
Truth table33%1/3
A or B
A= $default eq "allow"B= $default eq "deny"
ABresult
1X1
011
000
575 unless $default eq 'allow' || $default eq 'deny';
576 0 $out{default} = $default;
577 }
578  
579 6 ▶ if ( defined( my $cmds = delete $in{commands} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cmds = delete $in{"commands"})) 5 1 6
580 5 ▶ croak "permissions 'commands' must be a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cmds eq "HASH" 0 5 5
581 unless ref $cmds eq 'HASH';
582 5 for my $name ( sort keys %$cmds ) {
583 5 $out{commands}{$name} = _normalize_permission_spec( $name, $cmds->{$name} );
584 }
585 }
586  
587 1 ▶ croak "unknown permissions key(s): " . join( ', ', sort keys %in )
Branch: 1 branch
BranchTrueFalseTotal
if %in 1 0 1
588 if %in;
589  
590 0 return \%out;
591 } ## end sub _build_permissions
592  
593 # A spec is 'allow', 'deny', or a hash of rule lists. Entries that look like
594 # a number are treated as uids/gids; everything else as user/group names.
595 # Lists are turned into lookup hashes once, here, so per-request checks are
596 # cheap.
597 5 ▶sub _normalize_permission_spec {
Subroutine _normalize_permission_spec: called CC=19 SCAR=40.9
598 5 my ( $name, $spec ) = @_;
599  
600 5 ▶ unless ( ref $spec ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $spec) 1 4 5
601 1 ▶ croak "permission for '$name' must be 'allow', 'deny', or a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec and $spec eq "allow" || $spec eq "deny" 1 0 1
Condition33%2/6
defined $spec and $spec eq "allow" || $spec eq "deny"
!ll&&!rl&&r
010
$spec eq "allow" or $spec eq "deny"
l!l&&r!l&&!r
001
MC/DC0%0/3
defined $spec and $spec eq "allow" || $spec eq "deny"
defined $spec $spec eq "allow" $spec eq "deny"
Truth table25%1/4
A and B or C
A= defined $specB= $spec eq "allow"C= $spec eq "deny"
ABCresult
0XX0
1000
11X1
1011
602 unless defined $spec && ( $spec eq 'allow' || $spec eq 'deny' );
603 0 return $spec;
604 }
605 4 ▶ croak "permission for '$name' must be 'allow', 'deny', or a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "HASH" 1 3 4
606 unless ref $spec eq 'HASH';
607  
608 3 my %in = %$spec;
609 3 my %norm;
610  
611 3 for my $key (qw(users deny_users)) {
612 5 ▶ next unless defined( my $list = delete $in{$key} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $list = delete $in{$key}) 4 1 5
613 1 ▶ croak "'$key' for '$name' must be an array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 1 0 1
614 unless ref $list eq 'ARRAY';
615 0 for my $entry (@$list) {
616 0 ▶ if ( $entry =~ /\A[0-9]+\z/ ) { $norm{$key}{uids}{$entry} = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /\A[0-9]+\z/) { } 0 0 0
617 0 else { $norm{$key}{names}{$entry} = 1 }
618 }
619 }
620  
621 2 for my $key (qw(groups deny_groups)) {
622 4 ▶ next unless defined( my $list = delete $in{$key} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $list = delete $in{$key}) 4 0 4
623 0 ▶ croak "'$key' for '$name' must be an array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 0 0 0
624 unless ref $list eq 'ARRAY';
625 0 for my $entry (@$list) {
626 0 ▶ if ( $entry =~ /\A[0-9]+\z/ ) { $norm{$key}{gids}{$entry} = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /\A[0-9]+\z/) { } 0 0 0
627 0 else { $norm{$key}{names}{$entry} = 1 }
628 }
629 }
630  
631 2 ▶ if ( defined( my $check = delete $in{check} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $check = delete $in{"check"})) 1 1 2
632 1 ▶ croak "'check' for '$name' must be a code reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $check eq "CODE" 1 0 1
633 unless ref $check eq 'CODE';
634 0 $norm{check} = $check;
635 }
636  
637 1 ▶ croak "unknown permission key(s) for '$name': " . join( ', ', sort keys %in )
Branch: 1 branch
BranchTrueFalseTotal
if %in 1 0 1
638 if %in;
639  
640 0 return \%norm;
641 } ## end sub _normalize_permission_spec
642  
643 # Full group membership for a user, via the same getpw*/getgr* calls perl
644 # provides everywhere: they route through the platform's NSS (or its local
645 # equivalent), so files, LDAP, sssd, and friends all just work. Includes the
646 # primary group from the passwd entry and every secondary group whose member
647 # list names the user.
648 0 ▶sub _resolve_user_groups {
Subroutine _resolve_user_groups: not called CC=9 SCAR=45.0
649 0 my ( $uid, $username ) = @_;
650  
651 0 my %names;
652 my %gids;
653  
654 0 eval {
655 0 my @pw = getpwuid($uid);
656 0 ▶ if (@pw) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pw) 0 0 0
657 0 my $primary_gid = $pw[3];
658 0 $gids{$primary_gid} = 1;
659 0 my $primary_name = getgrgid($primary_gid);
660 0 ▶ $names{$primary_name} = 1 if defined $primary_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $primary_name 0 0 0
661 }
662  
663 0 ▶ if ( defined $username && length $username ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $username and length $username) 0 0 0
Condition0%0/3
defined $username and length $username
!ll&&!rl&&r
000
MC/DC0%0/2
defined $username and length $username
defined $username length $username
Truth table0%0/3
A and B
A= defined $usernameB= length $username
ABresult
0X0
100
111
664 0 setgrent();
665 0 ▶ while ( my @gr = getgrent() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@gr) = getgrent) 0 0 0
666 0 my ( $group_name, $gid, $members ) = @gr[ 0, 2, 3 ];
667 next
668 unless defined $members
669 0 ▶ && grep { $_ eq $username } split ' ', $members;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $members and grep {$_ eq $username;} split(' ', $members, 0) 0 0 0
Condition0%0/3
defined $members and grep {$_ eq $username;} split(' ', $members, 0)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $members and grep {$_ eq $username;} split(' ', $members, 0)
defined $members grep {$_ eq $username;} split(' ', $members, 0)
Truth table0%0/3
A and B
A= defined $membersB= grep {$_ eq $username;} split(' ', $members, 0)
ABresult
0X0
100
111
670 0 $gids{$gid} = 1;
671 0 ▶ $names{$group_name} = 1 if defined $group_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $group_name 0 0 0
672 }
673 0 endgrent();
674 } ## end if ( defined $username && length $username)
675 };
676  
677 return {
678 0 names => \%names,
679 gids => \%gids,
680 list => [ sort keys %names ],
681 };
682 } ## end sub _resolve_user_groups
683  
684 # Group info for an authenticated connection, resolved at most once per
685 # connection and cached on the client record -- group database lookups can be
686 # expensive (LDAP, NIS, ...) and must not be paid per request.
687 0 ▶sub _client_groups {
Subroutine _client_groups: not called CC=4 SCAR=30.0
688 0 my ( $self, $id ) = @_;
689 0 ▶ my $client = $self->{clients}{$id} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $client = $self->{"clients"}{$id} or return undef 0 0 0
690 0 ▶ return undef unless defined $client->{auth_uid};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $client->{"auth_uid"} 0 0 0
691 0 ▶ $client->{auth_group_info} //= _resolve_user_groups( $client->{auth_uid}, $client->{auth_username} );
Condition0%0/3
$client->{"auth_group_info"} //= _resolve_user_groups($client->{"auth_uid"}, $client->{"auth_username"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$client->{"auth_group_info"} //= _resolve_user_groups($client->{"auth_uid"}, $client->{"auth_username"})
$client->{"auth_group_info"} _resolve_user_groups($client->{"auth_uid"}, $client->{"auth_username"})
Truth table0%0/3
A //= B
A= $client->{"auth_group_info"}B= _resolve_user_groups($client->{"auth_uid"}, $client->{"auth_username"})
ABresult
1X1
011
000
692 0 return $client->{auth_group_info};
693 }
694  
695 # Decide whether the client behind wheel $id may run $cmd_name under the
696 # configured policy. Returns 1 to allow, or (0, $code, $message) to deny.
697 # Only called when a policy is configured.
698 0 ▶sub _permission_verdict {
Subroutine _permission_verdict: not called CC=33 SCAR=70.2
699 0 my ( $self, $id, $cmd_name, $ctx ) = @_;
700  
701 # The handshake itself must always be reachable, or nobody could ever
702 # gain the identity the policy is written in terms of.
703 0 ▶ return 1 if $cmd_name eq 'auth_start' || $cmd_name eq 'auth_verify';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd_name eq "auth_start" or $cmd_name eq "auth_verify" 0 0 0
Condition0%0/3
$cmd_name eq "auth_start" or $cmd_name eq "auth_verify"
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmd_name eq "auth_start" or $cmd_name eq "auth_verify"
$cmd_name eq "auth_start" $cmd_name eq "auth_verify"
Truth table0%0/3
A or B
A= $cmd_name eq "auth_start"B= $cmd_name eq "auth_verify"
ABresult
1X1
011
000
704  
705 # A command's own rule wins; the special '%DEFAULT%' entry covers any
706 # command (known or not) without one; the 'default' string is last.
707 0 my $perms = $self->{permissions};
708 my $spec =
709 $perms->{commands}{$cmd_name}
710 // $perms->{commands}{'%DEFAULT%'}
711 0 ▶ // $perms->{default};
Condition0%0/6
($perms->{"commands"}{$cmd_name} // $perms->{"commands"}{"%DEFAULT%"}) // $perms->{"default"}
l!l&&r!l&&!r
000
$perms->{"commands"}{$cmd_name} // $perms->{"commands"}{"%DEFAULT%"}
l!l&&r!l&&!r
000
MC/DC0%0/3
($perms->{"commands"}{$cmd_name} // $perms->{"commands"}{"%DEFAULT%"}) // $perms->{"default"}
$perms->{"commands"}{$cmd_name} $perms->{"commands"}{"%DEFAULT%"} $perms->{"default"}
Truth table0%0/4
A // B // C
A= $perms->{"commands"}{$cmd_name}B= $perms->{"commands"}{"%DEFAULT%"}C= $perms->{"default"}
ABCresult
1XX1
01X1
0011
0000
712  
713 0 ▶ unless ( ref $spec ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $spec) 0 0 0
714 0 ▶ return 1 if $spec eq 'allow';
Branch: 1 branch
BranchTrueFalseTotal
if $spec eq "allow" 0 0 0
715 0 return ( 0, 'permission_denied', "permission denied: $cmd_name" );
716 }
717  
718 0 ▶ my $client = $self->{clients}{$id}
Branch: 1 branch
BranchTrueFalseTotal
my $client = $self->{"clients"}{$id} or return 0, "permission_denied", "permission denied: $cmd_name" 0 0 0
719 or return ( 0, 'permission_denied', "permission denied: $cmd_name" );
720  
721 # A rule written in terms of users or groups implies the connection must
722 # be authenticated, even when auth_required is off globally.
723 0 ▶ unless ( defined $client->{auth_uid} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $client->{"auth_uid"}) 0 0 0
724 0 return ( 0, 'auth_required', 'authentication required: call auth_start then auth_verify first' );
725 }
726  
727 0 my $uid = $client->{auth_uid};
728 0 ▶ my $username = $client->{auth_username} // '';
Condition0%0/2
$client->{"auth_username"} // ""
l!l
00
MC/DC0%0/1
$client->{"auth_username"} // ""
$client->{"auth_username"}
Truth table0%0/2
A
A= $client->{"auth_username"}
Aresult
11
00
729 0 my $groups = $self->_client_groups($id);
730 0 my $denial = "permission denied: user '$username' may not run '$cmd_name'";
731  
732 # Explicit denies win over everything.
733 0 ▶ if ( my $deny = $spec->{deny_users} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $deny = $spec->{"deny_users"}) 0 0 0
734 return ( 0, 'permission_denied', $denial )
735 0 ▶ if $deny->{names}{$username} || $deny->{uids}{$uid};
Branch: 1 branch
BranchTrueFalseTotal
if $deny->{"names"}{$username} or $deny->{"uids"}{$uid} 0 0 0
Condition0%0/3
$deny->{"names"}{$username} or $deny->{"uids"}{$uid}
l!l&&r!l&&!r
000
MC/DC0%0/2
$deny->{"names"}{$username} or $deny->{"uids"}{$uid}
$deny->{"names"}{$username} $deny->{"uids"}{$uid}
Truth table0%0/3
A or B
A= $deny->{"names"}{$username}B= $deny->{"uids"}{$uid}
ABresult
1X1
011
000
736 }
737 0 ▶ if ( my $deny = $spec->{deny_groups} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $deny = $spec->{"deny_groups"}) 0 0 0
738 0 for my $group_name ( keys %{ $groups->{names} } ) {
739 0 ▶ return ( 0, 'permission_denied', $denial ) if $deny->{names}{$group_name};
Branch: 1 branch
BranchTrueFalseTotal
if $deny->{"names"}{$group_name} 0 0 0
740 }
741 0 for my $gid ( keys %{ $groups->{gids} } ) {
742 0 ▶ return ( 0, 'permission_denied', $denial ) if $deny->{gids}{$gid};
Branch: 1 branch
BranchTrueFalseTotal
if $deny->{"gids"}{$gid} 0 0 0
743 }
744 }
745  
746 # With no allow criteria at all (a deny-list-only spec, or an empty
747 # hash), whoever survived the denies falls through to the default.
748 0 ▶ unless ( $spec->{users} || $spec->{groups} || $spec->{check} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec->{"users"} or $spec->{"groups"} or $spec->{"check"}) 0 0 0
Condition0%0/6
$spec->{"users"} or $spec->{"groups"} or $spec->{"check"}
l!l&&r!l&&!r
000
$spec->{"users"} or $spec->{"groups"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$spec->{"users"} or $spec->{"groups"} or $spec->{"check"}
$spec->{"users"} $spec->{"groups"} $spec->{"check"}
Truth table0%0/4
A or B or C
A= $spec->{"users"}B= $spec->{"groups"}C= $spec->{"check"}
ABCresult
1XX1
01X1
0011
0000
749 0 ▶ return 1 if $perms->{default} eq 'allow';
Branch: 1 branch
BranchTrueFalseTotal
if $perms->{"default"} eq "allow" 0 0 0
750 0 return ( 0, 'permission_denied', $denial );
751 }
752  
753 # Any matching allow criterion is enough.
754 0 ▶ if ( my $allow = $spec->{users} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $allow = $spec->{"users"}) 0 0 0
755 0 ▶ return 1 if $allow->{names}{$username} || $allow->{uids}{$uid};
Branch: 1 branch
BranchTrueFalseTotal
if $allow->{"names"}{$username} or $allow->{"uids"}{$uid} 0 0 0
Condition0%0/3
$allow->{"names"}{$username} or $allow->{"uids"}{$uid}
l!l&&r!l&&!r
000
MC/DC0%0/2
$allow->{"names"}{$username} or $allow->{"uids"}{$uid}
$allow->{"names"}{$username} $allow->{"uids"}{$uid}
Truth table0%0/3
A or B
A= $allow->{"names"}{$username}B= $allow->{"uids"}{$uid}
ABresult
1X1
011
000
756 }
757 0 ▶ if ( my $allow = $spec->{groups} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $allow = $spec->{"groups"}) 0 0 0
758 0 for my $group_name ( keys %{ $groups->{names} } ) {
759 0 ▶ return 1 if $allow->{names}{$group_name};
Branch: 1 branch
BranchTrueFalseTotal
if $allow->{"names"}{$group_name} 0 0 0
760 }
761 0 for my $gid ( keys %{ $groups->{gids} } ) {
762 0 ▶ return 1 if $allow->{gids}{$gid};
Branch: 1 branch
BranchTrueFalseTotal
if $allow->{"gids"}{$gid} 0 0 0
763 }
764 }
765 0 ▶ if ( my $check = $spec->{check} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $check = $spec->{"check"}) 0 0 0
766 0 my $ok = eval { $check->( $self, $ctx, $cmd_name ) };
767 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif ($ok) { } 0 0 0
768 0 carp "permission check for '$cmd_name' died: $@"; # fail closed
769 } elsif ($ok) {
770 0 return 1;
771 }
772 }
773  
774 0 return ( 0, 'permission_denied', $denial );
775 } ## end sub _permission_verdict
776  
777 2 ▶sub _random_cookie {
Subroutine _random_cookie: called CC=3 SCAR=12.0
778 2 my $bytes = '';
779 2 ▶ if ( open( my $fh, '<:raw', '/dev/urandom' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<:raw", "/dev/urandom") 2 0 2
780 2 read( $fh, $bytes, 16 );
781 2 close($fh);
782 }
783 # Fallback: should never happen on a Unix system.
784 2 ▶ unless ( length($bytes) == 16 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $bytes == 16) 0 2 2
785 0 $bytes = pack( 'N4', map { int( rand(2**32) ) } 1 .. 4 );
786 }
787 2 return unpack( 'H*', $bytes ); # 32 lowercase hex chars
788 }
789  
790 =head2 shutdown
791  
792 $server->shutdown;
793  
794 Stop accepting connections, close all clients, remove the socket file, and let
795 the session end.
796  
797 =cut
798  
799 2 ▶sub shutdown {
Subroutine shutdown: called CC=1 SCAR=0.0
Pod documented
800 2 my ($self) = @_;
801 2 $poe_kernel->post( $self->{alias}, 'shutdown' );
802 2 return;
803 }
804  
805 #--- socket setup ----------------------------------------------------------
806  
807 2 ▶sub _prepare_socket_path {
Subroutine _prepare_socket_path: called CC=6 SCAR=32.2
808 2 my ($self) = @_;
809 2 my $path = $self->{socket_path};
810  
811 2 ▶ return unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 2 0 2
812  
813 0 ▶ croak "socket_path '$path' exists and is not a socket"
Branch: 1 branch
BranchTrueFalseTotal
unless -S $path 0 0 0
814 unless -S $path;
815  
816 # If something is actively listening there, refuse rather than clobber it.
817 0 my $probe = IO::Socket::UNIX->new( Type => SOCK_STREAM, Peer => $path );
818 0 ▶ if ($probe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($probe) 0 0 0
819 0 close $probe;
820 0 croak "Another server is already listening on '$path'";
821 }
822  
823 croak "Stale socket '$path' present but unlink_existing is disabled"
824 0 ▶ unless $self->{unlink_existing};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"unlink_existing"} 0 0 0
825  
826 0 ▶ unlink $path
Branch: 1 branch
BranchTrueFalseTotal
unlink $path or croak("Could not remove stale socket '${path}': $!") 0 0 0
827 or croak "Could not remove stale socket '$path': $!";
828 0 return;
829 } ## end sub _prepare_socket_path
830  
831 #--- POE: session lifecycle ------------------------------------------------
832  
833 2 ▶sub _poe_start {
Subroutine _poe_start: called CC=3 SCAR=12.7
834 2 my ( $self, $kernel ) = @_[ OBJECT, KERNEL ];
835  
836 2 $kernel->alias_set( $self->{alias} );
837  
838 $self->{listener} = POE::Wheel::SocketFactory->new(
839 SocketDomain => PF_UNIX,
840 SocketType => SOCK_STREAM,
841 BindAddress => $self->{socket_path},
842 2 SuccessEvent => 'got_connection',
843 FailureEvent => 'listen_error',
844 );
845  
846 2 ▶ if ( defined $self->{socket_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"socket_mode"}) 0 2 2
847 chmod $self->{socket_mode}, $self->{socket_path}
848 0 ▶ or carp "chmod on '$self->{socket_path}' failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
chmod $self->{"socket_mode"}, $self->{"socket_path"} or carp(qq[chmod on '$self->{"socket_path"}' failed: $!]) 0 0 0
849 }
850 2 return;
851 } ## end sub _poe_start
852  
853 2 ▶sub _poe_stop {
Subroutine _poe_stop: called CC=1 SCAR=0.0
854 2 my ($self) = $_[OBJECT];
855 2 $self->_remove_socket_file;
856 2 return;
857 }
858  
859 2 ▶sub _poe_shutdown {
Subroutine _poe_shutdown: called CC=1 SCAR=0.0
860 2 my ( $self, $kernel ) = @_[ OBJECT, KERNEL ];
861 2 delete $self->{listener}; # stop accepting new connections
862 2 %{ $self->{clients} } = (); # drop wheels -> close client sockets
863 2 $kernel->alias_remove( $self->{alias} );
864 2 $self->_remove_socket_file;
865 2 return;
866 }
867  
868 0 ▶sub _poe_register_command {
Subroutine _poe_register_command: not called CC=2 SCAR=17.9
869 0 my ( $self, $name, $code ) = @_[ OBJECT, ARG0, ARG1 ];
870 0 eval { $self->register( $name => $code ) };
871 0 ▶ carp "register_command failed: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
872 0 return;
873 }
874  
875 4 ▶sub _remove_socket_file {
Subroutine _remove_socket_file: called CC=3 SCAR=11.0
876 4 my ($self) = @_;
877 4 my $path = $self->{socket_path};
878 4 ▶ unlink $path if defined $path && -S $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path and -S $path 2 2 4
Condition66%2/3
defined $path and -S $path
!ll&&!rl&&r
022
MC/DC50%1/2
defined $path and -S $path
defined $path -S $path
Truth table66%2/3
A and B
A= defined $pathB= -S $path
ABresult
0X0
100
111
879 4 return;
880 }
881  
882 #--- POE: listener events --------------------------------------------------
883  
884 0 ▶sub _poe_listen_error {
Subroutine _poe_listen_error: not called CC=2 SCAR=17.9
885 0 my ( $self, $op, $errnum, $errstr ) = @_[ OBJECT, ARG0, ARG1, ARG2 ];
886 0 carp "listen error during $op: $errstr ($errnum)";
887 0 ▶ $self->{on_error}->( "listen:$op", $errnum, $errstr ) if $self->{on_error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_error"} 0 0 0
888 0 delete $self->{listener};
889 0 return;
890 }
891  
892 4 ▶sub _poe_got_connection {
Subroutine _poe_got_connection: called CC=1 SCAR=0.0
893 4 my ( $self, $socket ) = @_[ OBJECT, ARG0 ];
894  
895 # Capture the kernel-verified peer credentials while we still hold the raw
896 # handle, before it is wrapped in a wheel. Empty on platforms without
897 # support -- there the cookie challenge remains the only path.
898 4 my ( $peer_uid, $peer_gid ) = $self->_read_peer_cred($socket);
899  
900 4 my $wheel = POE::Wheel::ReadWrite->new(
901 Handle => $socket,
902 Driver => POE::Driver::SysRW->new,
903 Filter => POE::Filter::Line->new( Literal => "\n" ),
904 InputEvent => 'client_input',
905 ErrorEvent => 'client_error',
906 FlushedEvent => 'client_flushed',
907 );
908  
909 4 $self->{clients}{ $wheel->ID } = {
910 wheel => $wheel,
911 close_after_flush => 0,
912 auth_cookie => undef,
913 auth_uid => undef,
914 auth_username => undef,
915 auth_group_info => undef, # filled once by _client_groups
916 peer_uid => $peer_uid, # kernel-verified, undef where unsupported
917 peer_gid => $peer_gid,
918 };
919 4 return;
920 } ## end sub _poe_got_connection
921  
922 #--- POE: per-client events ------------------------------------------------
923  
924 15 ▶sub _poe_client_input {
Subroutine _poe_client_input: called CC=20 SCAR=37.7
925 15 my ( $self, $line, $id ) = @_[ OBJECT, ARG0, ARG1 ];
926  
927 15 ▶ return unless $self->{clients}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"clients"}{$id} 0 15 15
928 15 ▶ return unless defined $line && $line =~ /\S/; # ignore blank keepalives
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and $line =~ /\S/ 0 15 15
Condition33%1/3
defined $line and $line =~ /\S/
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $line and $line =~ /\S/
defined $line $line =~ /\S/
Truth table33%1/3
A and B
A= defined $lineB= $line =~ /\S/
ABresult
0X0
100
111
929  
930 15 my $request;
931 15 ▶ unless ( eval { $request = $self->{json}->decode($line); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $request = $self->{"json"}->decode($line); 1 } }) 0 15 15
932 0 $self->_send(
933 $id,
934 {
935 status => 'error',
936 error => 'invalid JSON: ' . _clean_err($@),
937 }
938 );
939 0 return;
940 } ## end unless ( eval { $request = $self->{json}->decode...})
941  
942 15 ▶ unless ( ref $request eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $request eq "HASH") 0 15 15
943 0 $self->_send(
944 $id,
945 {
946 status => 'error',
947 error => 'request must be a JSON object',
948 }
949 );
950 0 return;
951 } ## end unless ( ref $request eq 'HASH' )
952  
953 15 my $req_id = $request->{id};
954 15 ▶ my $cmd_name = $request->{command} // $request->{cmd};
Condition33%1/3
$request->{"command"} // $request->{"cmd"}
l!l&&r!l&&!r
1500
MC/DC0%0/2
$request->{"command"} // $request->{"cmd"}
$request->{"command"} $request->{"cmd"}
Truth table33%1/3
A // B
A= $request->{"command"}B= $request->{"cmd"}
ABresult
1X1
011
000
955  
956 15 my $ctx = POE::Component::Server::JSONUnix::Context->_new(
957 server => $self,
958 wheel_id => $id,
959 req_id => $req_id,
960 command => $cmd_name,
961 request => $request,
962 );
963  
964 15 ▶ unless ( defined $cmd_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cmd_name) 0 15 15
965 0 $ctx->error("missing 'command' field");
966 0 return;
967 }
968  
969 # Enforce auth_required: only auth_start and auth_verify bypass the gate.
970 15 ▶ if ( $self->{auth_required} && !defined( $self->{clients}{$id}{auth_uid} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"auth_required"} and not defined $self->{"clients"}{$id}{"auth_uid"}) 5 10 15
Condition100%3/3
$self->{"auth_required"} and not defined $self->{"clients"}{$id}{"auth_uid"}
!ll&&!rl&&r
915
MC/DC100%2/2
$self->{"auth_required"} and not defined $self->{"clients"}{$id}{"auth_uid"}
$self->{"auth_required"} not defined $self->{"clients"}{$id}{"auth_uid"}
Truth table100%3/3
A and not(B)
A= $self->{"auth_required"}B= not defined $self->{"clients"}{$id}{"auth_uid"}
ABresult
0X0
100
111
971 5 ▶ unless ( $cmd_name eq 'auth_start' || $cmd_name eq 'auth_verify' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmd_name eq "auth_start" or $cmd_name eq "auth_verify") 1 4 5
Condition100%3/3
$cmd_name eq "auth_start" or $cmd_name eq "auth_verify"
l!l&&r!l&&!r
221
MC/DC100%2/2
$cmd_name eq "auth_start" or $cmd_name eq "auth_verify"
$cmd_name eq "auth_start" $cmd_name eq "auth_verify"
Truth table100%3/3
A or B
A= $cmd_name eq "auth_start"B= $cmd_name eq "auth_verify"
ABresult
1X1
011
000
972 1 $ctx->error('authentication required: call auth_start then auth_verify first');
973 1 return;
974 }
975 }
976  
977 # Enforce the optional permission policy. Runs before the unknown-command
978 # check so a default-deny server does not reveal which commands exist.
979 14 ▶ if ( $self->{permissions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"permissions"}) 0 14 14
980 0 my ( $allowed, $code, $denial ) = $self->_permission_verdict( $id, $cmd_name, $ctx );
981 0 ▶ unless ($allowed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($allowed) 0 0 0
982 0 $ctx->error( $denial, code => $code );
983 0 return;
984 }
985 }
986  
987 14 my $handler = $self->{commands}{$cmd_name};
988 14 ▶ unless ($handler) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handler) 1 13 14
989 1 $ctx->error("unknown command: $cmd_name");
990 1 return;
991 }
992  
993 13 my @ret = eval { $handler->( $self, $request, $ctx ) };
994 13 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 13 13
995 0 ▶ unless ( $ctx->responded ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctx->responded) 0 0 0
996 0 ▶ if ( ref $err eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $err eq 'HASH') { } 0 0 0
997 0 $ctx->respond( { status => 'error', %$err } );
998 } else {
999 0 $ctx->error( _clean_err($err) );
1000 }
1001 }
1002 0 return;
1003 } ## end if ( my $err = $@ )
1004  
1005 13 ▶ return if $ctx->responded; # handler already answered (any path)
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->responded 0 13 13
1006 13 ▶ return unless defined $ret[0]; # undef return => async; answers later
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret[0] 2 11 13
1007 11 $ctx->respond_result( $ret[0] ); # sync return => wrap as {ok, result}
1008 11 return;
1009 } ## end sub _poe_client_input
1010  
1011 2 ▶sub _poe_client_error {
Subroutine _poe_client_error: called CC=4 SCAR=19.4
1012 2 my ( $self, $op, $errnum, $errstr, $id ) = @_[ OBJECT, ARG0, ARG1, ARG2, ARG3 ];
1013  
1014 # operation 'read' with errnum 0 is a normal EOF (client hung up).
1015 2 ▶ if ( $self->{on_error} && !( $op eq 'read' && $errnum == 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"on_error"} and not $op eq "read" && $errnum == 0) 0 2 2
Condition16%1/6
$self->{"on_error"} and not $op eq "read" && $errnum == 0
!ll&&!rl&&r
200
$op eq 'read' && $errnum == 0
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"on_error"} and not $op eq "read" && $errnum == 0
$self->{"on_error"} $op eq 'read' $errnum == 0
Truth table25%1/4
A and not(B && C)
A= $self->{"on_error"}B= $op eq 'read'C= $errnum == 0
ABCresult
0XX0
1110
10X1
1101
1016 0 eval { $self->{on_error}->( $op, $errnum, $errstr, $id ) };
1017 }
1018 2 $self->_close_client($id);
1019 2 return;
1020 } ## end sub _poe_client_error
1021  
1022 11 ▶sub _poe_client_flushed {
Subroutine _poe_client_flushed: called CC=3 SCAR=11.4
1023 11 my ( $self, $id ) = @_[ OBJECT, ARG0 ];
1024 11 ▶ my $client = $self->{clients}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $client = $self->{"clients"}{$id} or return 11 0 11
1025 11 ▶ $self->_close_client($id) if $client->{close_after_flush};
Branch: 1 branch
BranchTrueFalseTotal
if $client->{"close_after_flush"} 0 11 11
1026 11 return;
1027 }
1028  
1029 #--- sending / closing -----------------------------------------------------
1030  
1031 14 ▶sub _send {
Subroutine _send: called CC=4 SCAR=14.6
1032 14 my ( $self, $id, $data ) = @_;
1033 14 ▶ my $client = $self->{clients}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $client = $self->{"clients"}{$id} or return 14 0 14
1034 14 ▶ my $wheel = $client->{wheel} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $wheel = $client->{"wheel"} or return 14 0 14
1035  
1036 14 my $json = eval { $self->{json}->encode($data) };
1037 14 ▶ unless ( defined $json ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $json) 0 14 14
1038 $json = $self->{json}->encode(
1039 {
1040 0 status => 'error',
1041 error => 'internal error: response could not be serialised',
1042 }
1043 );
1044 }
1045 14 $wheel->put($json); # Filter::Line appends the trailing newline
1046 14 return;
1047 } ## end sub _send
1048  
1049 2 ▶sub _close_client {
Subroutine _close_client: called CC=1 SCAR=0.0
1050 2 my ( $self, $id ) = @_;
1051 2 delete $self->{clients}{$id}; # destroying the wheel closes the socket
1052 2 return;
1053 }
1054  
1055 0 ▶sub _close_client_after_flush {
Subroutine _close_client_after_flush: not called CC=4 SCAR=30.0
1056 0 my ( $self, $id ) = @_;
1057 0 ▶ my $client = $self->{clients}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $client = $self->{"clients"}{$id} or return 0 0 0
1058 0 my $wheel = $client->{wheel};
1059 0 ▶ if ( $wheel && $wheel->get_driver_out_octets ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wheel and $wheel->get_driver_out_octets) { } 0 0 0
Condition0%0/3
$wheel and $wheel->get_driver_out_octets
!ll&&!rl&&r
000
MC/DC0%0/2
$wheel and $wheel->get_driver_out_octets
$wheel $wheel->get_driver_out_octets
Truth table0%0/3
A and B
A= $wheelB= $wheel->get_driver_out_octets
ABresult
0X0
100
111
1060 0 $client->{close_after_flush} = 1;
1061 } else {
1062 0 $self->_close_client($id);
1063 }
1064 0 return;
1065 } ## end sub _close_client_after_flush
1066  
1067 0 ▶sub _clean_err {
Subroutine _clean_err: not called CC=1 SCAR=6.9
1068 0 my ($msg) = @_;
1069 0 $msg = "$msg";
1070 0 $msg =~ s/\s+at \S+ line \d+\.?.*//s; # strip "at file line N ..."
1071 0 $msg =~ s/\s+\z//;
1072 0 return $msg;
1073 }
1074  
1075 #===========================================================================
1076 package POE::Component::Server::JSONUnix::Context;
1077 #===========================================================================
1078 # Handed to every command handler. A handler can answer synchronously by
1079 # returning a value, or asynchronously by stashing $ctx and calling one of
1080 # these methods later (e.g. after a timer fires or a backend request returns).
1081  
1082 10 our $VERSION = '0.01';
1083  
1084 25 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
1085 25 my ( $class, %a ) = @_;
1086 return bless {
1087 server => $a{server},
1088 wheel_id => $a{wheel_id},
1089 req_id => $a{req_id},
1090 command => $a{command},
1091 request => $a{request},
1092 25 responded => 0,
1093 }, $class;
1094 } ## end sub _new
1095  
1096 1 ▶sub request { $_[0]{request} }
Subroutine request: called CC=1 SCAR=0.0
1097 1 ▶sub command { $_[0]{command} }
Subroutine command: called CC=1 SCAR=0.0
1098 1 ▶sub id { $_[0]{req_id} }
Subroutine id: called CC=1 SCAR=0.0
1099 14 ▶sub responded { $_[0]{responded} }
Subroutine responded: called CC=1 SCAR=0.0
1100  
1101 # Auth accessors — defined after a successful auth_verify.
1102 5 ▶sub authenticated {
Subroutine authenticated: called CC=1 SCAR=0.0
1103 5 my ($self) = @_;
1104 5 return defined $self->{server}{clients}{ $self->{wheel_id} }{auth_uid};
1105 }
1106  
1107 6 ▶sub uid {
Subroutine uid: called CC=1 SCAR=0.0
1108 6 my ($self) = @_;
1109 6 return $self->{server}{clients}{ $self->{wheel_id} }{auth_uid};
1110 }
1111  
1112 4 ▶sub username {
Subroutine username: called CC=1 SCAR=0.0
1113 4 my ($self) = @_;
1114 4 return $self->{server}{clients}{ $self->{wheel_id} }{auth_username};
1115 }
1116  
1117 # Kernel-verified peer credentials, available on supported platforms from the
1118 # moment the connection is accepted -- independent of any auth handshake.
1119 # undef where the platform does not provide them.
1120 0 ▶sub peer_uid {
Subroutine peer_uid: not called CC=1 SCAR=6.9
1121 0 my ($self) = @_;
1122 0 return $self->{server}{clients}{ $self->{wheel_id} }{peer_uid};
1123 }
1124  
1125 0 ▶sub peer_gid {
Subroutine peer_gid: not called CC=1 SCAR=6.9
1126 0 my ($self) = @_;
1127 0 return $self->{server}{clients}{ $self->{wheel_id} }{peer_gid};
1128 }
1129  
1130 # Group names of the authenticated user (empty before authentication).
1131 # Resolved via NSS at most once per connection, then served from the cache.
1132 0 ▶sub groups {
Subroutine groups: not called CC=2 SCAR=17.9
1133 0 my ($self) = @_;
1134 0 my $groups = $self->{server}->_client_groups( $self->{wheel_id} );
1135 0 ▶ return $groups ? [ @{ $groups->{list} } ] : [];
Branch: 1 branch
BranchTrueFalseTotal
$groups ? : 0 0 0
1136 }
1137  
1138 # Membership test by group name or numeric gid.
1139 0 ▶sub in_group {
Subroutine in_group: not called CC=5 SCAR=34.0
1140 0 my ( $self, $group ) = @_;
1141 0 ▶ return 0 unless defined $group;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $group 0 0 0
1142 my $groups = $self->{server}->_client_groups( $self->{wheel_id} )
1143 0 ▶ or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $groups = $self->{"server"}->_client_groups($self->{"wheel_id"}) or return 0 0 0 0
1144 0 ▶ my $set = $group =~ /\A[0-9]+\z/ ? $groups->{gids} : $groups->{names};
Branch: 1 branch
BranchTrueFalseTotal
$group =~ /\A[0-9]+\z/ ? : 0 0 0
1145 0 ▶ return $set->{$group} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$set->{$group} ? : 0 0 0
1146 }
1147  
1148 # Would this connection be allowed to run $command_name right now? Always
1149 # true when no permission policy is configured.
1150 0 ▶sub may {
Subroutine may: not called CC=3 SCAR=24.8
1151 0 my ( $self, $command_name ) = @_;
1152 0 ▶ return 1 unless $self->{server}{permissions};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"server"}{"permissions"} 0 0 0
1153 0 my ($allowed) = $self->{server}->_permission_verdict( $self->{wheel_id}, $command_name, $self );
1154 0 ▶ return $allowed ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$allowed ? : 0 0 0
1155 }
1156  
1157 # Send a full response envelope. status defaults to 'ok'; the request id, if
1158 # present, is echoed back automatically. Only the first call has any effect.
1159 19 ▶sub respond {
Subroutine respond: called CC=5 SCAR=16.1
1160 19 my ( $self, $envelope ) = @_;
1161 19 ▶ return if $self->{responded};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"responded"} 1 18 19
1162 18 $self->{responded} = 1;
1163  
1164 18 my %out = %$envelope;
1165 18 ▶ $out{status} //= 'ok';
Condition100%2/2
$out{"status"} //= "ok"
l!l
162
MC/DC100%1/1
$out{"status"} //= "ok"
$out{"status"}
Truth table100%2/2
A
A= $out{"status"}
Aresult
11
00
1166 $out{id} = $self->{req_id}
1167 18 ▶ if defined $self->{req_id} && !exists $out{id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"req_id"} and not exists $out{"id"} 17 1 18
Condition66%2/3
defined $self->{"req_id"} and not exists $out{"id"}
!ll&&!rl&&r
1017
MC/DC50%1/2
defined $self->{"req_id"} and not exists $out{"id"}
defined $self->{"req_id"} not exists $out{"id"}
Truth table66%2/3
A and not(B)
A= defined $self->{"req_id"}B= not exists $out{"id"}
ABresult
0X0
100
111
1168  
1169 18 $self->{server}->_send( $self->{wheel_id}, \%out );
1170 18 return;
1171 } ## end sub respond
1172  
1173 # Convenience: respond with {status:ok, result:<data>}.
1174 13 ▶sub respond_result {
Subroutine respond_result: called CC=1 SCAR=0.0
1175 13 my ( $self, $result ) = @_;
1176 13 return $self->respond( { status => 'ok', result => $result } );
1177 }
1178  
1179 # Convenience: respond with {status:error, error:<message>, ...extra}.
1180 3 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
1181 3 my ( $self, $message, %extra ) = @_;
1182 3 return $self->respond( { status => 'error', error => $message, %extra } );
1183 }
1184  
1185 # Close this client's connection once any queued output has been flushed.
1186 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
1187 0 my ($self) = @_;
1188 0 $self->{server}->_close_client_after_flush( $self->{wheel_id} );
1189 0 return;
1190 }
1191  
1192 10 1;
1193  
1194 __END__
↑ Summary JSONUnix/BlockingClient.pm »