TER1 (Statement): 99.12%
TER2 (Branch): 89.77%
TER3 (LCSAJ): 100.0% (7/7)
Approximate LCSAJ segments: 89
โ Covered โ this LCSAJ path was executed during testing.
โ Not covered โ this LCSAJ path was never executed. These are the paths to focus on.
Multiple dots on a line indicate that multiple control-flow paths begin at that line. Hovering over any dot shows:
start โ end โ jump
Uncovered paths show [NOT COVERED] in the tooltip.
1: package Class::Shared; 2: 3: use strict; 4: use warnings; 5: 6: use File::Basename (); 7: use File::Spec (); 8: 9: # version->declare here alone: this is VERSION_FROM, and with it MakeMaker 10: # writes 0.001.0 into META; a plain dotted string becomes v0.001.0 there. 11: use version; our $VERSION = version->declare('0.001.0'); 12: 13: # Bumped whenever the wire protocol changes in an incompatible way. 2 added 14: # the HMAC-SHA-256 digest every frame carries when a token is in use. 15: our $PROTOCOL_VERSION = 2; 16: 17: # Longest unix(7) path we are prepared to generate. The kernel limit is 18: # 107 characters plus the terminator; leaving headroom keeps us portable. 19: our $MAX_SOCKET_PATH = 100; 20: 21: # How often the loops that wait for a socket or a lock look again, in seconds. 22: # One value, so every wait in the distribution polls at the same rate. 23: our $POLL_INTERVAL = 0.02; 24: 25: # Why a unix socket takes neither a token nor TLS. It is quoted by the backend, 26: # the frontend and the command line, so they cannot word it differently. 27: our $UNIX_TRUST_REASON = 'its mode 0600 file already decides who may connect'; 28: 29: # The protocol versions a TLS connection may use, in IO::Socket::SSL's own 30: # notation: TLS 1.2 and later. SSL 2 and 3 and TLS 1.0 and 1.1 are broken or 31: # deprecated (RFC 8996), and left to its defaults the library would still 32: # offer them to a peer that asked. Used by both ends so that they agree. 33: our $TLS_VERSIONS = 'SSLv23:!SSLv3:!SSLv2:!TLSv1:!TLSv1_1'; 34: 35: # Parts of a class name are joined by "::". The characters of a part never 36: # include ":", so a part can only end where "::" or the end begins, and there 37: # is only one way to match any name. 38: # 39: # The name is checked in two flat steps, not by one pattern that repeats a 40: # group once per part: perl 5.26 stops a repeated group at 32766 repeats 41: # ("Complex regular subexpression recursion limit"), so a long but valid 42: # name would die there. Step one matches only the allowed characters, and its 43: # capture is what is returned, so it is untainted. Step two checks each part. 44: # The classes are written out, not \w, so that no non-ASCII letter is accepted. 45: my $CLASS_CHARS_RE = qr/ \A ( [A-Za-z0-9_:]++ ) \z /x; 46: my $CLASS_PART_RE = qr/ \A [A-Za-z_] [A-Za-z0-9_]*+ \z /x; 47: 48: =head1 NAME 49: 50: Class::Shared - Keep one instance of a class and let other processes call it 51: 52: =head1 SYNOPSIS 53: 54: The usual way to use this distribution is through L<Class::Shared::Frontend>. 55: Each example below is a different real-world pattern. 56: 57: # 1. The simplest case: share one class between many processes. 58: use Class::Shared::Frontend; 59: 60: my $simple = Class::Shared::Frontend->new('Class::Simple'); 61: 62: $simple->foo('hello'); # runs in the shared backend 63: print $simple->foo(), "\n"; # prints "hello" 64: 65: # 2. Give the class its constructor arguments. They are used once, when 66: # the object is made, and must be plain data (no code references). 67: my $logger = Class::Shared::Frontend->new('My::Logger', 68: ctor => [ level => 'debug', file => '/var/log/app.log' ], 69: ); 70: 71: my @all = $logger->lines(); # list context is kept 72: my $n = $logger->lines(); # scalar context is kept too 73: 74: # 3. Use a backend on another machine, over TLS with client certificates. 75: my $remote = Class::Shared::Frontend->new('Class::Simple', 76: host => 'classhost.example.com', 77: port => 7000, 78: tls_ca => 'ca.pem', 79: tls_cert => 'client.pem', 80: tls_key => 'client-key.pem', 81: token_file => '/etc/class-shared/token', 82: ); 83: 84: # 4. Look after the backend yourself, from the shell. 85: # class-shared start # start it in the background 86: # class-shared status # ask what it is doing 87: # class-shared stop # ask it to stop 88: 89: # 5. Use the helper functions to write your own client. 90: use Class::Shared (); 91: 92: my $dir = Class::Shared::default_dir(); 93: my ($ok, $why) = Class::Shared::ensure_dir($dir); 94: die "cannot use $dir: $why" unless $ok; 95: 96: my $socket = Class::Shared::socket_path($dir); 97: 98: =head1 DESCRIPTION 99: 100: Perl has no equivalent of a shared library for classes: every process loads 101: its own copy of F<Class/Simple.pm>, runs its own constructors and keeps its 102: own state. For a large class used by many short lived processes that is a lot 103: of memory and a lot of start up time. 104: 105: Class::Shared moves the code into one process and lets the others keep a small 106: proxy: a L<Class::Shared::Frontend> standing in for one object, held by a 107: L<Class::Shared::Backend> that loaded the class once and does the work. 108: 109: client process singleton backend process 110: -------------- ------------------------- 111: Frontend (object #1) --socket--> Class::Shared::Backend 112: Frontend (object #2) --socket--> |-- Class::Simple (code, loaded once) 113: Frontend (object #3) --socket--> |-- object #1 114: |-- object #2 115: `-- object #3 116: 117: So there is one copy of the class in the system and as many objects as there are 118: frontends: C<< Class::Shared::Frontend->new('Class::Simple', ...) >> is one call 119: to C<< Class::Simple->new(...) >>, in the backend. 120: 121: The arguments for that constructor go in C<ctor>, as an array reference: 122: C<ctor =E<gt> [verbose =E<gt> 1]> is 123: C<< Class::Simple->new(verbose =E<gt> 1) >>. They are used once, when the 124: frontend connects and asks the backend for the object, and they travel over the 125: socket, so they must be plain data -- a code reference cannot be sent. If the 126: constructor dies, the frontend's C<new> dies too, naming the class and the error 127: the backend reported. 128: 129: L<Class::Shared::Frontend> lists every option this constructor takes. 130: 131: The frontend never loads the target class, so it stays cheap. An object lives 132: as long as the connection that made it; when the frontend goes, the backend 133: destroys the object, so its C<DESTROY> runs once, in the backend. 134: 135: One backend serves every class its clients ask for. A client names the class it 136: wants in its first message, the handshake, and the backend loads that module 137: then, once per class, however many clients ask for it. The backend may be on 138: this machine (a unix socket, see C<Class::Shared::default_dir>) or on another 139: one (TCP). 140: 141: Class::Shared is for Unix-like systems: unix sockets, POSIX directory 142: permissions and C<fork> are what make a backend reachable, private and 143: startable. See L</PLATFORMS>. 144: 145: This page documents the helper functions that the frontend, the backend and the 146: command line program share. Start with L<Class::Shared::Frontend> if you only 147: want to use a shared class. 148: 149: =head2 Words used in this documentation 150: 151: =over 4 152: 153: =item backend 154: 155: The one process that loads the class and holds the real objects. 156: 157: =item frontend 158: 159: The small stand-in object in your own process. It forwards each method call to 160: the backend and gives you back the answer. 161: 162: =item unix socket 163: 164: A special file that two programs on the same machine use to talk to each other. 165: Its file permissions decide who may connect. 166: 167: =item TCP 168: 169: The way programs on different machines talk to each other over a network. Here 170: it is always protected by TLS. 171: 172: =item TLS 173: 174: Encryption for a network connection. Each side also shows a certificate to 175: prove who it is. 176: 177: =item certificate, CA 178: 179: A certificate is a small file that says "this key belongs to this name". A CA 180: (certificate authority) is the organisation, or your own private key pair, that 181: signs certificates so others can trust them. 182: 183: =item fingerprint 184: 185: A short code (SHA-256) that identifies exactly one certificate. A client can 186: trust one certificate by its fingerprint instead of trusting a CA. 187: 188: =item token 189: 190: A shared secret, kept in a file, that both sides of a TCP connection know. It 191: proves that each message came from somebody who has the secret. 192: 193: =item frame 194: 195: One message on the wire: a length, then the data. See 196: L<Class::Shared::Protocol>. 197: 198: =item handshake 199: 200: The first message a client sends. It names the class the client wants. 201: 202: =item context 203: 204: Perl's idea of what the caller wants back from a call: a list, one value, or 205: nothing (void). Class::Shared keeps the context you used. 206: 207: =back 208: 209: =head1 FUNCTIONS 210: 211: None is exported. They are used by the frontend and the backend, and may be 212: useful when writing your own client. Call each one with its full name, for 213: example C<Class::Shared::validate_class($name)>. 214: 215: A function either returns its answer or, when it cannot, dies with a message 216: that begins C<Class::Shared:> and ends with a new line. The one exception is 217: C<ensure_dir>, which returns a pair so that the reason can be shown to the user. 218: 219: The schemas in each B<API SPECIFICATION> follow the conventions of 220: L<Params::Validate::Strict> (for input) and L<Return::Set> (for output). They 221: describe the function for people and for tools. The code does not call those 222: two modules, and this distribution does not depend on them. 223: 224: =head2 validate_class 225: 226: my $class = Class::Shared::validate_class($class); 227: 228: Checks that C<$class> looks like a Perl package name, and returns it unchanged. 229: Use it before you pass a name to C<require> or build a file name from it. 230: 231: A valid name is one or more words joined by C<::>. Each word starts with an 232: English letter or an underscore and then has letters, digits or underscores. 233: So C<Class::Simple> and C<My_Class2> are valid, and C<2Bad>, C<Foo::> and 234: C<Foo-Bar> are not. 235: 236: B<Side effects:> none. 237: 238: B<Dies> with C<Class::Shared: no class name given> if C<$class> is undef or 239: empty, and with C<Class::Shared: 'NAME' is not a valid class name> otherwise. 240: The domains and boundaries these two messages divide are listed under 241: B<API SPECIFICATION> below, and F<t/domain.t> tests every one of them. 242: 243: =head3 API SPECIFICATION 244: 245: =head4 Input 246: 247: { 248: class => { 249: type => 'string', 250: position => 0, 251: optional => 0, 252: min => 1, 253: matches => qr/\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/, 254: }, 255: } 256: 257: =head4 Domains 258: 259: valid one or more ASCII words, each starting with a letter or "_" and 260: continuing with letters, digits or "_", joined by "::" 261: invalid undef, the empty string, a leading digit in any word, an empty 262: word, any other separator, any non-ASCII character 263: 264: partition representative result 265: ------------------------------------------------------------- 266: one word Foo accepted 267: several words Foo::Bar::Baz accepted 268: digits and underscores My_Class2, A::_2 accepted 269: length 1 A, _ accepted 270: empty string "" dies: no class name 271: leading digit 2Foo dies 272: empty word Foo::, ::Foo, A:::B dies 273: hyphen, space or dot Foo-Bar, Foo Bar dies 274: non-ASCII letter "M" + U+00FC + "ller" dies 275: emoji, RTL override U+1F600, U+202E dies 276: 277: Boundary values: length 0 dies, length 1 is the smallest accepted. There 278: is no maximum: a 40,000 character name is accepted, because the check does 279: not repeat a group (perl 5.26 stops a repeated group at 32,766 repeats). 280: Only the pattern in this specification can match, so the answer is 281: untainted and safe to use as a module name. 282: 283: =head4 Output 284: 285: { type => 'string', matches => qr/\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/ } 286: 287: =cut 288: 289: sub validate_class { โ290 โ 302 โ 307 290: my $class = $_[0]; 291: 292: die "Class::Shared: no class name given\n" 293: unless defined $class && length $class; 294: 295: # The captured copy is returned, not the argument: it is the only text that 296: # has been matched against the pattern, so under taint mode (-T) it is 297: # untainted by the check that proves it safe. 298: my ($valid) = $class =~ $CLASS_CHARS_RE 299: or die "Class::Shared: '$class' is not a valid class name\n"; 300: 301: # -1 keeps a trailing empty part, so "A::" and "A:::B" are refused. 302: foreach my $part (split /::/, $valid, -1) { 303: $part =~ $CLASS_PART_RE 304: or die "Class::Shared: '$class' is not a valid class name\n"; 305: } 306: 307: return $valid;Mutants (Total: 2, Killed: 2, Survived: 0)
308: } 309: 310: =head2 read_secret 311: 312: my $secret = Class::Shared::read_secret('/etc/class-shared/token'); 313: 314: Reads a shared secret (a token) from a file and returns it. The whole file is 315: read as bytes. White space at the end, including the final new line, is 316: removed. White space at the start, and any inside the secret, is kept. 317: 318: Never put a secret on a command line, where other users can see it. Give its 319: file name instead and let this function read it. 320: 321: B<Side effects:> opens, reads and closes the file. 322: 323: B<Dies> if no file name is given, if the file cannot be read, if it is larger 324: than 65536 bytes (which also refuses an endless file such as F</dev/zero>), or 325: if the secret is empty after the end of it has been trimmed. 326: 327: =head3 API SPECIFICATION 328: 329: =head4 Input 330: 331: { 332: path => { type => 'string', position => 0, optional => 0, min => 1 }, 333: } 334: 335: =head4 Domains 336: 337: valid a path to a readable file whose content, after trailing white 338: space is removed, is 1 to 65536 bytes 339: invalid undef, the empty path, an unreadable path, a directory, a file 340: larger than 65536 bytes, and a file that is empty after trimming 341: 342: partition representative result 343: ----------------------------------------------------------- 344: 1 byte "a" accepted 345: trailing white space "secret \r\n" trimmed 346: leading white space " secret" kept 347: white space only "\n\t " dies: is empty 348: 0 bytes "" dies: is empty 349: 65536 bytes "a" x 65536 accepted 350: 65537 bytes "a" x 65537 dies: too large 351: 352: Boundary values: 0 bytes dies, 1 byte is the smallest accepted, 65536 353: bytes is the largest accepted, 65537 dies. The size is counted in bytes, 354: so the limit is the same for a UTF-8 or a binary secret. The content is 355: returned as bytes, unchanged and not decoded, so an embedded NUL and a 356: multibyte character cost exactly what they occupy in the file. 357: 358: =head4 Output 359: 360: { type => 'string', min => 1 } 361: 362: =cut 363: 364: # A shared secret for a backend listening on a network socket. Never pass 365: # secrets on a command line: give a file and let the caller read it. 366: # The most read_secret accepts, in bytes. A token is a line of text. 367: our $MAX_SECRET = 65536; 368: 369: sub read_secret { โ370 โ 379 โ 387 370: my $path = $_[0]; 371: 372: die "Class::Shared: no token file given\n" unless defined $path && length $path; 373: 374: open(my $fh, '<', $path) or die "Class::Shared: cannot read $path: $!\n"; 375: 376: # One byte more than is allowed is asked for, and no more: a "file" such as 377: # /dev/zero never ends, and reading it whole would use all the memory. 378: my $secret = ''; 379: while (length($secret) <= $MAX_SECRET) {
380: my $got = read($fh, $secret, $MAX_SECRET + 1 - length($secret), length($secret)); 381: 382: # Some systems (macOS) let a directory be opened and only fail here. 383: die "Class::Shared: cannot read $path: $!\n" unless defined $got; 384: last unless $got; 385: } 386: 387: close($fh) or die "Class::Shared: cannot read $path: $!\n"; 388: 389: die "Class::Shared: $path is too large to be a secret (more than $MAX_SECRET bytes)\n" 390: if length($secret) > $MAX_SECRET;Mutants (Total: 3, Killed: 0, Survived: 3)
- NUM_BOUNDARY_379_25_<: Numeric boundary flip <= to <
HIGH: Likely missing edge-case test (boundary value)๐งช Suggested Test# Boundary test suggestion is( func(VALUE_AT_BOUNDARY), EXPECTED, 'Test boundary behaviour' );- NUM_BOUNDARY_379_25_>: Numeric boundary flip <= to >
HIGH: Likely missing edge-case test (boundary value)๐งช Suggested Test# Boundary test suggestion is( func(VALUE_AT_BOUNDARY), EXPECTED, 'Test boundary behaviour' );- NUM_BOUNDARY_379_25_>=: Numeric boundary flip <= to >=
HIGH: Likely missing edge-case test (boundary value)๐งช Suggested Test# Boundary test suggestion is( func(VALUE_AT_BOUNDARY), EXPECTED, 'Test boundary behaviour' );Mutants (Total: 3, Killed: 3, Survived: 0)
391: 392: $secret =~ s/\s+\z//; 393: 394: die "Class::Shared: $path is empty\n" unless length $secret; 395: 396: return $secret;
Mutants (Total: 2, Killed: 2, Survived: 0)
397: } 398: 399: # A SHA-256 fingerprint of a certificate, in the shape IO::Socket::SSL wants 400: # for SSL_fingerprint. Accepts the colon-separated form openssl prints, with 401: # or without a "sha256$" prefix, so a pin can be pasted straight from 402: # "openssl x509 -fingerprint -sha256". 403: my $FINGERPRINT_RE = qr{ 404: \A 405: (?: sha256 \$ )? 406: ( (?:[0-9A-Fa-f]{2}:){31}[0-9A-Fa-f]{2} | [0-9A-Fa-f]{64} ) 407: \z 408: }x; 409: 410: sub normalise_fingerprint { 411: my $value = $_[0]; 412: 413: die "Class::Shared: no TLS fingerprint given\n" 414: unless defined $value && length $value; 415: 416: my ($hex) = $value =~ $FINGERPRINT_RE 417: or die "Class::Shared: '$value' is not a SHA-256 certificate fingerprint: " 418: . "give 64 hexadecimal digits, optionally colon-separated\n"; 419: 420: $hex =~ s/://g; 421: 422: return 'sha256$' . lc $hex;
Mutants (Total: 2, Killed: 2, Survived: 0)
423: } 424: 425: # The TLS options a client hands to IO::Socket::SSL->new, in one place so the 426: # frontend and the command line cannot drift apart about what verification 427: # means. IO::Socket::SSL is loaded here rather than at the top of this module: 428: # a unix socket never needs any of it. 429: sub client_tls_options { โ430 โ 441 โ 452 430: my (%opt) = @_; 431: 432: require IO::Socket::SSL; 433: 434: my %tls = ( 435: SSL_version => $TLS_VERSIONS, 436: SSL_cert_file => $opt{cert}, 437: SSL_key_file => $opt{key}, 438: SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER(), 439: ); 440: 441: if (defined $opt{fingerprint}) {
Mutants (Total: 1, Killed: 1, Survived: 0)
442: # A pin names one exact certificate, so it stands in for both the CA 443: # and the name, and SSL_force_fingerprint makes the digest the only 444: # thing that can accept the peer. 445: $tls{SSL_fingerprint} = $opt{fingerprint}; 446: $tls{SSL_force_fingerprint} = 1; 447: $tls{SSL_verifycn_scheme} = 'none'; 448: } else { 449: $tls{SSL_ca_file} = $opt{ca}; 450: } 451: 452: return %tls;
Mutants (Total: 2, Killed: 2, Survived: 0)
453: } 454: 455: sub dir_of { 456: my ($path) = @_; 457: my $dir = File::Basename::dirname($path); 458: return length $dir ? $dir : '/';
Mutants (Total: 2, Killed: 2, Survived: 0)
459: } 460: 461: # Sleep for one polling interval. Used by every loop that waits for something 462: # on the file system, so none of them spins and all of them wake at the same 463: # rate. select is the portable sub-second sleep that needs no extra module. 464: sub poll_pause { 465: select(undef, undef, undef, $POLL_INTERVAL); 466: 467: return; 468: } 469: 470: # Point STDIN at the null device and STDOUT and STDERR at the log, so a 471: # background process holds no terminal and its diagnostics land somewhere. If 472: # the log cannot be opened they go to the null device instead: a backend that 473: # cannot log should still run. Returns (1, undef), or (0, $why) if a stream 474: # could not be reopened; the caller decides whether that is fatal, because 475: # this is used both by the backend and by the forked child that becomes one. 476: sub detach_standard_streams { โ477 โ 483 โ 492 477: my ($log) = @_; 478: 479: my $null = File::Spec->devnull; 480: 481: open(STDIN, '<', $null) or return (0, "cannot reopen STDIN on $null: $!"); 482: 483: if (open(my $log_fh, '>>', $log)) {
Mutants (Total: 1, Killed: 1, Survived: 0)
484: open(STDOUT, '>&', $log_fh) or return (0, "cannot send STDOUT to $log: $!"); 485: open(STDERR, '>&', $log_fh) or return (0, "cannot send STDERR to $log: $!"); 486: close($log_fh) or return (0, "cannot close $log: $!"); 487: } else { 488: open(STDOUT, '>', $null) or return (0, "cannot send STDOUT to $null: $!"); 489: open(STDERR, '>&', \*STDOUT) or return (0, "cannot send STDERR to $null: $!"); 490: } 491: 492: return (1, undef); 493: } 494: 495: # Where per-user sockets live. Kept short because the path ends up in a 496: # sockaddr_un, and per-user because the socket is a trust boundary. 497: sub default_dir { โ498 โ 502 โ 521 498: my (%opt) = @_; 499: 500: my $dir; 501: 502: if (defined $opt{dir} && length $opt{dir}) {
Mutants (Total: 1, Killed: 1, Survived: 0)
503: $dir = $opt{dir}; 504: } elsif (defined $ENV{CLASS_SHARED_DIR} && length $ENV{CLASS_SHARED_DIR}) { 505: $dir = $ENV{CLASS_SHARED_DIR}; 506: } elsif (defined $ENV{XDG_RUNTIME_DIR} && length $ENV{XDG_RUNTIME_DIR} && -d $ENV{XDG_RUNTIME_DIR}) { 507: $dir = File::Spec->catdir($ENV{XDG_RUNTIME_DIR}, 'class-shared'); 508: } elsif (-d "/run/user/$>") { 509: $dir = File::Spec->catdir("/run/user/$>", 'class-shared'); 510: } else { 511: $dir = File::Spec->catdir(File::Spec->tmpdir, "class-shared-$>"); 512: } 513: 514: # The directory may come from the environment or the caller, both of which 515: # are tainted under -T, and perl refuses to mkdir or open a file in a 516: # tainted path. A name is not dangerous because of what it says, only 517: # because of what it is used for, and everything made here is checked by 518: # ensure_dir (ours, and closed to others) before anything is trusted in it. 519: # The one thing that is refused outright is a NUL byte, which would end 520: # the name early in the system call and so name a different place. 521: my ($clean) = $dir =~ /\A([^\0]+)\z/ 522: or die "Class::Shared: a directory name may not contain a NUL byte\n"; 523: 524: return $clean;
Mutants (Total: 2, Killed: 2, Survived: 0)
525: } 526: 527: # Whether this perl can make a unix domain socket, which is what a backend is 528: # reached through and what a frontend starts one over. Probed rather than 529: # assumed from $^O: a Cygwin perl is not MSWin32 and has them, and a platform 530: # without them is refused whatever it calls itself. The answer cannot change 531: # during a run, so it is asked for once. 532: our $UNIX_SOCKETS; 533: 534: sub _has_unix_sockets { 535: return $UNIX_SOCKETS if defined $UNIX_SOCKETS;
Mutants (Total: 2, Killed: 2, Survived: 0)
536: 537: $UNIX_SOCKETS = eval { 538: # Both have to work. A Windows perl 5.26 emulates socketpair, so it 539: # succeeds, but cannot name a unix socket, and pack_sockaddr_un dies. 540: # IO::Socket::UNIX is not required here: it would cost every client 541: # process about 1.2 MB, and the backend and the command load it 542: # themselves when they need it. 543: require Socket; 544: 545: Socket::pack_sockaddr_un('probe'); 546: 547: socketpair(my $reader, my $writer, Socket::AF_UNIX(), Socket::SOCK_STREAM(), 0) 548: ? 1
Mutants (Total: 2, Killed: 2, Survived: 0)
549: : 0; 550: } ? 1 : 0; 551: 552: return $UNIX_SOCKETS; 553: } 554: 555: # Nothing here can work without unix domain sockets, so say that where it is 556: # tried rather than blaming the directory the caller passed: on a platform 557: # without them there is no directory that would have helped. Whether they 558: # exist can be given as an argument, so the refusal is exercised by the suite 559: # on machines that have them. 560: sub _require_unix_sockets { 561: my ($what, $has_unix_sockets) = @_; 562: 563: $has_unix_sockets = _has_unix_sockets() unless defined $has_unix_sockets; 564: 565: return if $has_unix_sockets; 566: 567: die "Class::Shared: $what is not available here: this perl has no unix " 568: . "domain sockets, so a backend could neither be reached nor started. " 569: . "This distribution is Unix-only (see PLATFORMS)\n"; 570: } 571: 572: # Create $dir if needed and make sure nobody else can drop a socket in it. 573: # Returns (1, undef), or (0, $why_not), as the POD documents. 574: sub ensure_dir { โ575 โ 581 โ 590 575: my ($dir) = @_; 576: 577: die "Class::Shared: no directory given\n" unless defined $dir && length $dir;
Mutants (Total: 1, Killed: 1, Survived: 0)
578: 579: _require_unix_sockets('a backend directory'); 580:
Mutants (Total: 1, Killed: 1, Survived: 0)
581: if (!-d $dir) { 582: # Losing a race to make it is not a failure; what the winner made is 583: # checked below like any directory that was already there. 584: if (!mkdir($dir, 0700)) { 585: my $why = "$!"; 586: return (0, "cannot create $dir: $why") unless -d $dir;
Mutants (Total: 2, Killed: 2, Survived: 0)
587: } 588: } 589: 590: return _check_dir($dir); 591: } 592: 593: # Purpose: say whether $dir is somewhere only this user can put a socket. 594: # Entry: $dir is a directory name; nothing is created. 595: # Exit status: (1, undef) when it is ours and not writable by group or 596: # others, otherwise (0, $why_not). 597: # Side effects: none. 598: sub _check_dir { 599: my ($dir) = @_; 600: 601: my @stat = stat($dir);
Mutants (Total: 1, Killed: 1, Survived: 0)
602: return (0, "cannot stat $dir: $!") unless @stat; 603: 604: return (0, "$dir is owned by uid $stat[4], not by uid $>") 605: if $stat[4] != $>; 606: 607: return (0, "$dir is writable by group or others; refusing to use it") 608: if $stat[2] & 0022; 609: 610: return (1, undef); 611: } 612: 613: # Purpose: decide whether to talk to the socket at $path. What comes back 614: # over it is thawed by Storable, so a socket made by another user 615: # must never be trusted: it could answer with whatever it likes, 616: # and would be told every argument the client sends. 617: # Entry: $path names a socket that is known to exist. 618: # Exit status: (1, undef) when its directory is safe (see _check_dir) and the 619: # socket is a socket owned by this user, otherwise (0, $why_not). 620: # Side effects: none. 621: # Why both: a safe directory means nobody else could have put the socket 622: # there; the owner check catches a socket that was already there. 623: sub _check_endpoint { 624: my ($path) = @_; 625: 626: my ($safe, $why) = _check_dir(dir_of($path)); 627: return (0, $why) unless $safe; 628: 629: my @stat = stat($path); 630: return (0, "cannot stat $path: $!") unless @stat; 631:
Mutants (Total: 1, Killed: 1, Survived: 0)
632: return (0, "$path is not a socket") unless -S _; 633: 634: return (0, "$path is owned by uid $stat[4], not by uid $>") 635: if $stat[4] != $>; 636: 637: return (1, undef); 638: } 639: 640: # One socket for every class. The backend behind it is told which class a 641: # client wants when that client connects, so the path names no class and one 642: # backend can serve as many as are asked for. 643: sub socket_path { 644: my ($dir) = @_; 645: 646: _require_unix_sockets('a unix socket');
Mutants (Total: 2, Killed: 2, Survived: 0)
647: 648: $dir = default_dir(dir => $dir); 649: 650: return check_socket_path(File::Spec->catfile($dir, 'class-shared.sock')); 651: } 652: 653: # A path that will not fit in a sockaddr_un is silently truncated by the 654: # operating system, so a client and a backend could end up on different 655: # sockets. Refuse it instead. Applies to a path given explicitly as well as 656: # one socket_path built. 657: sub check_socket_path { โ658 โ 662 โ 668 658: my ($path) = @_;
Mutants (Total: 4, Killed: 4, Survived: 0)
659: 660: die "Class::Shared: no socket path given\n" unless defined $path && length $path; 661: 662: if (length($path) > $MAX_SOCKET_PATH) { 663: die sprintf("Class::Shared: socket path '%s' is %d characters, more than the %d allowed; " 664: . "set CLASS_SHARED_DIR or pass dir => to a shorter directory\n",
Mutants (Total: 2, Killed: 2, Survived: 0)
665: $path, length($path), $MAX_SOCKET_PATH); 666: } 667: 668: return $path; 669: } 670: 671: 1; 672: 673: __END__ 674: 675: =head2 normalise_fingerprint 676: 677: my $pin = Class::Shared::normalise_fingerprint( 678: 'AB:CD:...:EF' # 32 pairs of hex digits, as openssl prints them 679: ); 680: 681: Turns the SHA-256 fingerprint of a certificate into the one form the TLS layer 682: wants: C<sha256$> followed by 64 lower case hexadecimal digits. 683: 684: It accepts 64 hex digits, or 32 pairs of hex digits separated by colons, with or 685: without a leading C<sha256$>. Upper or lower case digits are fine. The prefix 686: itself must be lower case. To get a fingerprint from a certificate file, run 687: C<openssl x509 -in server.pem -noout -fingerprint -sha256> and copy the part 688: after the equals sign. 689: 690: B<Side effects:> none. 691: 692: B<Dies> if no value is given, or if the value is not a SHA-256 fingerprint. 693: 694: =head3 API SPECIFICATION 695: 696: =head4 Input 697: 698: { 699: value => { 700: type => 'string', 701: position => 0, 702: optional => 0, 703: matches => qr/\A(?:sha256\$)?(?:(?:[0-9A-Fa-f]{2}:){31}[0-9A-Fa-f]{2}|[0-9A-Fa-f]{64})\z/, 704: }, 705: } 706: 707: =head4 Domains 708: 709: valid exactly 64 hexadecimal digits, or 32 pairs of two hexadecimal 710: digits separated by ":", with or without a lower case "sha256$" 711: prefix 712: invalid any other length, any other separator, an upper case prefix, and 713: any character that is not a hexadecimal digit 714: 715: partition representative result 716: ---------------------------------------------------------- 717: 64 digits 0102...1f20 accepted 718: 32 colon-separated 01:02:...:1f:20 accepted 719: sha256$ prefix sha256$0102... accepted 720: upper case digits 0102...1F20 accepted, lower cased 721: upper case prefix SHA256$0102... dies 722: 63 or 65 digits one short, one long dies 723: 31 or 33 pairs one short, one long dies 724: spaces or hyphens 01 02 03..., 01-02-03 dies 725: non-ASCII character U+00FC, U+1F600, U+202E dies 726: 727: Boundary values: the plain form is valid at exactly 64 characters (63 and 728: 65 die); the colon form is valid at exactly 95 (94 and 96 die). The 729: prefix is removed and every digit is lower cased; all invalid values are 730: echoed in the message byte for byte, without being decoded. 731: 732: =head4 Output 733: 734: { type => 'string', matches => qr/\Asha256\$[0-9a-f]{64}\z/ } 735: 736: =head2 client_tls_options 737: 738: my %tls = Class::Shared::client_tls_options( 739: cert => 'client.pem', 740: key => 'client-key.pem', 741: ca => 'ca.pem', 742: ); 743: my $socket = IO::Socket::SSL->new(PeerAddr => $host, PeerPort => $port, %tls); 744: 745: Returns the options a TLS client needs, as a flat list of key and value pairs 746: that you can pass straight to C<< IO::Socket::SSL->new >>. It exists so that 747: the frontend and the command line program cannot disagree about what "verify the 748: backend" means. 749: 750: Give C<cert> and C<key> (the certificate this client shows), and then either 751: C<ca> or C<fingerprint>. With C<ca>, the backend's certificate must be signed 752: by that CA and must name the host you connect to. With C<fingerprint> (the 753: output of L</normalise_fingerprint>), exactly one certificate is trusted, and 754: its name and its signer do not matter. Verification is always on. 755: 756: This function does not check that the files exist, and does not check that you 757: gave only one of C<ca> and C<fingerprint>. The caller does that. 758: 759: B<Side effects:> loads L<IO::Socket::SSL>. Nothing else loads it. 760: 761: B<Dies> if L<IO::Socket::SSL> is not installed. 762: 763: =head3 API SPECIFICATION 764: 765: =head4 Input 766: 767: { 768: cert => { type => 'string', optional => 0 }, 769: key => { type => 'string', optional => 0 }, 770: ca => { type => 'string', optional => 1 }, 771: fingerprint => { type => 'string', optional => 1, 772: matches => qr/\Asha256\$[0-9a-f]{64}\z/ }, 773: } 774: 775: =head4 Domains 776: 777: valid cert and key, then either ca or fingerprint (or neither, which 778: the caller is expected to have refused already) 779: invalid nothing is refused here: this function checks neither that the 780: files exist nor that ca and fingerprint are not both given 781: 782: partition representative result 783: --------------------------------------------------------------- 784: ca given ca => "ca.pem" SSL_ca_file 785: fingerprint given sha256$ + 64 lower hex digits pin, no CA 786: both given ca and fingerprint the pin wins 787: neither given cert and key only SSL_ca_file undef 788: multibyte path /etc/U+00FC/client.pem passed through 789: 790: Output boundaries: the flat list has five pairs with a CA and seven with a 791: pin, so ten and fourteen elements. SSL_verify_mode is "verify the peer" 792: in every partition, and SSL_version is the same for both ends. 793: 794: =head4 Output 795: 796: { 797: type => 'arrayref', 798: min => 10, 799: max => 14, 800: description => 'a flat list of option name and value pairs: SSL_version, SSL_cert_file, SSL_key_file, SSL_verify_mode, and either SSL_ca_file or (SSL_fingerprint, SSL_force_fingerprint, SSL_verifycn_scheme)', 801: } 802: 803: =head2 dir_of 804: 805: my $dir = Class::Shared::dir_of('/run/user/1000/class-shared/x.sock'); 806: # '/run/user/1000/class-shared' 807: 808: Returns the directory part of a path, so that the answer can always be used. A 809: path with no directory part, such as C<x.sock>, gives C<.> (the current 810: directory). A file directly in the top directory, such as C</x.sock>, gives C</>. 811: 812: B<Side effects:> none. 813: 814: =head3 API SPECIFICATION 815: 816: =head4 Input 817: 818: { path => { type => 'string', position => 0, optional => 0 } } 819: 820: =head4 Domains 821: 822: partition representative result 823: ------------------------------------------------- 824: bare file name x.sock . 825: file in the root /x.sock / 826: nested file /a/b/c /a/b 827: trailing slash a/b/ a 828: doubled separator a//b a 829: empty path "" . 830: 831: The answer is never empty, so it can always be used: a path with no 832: directory part gives ".". There is no length boundary. A multibyte 833: directory part is returned unchanged, with no encoding or decoding. 834: 835: =head4 Output 836: 837: { type => 'string', min => 1 } 838: 839: =head2 default_dir 840: 841: my $dir = Class::Shared::default_dir(); 842: my $dir = Class::Shared::default_dir(dir => '/tmp/mine'); 843: 844: Returns the directory that holds this user's sockets. The first of these that 845: applies wins: 846: 847: =over 4 848: 849: =item 1. 850: 851: the C<dir> you pass; 852: 853: =item 2. 854: 855: the environment variable C<CLASS_SHARED_DIR>; 856: 857: =item 3. 858: 859: C<$ENV{XDG_RUNTIME_DIR}/class-shared>, if C<XDG_RUNTIME_DIR> names a directory 860: that exists; 861: 862: =item 4. 863: 864: F</run/user/UID/class-shared>, if F</run/user/UID> exists (UID is the effective 865: user id); 866: 867: =item 5. 868: 869: a per-user directory under the system temporary directory. 870: 871: =back 872: 873: The path is kept short on purpose, because it ends up inside a socket address, 874: which has a small size limit. It is per user because the socket is a trust 875: boundary. 876: 877: B<Side effects:> reads the environment and checks whether directories exist. It 878: does not create anything; use L</ensure_dir> for that. 879: 880: B<Dies> if the directory name contains a NUL byte, which would end the name 881: early in a system call and so name a different place. Under taint mode 882: (C<-T>) the name is untainted here, because everything made in it is checked by 883: L</ensure_dir> before it is trusted. 884: 885: =head3 API SPECIFICATION 886: 887: =head4 Input 888: 889: { dir => { type => 'string', optional => 1, min => 1 } } 890: 891: =head4 Domains 892: 893: The first partition that applies wins, so they are ordered: 894: 895: rank partition result 896: ----------------------------------------------------------------- 897: 1 dir given and not empty the dir, unchanged 898: 2 CLASS_SHARED_DIR set and not empty that value 899: 3 XDG_RUNTIME_DIR names a directory that directory/class-shared 900: 4 /run/user/UID exists that directory/class-shared 901: 5 none of the above tmpdir/class-shared-UID 902: 903: invalid: a name containing a NUL byte dies at any rank, because a system 904: call would end the name there and so name a different place. The empty 905: string is not invalid: it means "not given" and falls through to the next 906: rank. A multibyte name is returned unchanged, not encoded. Nothing is 907: created, so a rank may name a directory that does not exist yet. 908: 909: =head4 Output 910: 911: { type => 'string', min => 1 } 912: 913: =head2 ensure_dir 914: 915: my ($ok, $why) = Class::Shared::ensure_dir($dir); 916: die "cannot use $dir: $why" unless $ok; 917: 918: Makes sure C<$dir> exists and that nobody else can put a socket in it. It 919: returns C<(1, undef)> on success or C<(0, $why_not)> when the directory is not 920: safe. It is refused if 921: 922: =over 4 923: 924: =item * 925: 926: it cannot be created or inspected; 927: 928: =item * 929: 930: it belongs to a different user (anybody who owns the directory could pretend to 931: be the backend); or 932: 933: =item * 934: 935: group or other users can write to it. 936: 937: =back 938: 939: If the directory is missing it is created with mode 0700. Only the last part of 940: the path is created: the parent directory must already exist. 941: 942: B<Side effects:> may create one directory. 943: 944: B<Dies> (and does not return a pair) if no directory is given, or on a perl 945: without unix sockets. 946: 947: =head3 API SPECIFICATION 948: 949: =head4 Input 950: 951: { dir => { type => 'string', position => 0, optional => 0, min => 1 } } 952: 953: =head4 Domains 954: 955: valid a path whose last part exists, or can be created, as a directory 956: owned by this user with neither the group write bit (0020) nor 957: the other write bit (0002) set 958: invalid undef, the empty path, a path whose parent is missing, a path 959: that is not a directory, a directory owned by another user, and 960: a directory with either write bit set 961: 962: partition representative result 963: ----------------------------------------------------- 964: does not exist tmp/fresh (1, undef), mode 0700 965: exists, mode 0700 tmp/private (1, undef) 966: exists, mode 0755 tmp/open (1, undef) 967: exists, mode 0777 tmp/writable (0, reason) 968: exists, mode 0770 group writable (0, reason) 969: exists, mode 0702 other writable (0, reason) 970: parent missing tmp/absent/child (0, "cannot create ...") 971: a plain file tmp/file (0, "cannot create ...") 972: empty or undef "" dies 973: 974: Boundary values: the boundary is the write mask 0022, not a single mode: 975: 0700 is the most private mode created and 0777 the most open refused, but 976: 0755 is accepted because others may read and search, not write. Only the 977: last part of the path is created, so a missing parent is a refusal, not a 978: recursive mkdir. A multibyte name is created as given. 979: 980: =head4 Output 981: 982: { 983: type => 'arrayref', 984: min => 2, 985: max => 2, 986: schema => [ 987: { type => 'boolean', description => '1 when the directory is safe to use' }, 988: { type => 'string', optional => 1, description => 'undef on success, otherwise the reason' }, 989: ], 990: } 991: 992: =head2 socket_path 993: 994: my $path = Class::Shared::socket_path(); 995: my $path = Class::Shared::socket_path('/tmp/mine'); 996: 997: Returns the path of the backend socket inside C<$dir>, or inside the default 998: directory (see L</default_dir>) if none is given. The file name is always 999: C<class-shared.sock>. There is one socket for every class, because the client 1000: chooses the class and not the listener. 1001: 1002: B<Side effects:> none. The directory and the socket are not created. 1003: 1004: B<Dies> on a perl without unix sockets, or if the result is longer than 100 1005: characters (see L</check_socket_path>). 1006: 1007: =head3 API SPECIFICATION 1008: 1009: =head4 Input 1010: 1011: { dir => { type => 'string', position => 0, optional => 1, min => 1 } } 1012: 1013: =head4 Domains 1014: 1015: valid no argument, or a directory name, such that the socket path is 1016: 1 to 100 characters 1017: invalid a directory name that makes the path longer than 100 characters, 1018: which dies with the message documented under L</check_socket_path> 1019: 1020: partition representative result 1021: ------------------------------------------------------------------ 1022: default directory () .../class-shared.sock 1023: a directory /tmp /tmp/class-shared.sock 1024: path exactly 100 characters dir of 82 chars accepted 1025: path 101 characters dir of 83 chars dies 1026: 1027: The file name is always "class-shared.sock", so the boundary is reached 1028: through the directory: with a 17 character file name the largest usable 1029: directory is 82 characters. Nothing is created and nothing is checked for 1030: existence. 1031: 1032: =head4 Output 1033: 1034: { type => 'string', min => 1, max => 100 } 1035: 1036: =head2 check_socket_path 1037: 1038: my $path = Class::Shared::check_socket_path($path); 1039: 1040: Returns C<$path> if it is short enough to be a socket address, and dies if it 1041: is not, or if no path is given at all. The limit is 100 characters, which is 1042: less than the operating system's limit so that the code stays portable. Without this check the system would cut 1043: a long path short without saying so, and a client and a backend could end up 1044: using two different sockets. 1045: 1046: The frontend, the backend and F<class-shared> all use it for a C<socket> that 1047: you give yourself, as well as for the path that C<socket_path> builds. 1048: 1049: B<Side effects:> none. 1050: 1051: B<Dies> with C<Class::Shared: no socket path given> if C<$path> is undef or 1052: empty, and with C<Class::Shared: socket path 'PATH' is N characters, more than 1053: the 100 allowed; set CLASS_SHARED_DIR or pass dir =E<gt> to a shorter 1054: directory> if it is too long. 1055: 1056: =head3 API SPECIFICATION 1057: 1058: =head4 Input 1059: 1060: { path => { type => 'string', position => 0, optional => 0, min => 1 } } 1061: 1062: =head4 Domains 1063: 1064: valid a non-empty path of 1 to 100 characters 1065: invalid undef, the empty path, and any path longer than 100 characters 1066: 1067: partition representative result 1068: ------------------------------------------------- 1069: length 1 / accepted 1070: length 99 ... accepted 1071: length 100 ... accepted, the maximum 1072: length 101 ... dies 1073: length 0 or undef "" dies 1074: 1075: Boundary values: 100 is accepted and 101 dies; 0 and undef die. The limit 1076: is 100 characters and is below the operating system's. A multibyte path is 1077: counted in characters and returned unchanged. This function does not check 1078: that a socket can use its characters: see L</ENCODING>. 1079: 1080: =head4 Output 1081: 1082: { type => 'string', min => 1, max => 100 } 1083: 1084: =head2 poll_pause 1085: 1086: Class::Shared::poll_pause(); 1087: 1088: Sleeps for one polling interval (C<$Class::Shared::POLL_INTERVAL>, 0.02 1089: seconds). Every loop that waits for a socket or a lock uses it, so that none of 1090: them spins the processor and all of them wake at the same rate. 1091: 1092: B<Side effects:> sleeps. It returns nothing. 1093: 1094: =head3 API SPECIFICATION 1095: 1096: =head4 Input 1097: 1098: { } 1099: 1100: =head4 Domains 1101: 1102: Takes no input, so it has one partition. The output is the empty list 1103: (undef in scalar context). The only observable effect is a sleep of 1104: C<$Class::Shared::POLL_INTERVAL> seconds, 0.02, which is the same for 1105: every loop that waits for a socket or a lock. 1106: 1107: =head4 Output 1108: 1109: { type => 'undef' } 1110: 1111: =head2 detach_standard_streams 1112: 1113: my ($ok, $why) = Class::Shared::detach_standard_streams('/path/to/log'); 1114: 1115: Used when a backend goes into the background. It reopens standard input on the 1116: null device (F</dev/null>) and standard output and standard error on the log 1117: file, so that the process holds no terminal and its messages land in a file. If 1118: the log file cannot be opened, they go to the null device instead, because a 1119: backend that cannot write a log should still run. 1120: 1121: It returns C<(1, undef)>, or C<(0, $why)> if a stream could not be reopened. The 1122: caller decides whether that matters: the backend treats it as fatal. 1123: 1124: B<Side effects:> replaces STDIN, STDOUT and STDERR of the current process. Do 1125: not call it in a program that still needs them. 1126: 1127: =head3 API SPECIFICATION 1128: 1129: =head4 Input 1130: 1131: { log => { type => 'string', position => 0, optional => 0, min => 1 } } 1132: 1133: =head4 Domains 1134: 1135: valid any log path: one that can be opened for appending, and one that 1136: cannot (the streams then go to the null device and the call still 1137: succeeds, because a backend that cannot log should still run) 1138: invalid none by value: an unopenable path is a documented fallback, not 1139: an error 1140: 1141: partition representative result 1142: --------------------------------------------------------- 1143: writable log /tmp/backend.log (1, undef), file created 1144: log in a missing dir /absent/backend.log (1, undef), null device 1145: 1146: The refusal path, C<(0, $why)>, exists for a failure to reopen a stream 1147: after the log was opened, such as a descriptor limit; it cannot be 1148: provoked by the value of the path alone. The call replaces STDIN, STDOUT 1149: and STDERR of the current process, so a test makes it in a child. 1150: 1151: =head4 Output 1152: 1153: { 1154: type => 'arrayref', 1155: min => 2, 1156: max => 2, 1157: schema => [ 1158: { type => 'boolean' }, 1159: { type => 'string', optional => 1 }, 1160: ], 1161: } 1162: 1163: =head1 PACKAGE VARIABLES 1164: 1165: These can be changed with C<local> in a test or a special program. Most users 1166: never need to. 1167: 1168: =over 4 1169: 1170: =item C<$Class::Shared::VERSION> 1171: 1172: The version of the distribution. 1173: 1174: =item C<$Class::Shared::PROTOCOL_VERSION> 1175: 1176: The version of the wire protocol, now 2. A backend refuses a client that speaks 1177: a different one. It is raised whenever the protocol changes in a way old 1178: clients cannot understand. 1179: 1180: =item C<$Class::Shared::MAX_SOCKET_PATH> 1181: 1182: The longest socket path this module will make or accept: 100 characters. 1183: 1184: =item C<$Class::Shared::POLL_INTERVAL> 1185: 1186: How long, in seconds, the waiting loops sleep between looks: 0.02. 1187: 1188: =item C<$Class::Shared::UNIX_TRUST_REASON> 1189: 1190: The sentence explaining why a unix socket needs no token and no TLS. The 1191: backend, the frontend and the command line all quote it, so they use the same 1192: words. 1193: 1194: =back 1195: 1196: =head1 ENCODING 1197: 1198: Class::Shared moves text between processes, so it matters which parts accept 1199: more than plain English letters. 1200: 1201: =over 4 1202: 1203: =item Arguments, results and error text: full Unicode. 1204: 1205: The arguments you pass to a method, the C<ctor> list, the values a method 1206: returns and the text of an exception all travel through L<Storable>, which 1207: keeps Perl strings exactly as they are. Accented letters, Chinese, Japanese, 1208: Arabic and emoji all arrive unchanged, with their "UTF-8 flag" kept. This was 1209: checked with a round trip of C<"caf\x{e9}">, C<"\x{1F600}"> and a Japanese word. 1210: 1211: Bytes and characters are kept apart. A byte string comes back as a byte string 1212: and a character string as a character string. So decode your input (for 1213: example with L<Encode>) before you send it, and encode it when you print it. 1214: 1215: =item Class names: ASCII only. 1216: 1217: A class name may contain only the letters A to Z (either case), digits and 1218: underscores, joined by C<::>. A class with non-ASCII letters in its name is 1219: refused by L</validate_class>, even though Perl itself would allow it. 1220: 1221: =item Method names: ASCII is safe. 1222: 1223: A method name is passed on as given. Use ASCII names; a name that does not 1224: exist is reported as "has no method". 1225: 1226: =item Secrets (token, token file): ASCII only. 1227: 1228: Use only ASCII characters in a token. The token file is read as bytes, but a 1229: C<token> you give in your program is a Perl string. The word C<caf\x{e9}> has 1230: different bytes in the two forms and so gives a different digest, and a 1231: character above code point 255 stops the digest from being calculated at all. 1232: Both sides would then fail to agree, and the connection is refused. 1233: 1234: =item File and directory names: ASCII is safest. 1235: 1236: Paths (C<dir>, C<socket>, C<log>, certificate files) are handed to the operating 1237: system as they are. Non-ASCII names work if every program uses the same bytes, 1238: but they are not tested here. The 100 character limit on a socket path counts 1239: characters. Perl gives a socket one byte for each character up to code point 1240: 255, so an accepted path is not cut short. A path with a higher character in a 1241: character string cannot be used as a socket: connecting or binding dies with 1242: C<Wide character>. Encode such a name to bytes first; each byte then counts 1243: towards the limit. 1244: 1245: =item Fingerprints: hexadecimal digits only. 1246: 1247: See L</normalise_fingerprint>. 1248: 1249: =item The backend log: be careful. 1250: 1251: The log file has no encoding layer. Text that has only characters up to code 1252: point 255 (such as C<caf\x{e9}>) is written as one byte per character, which is 1253: not UTF-8. Text with a higher character (such as an emoji) is written as UTF-8 1254: and Perl prints a C<Wide character> warning. A log that contains both is mixed. 1255: Log lines are mostly the backend's own English messages; the main way for other 1256: text to appear is an error from your own class. 1257: 1258: =back 1259: 1260: =head1 COMMON PITFALLS 1261: 1262: =over 4 1263: 1264: =item Class variables are shared by every client. 1265: 1266: Each frontend has its own object, but all objects of a class live in one 1267: process, so a package variable (C<our $COUNT>) or other class data is shared by 1268: all clients. A change made by one client is seen by all the others. Keep state 1269: in the object, not in the package. 1270: 1271: =item The backend is one process: it does one thing at a time. 1272: 1273: A slow method makes every other client wait, whatever class they use. 1274: 1275: =item The backend's environment is not yours. 1276: 1277: Setting C<$ENV{FOO}>, calling C<chdir>, or opening a file in your own process 1278: changes nothing in the backend. Pass what a method needs as arguments. 1279: 1280: =item Only plain data can cross. 1281: 1282: Strings, numbers, C<undef>, array references and hash references can be sent. 1283: Code references, file handles and tied or inside-out objects cannot. An 1284: argument that cannot be sent makes the call fail. In this version the failure 1285: is reported as "lost the backend" and it also closes the connection, so the 1286: object is gone. Check your arguments before the call. 1287: 1288: =item Context is kept, so list rules still apply. 1289: 1290: A method that returns C<(1, 2, 3)> gives the last value, 3, in scalar context. 1291: That is normal Perl, and it is the same through a frontend. 1292: 1293: =item C<undef> is a value. 1294: 1295: C<undef> is sent and returned faithfully. A method that returns C<undef> in list 1296: context gives a list of one element, not an empty list. 1297: 1298: =item C<ensure_dir> returns a pair; the other functions die. 1299: 1300: Test C<$ok>, the first value of the pair. The pair as a whole is never an empty 1301: list, so testing the list itself tells you nothing. 1302: 1303: =item C<ensure_dir> makes only one directory. 1304: 1305: If the parent does not exist, it fails with "No such file or directory". Create 1306: the parent first. 1307: 1308: =item An over-long socket path is refused. 1309: 1310: Keep C<dir> or C<socket> short (100 characters at most). 1311: 1312: =item A token file's trailing white space is removed. 1313: 1314: A secret whose last character is meant to be a space or a new line cannot be 1315: stored in a token file. Use ordinary printable characters. 1316: 1317: =item A unix socket takes no token and no TLS. 1318: 1319: Passing one is an error, on purpose, so that it never looks like extra 1320: protection. 1321: 1322: =back 1323: 1324: =head1 TRANSPORT SECURITY 1325: 1326: Authentication belongs to the transport, and the two transports are 1327: deliberately different. 1328: 1329: A unix socket is created mode 0600 in a directory only its owner can write to, 1330: so the kernel has already decided who may connect: there is no token and no TLS, 1331: and no frame carries a digest. The backend, the frontend and L<class-shared> 1332: refuse both for a unix socket rather than let either look like a defence. 1333: 1334: TCP is the opposite case. A port is reachable by anybody who can route to it, 1335: and treating a frame means running what the sender put in it, so a TCP listener 1336: is a TLS listener: it needs a certificate and its key to prove which backend it 1337: is, and a CA to check client certificates against, and it will not start without 1338: all three. Clients must present a certificate that CA signed, and must verify 1339: the backend's certificate against the CA that signed it -- name included -- or 1340: against a pinned SHA-256 fingerprint of that one certificate, which stands in 1341: for both the CA and the name. A client whose certificate the backend cannot 1342: verify is dropped during the handshake and never receives a frame. 1343: 1344: TLS answers who is on the other end and makes the bytes unreadable. The token is 1345: kept on top of it, as a second and independent check: with a token every frame 1346: in both directions carries an HMAC-SHA-256 of its body, and the digest is 1347: verified before L<Storable::thaw> sees the body. So a client that has a 1348: certificate but not the shared secret still cannot make the backend 1349: deserialise, and so run, bytes of its choosing. The digest authenticates one 1350: frame; TLS authenticates the connection and encrypts it. Both, because either 1351: one alone has been enough to lose a system. 1352: 1353: See L<Class::Shared::Backend/SECURITY> for the backend's side of it and 1354: L<Class::Shared::Protocol> for the framing. 1355: 1356: =head2 Making the certificates 1357: 1358: A TCP listener needs four files: a CA, the listener's certificate and key, and 1359: a certificate for every client. OpenSSL makes them in three steps. The CA's 1360: key signs certificates and is not needed on the machines that use them. 1361: 1362: # 1. A CA of your own. 1363: openssl req -x509 -newkey rsa:2048 -nodes -days 3650 \ 1364: -keyout ca-key.pem -out ca.pem -subj '/CN=Class::Shared CA' \ 1365: -addext 'basicConstraints=critical,CA:TRUE' \ 1366: -addext 'keyUsage=critical,keyCertSign,cRLSign' 1367: 1368: The listener's certificate has to name the address clients connect to, in 1369: C<subjectAltName>: a C<DNS:> entry for a hostname, an C<IP:> entry for an 1370: address, or both. 1371: 1372: # 2. The listener's certificate. Put your own addresses in server.ext. 1373: printf 'subjectAltName = DNS:classhost.example.com, IP:127.0.0.1\n' > server.ext 1374: printf 'extendedKeyUsage = serverAuth\n' >> server.ext 1375: openssl req -new -newkey rsa:2048 -nodes -subj '/CN=classhost.example.com' \ 1376: -keyout server-key.pem -out server.csr 1377: openssl x509 -req -in server.csr -CA ca.pem -CAkey ca-key.pem \ 1378: -CAcreateserial -days 3650 -out server.pem -extfile server.ext 1379: 1380: Each client needs a certificate of its own. The backend cannot tell one from 1381: another beyond this, so give each client, or each pair of them, their own. 1382: 1383: # 3. A client certificate. 1384: printf 'extendedKeyUsage = clientAuth\n' > client.ext 1385: openssl req -new -newkey rsa:2048 -nodes -subj '/CN=class-shared client' \ 1386: -keyout client-key.pem -out client.csr 1387: openssl x509 -req -in client.csr -CA ca.pem -CAkey ca-key.pem \ 1388: -CAcreateserial -days 3650 -out client.pem -extfile client.ext 1389: 1390: The listener is given three of the files: 1391: 1392: class-shared start --listen tcp:0.0.0.0:7000 \ 1393: --token-file /etc/class-shared/token \ 1394: --tls-cert server.pem --tls-key server-key.pem --tls-ca ca.pem 1395: 1396: and every client the other three: 1397: 1398: my $simple = Class::Shared::Frontend->new('Class::Simple', 1399: host => 'classhost.example.com', 1400: port => 7000, 1401: tls_ca => 'ca.pem', 1402: tls_cert => 'client.pem', 1403: tls_key => 'client-key.pem', 1404: token_file => '/etc/class-shared/token', 1405: ); 1406: 1407: A frontend that pins the listener's certificate instead of trusting the CA skips 1408: C<tls_ca> and passes C<tls_fingerprint> the digest that 1409: C<openssl x509 -in server.pem -noout -fingerprint -sha256> prints. That works 1410: with a self-signed certificate too, and then the CA is only needed for the 1411: clients. F<t/certs/make-certs.sh> in the distribution generates this set, plus 1412: a second CA and impostor certificates, for the tests. 1413: 1414: =head1 LIMITATIONS 1415: 1416: =over 4 1417: 1418: =item * 1419: 1420: A unix socket is not encrypted: file permissions decide who may connect, and 1421: anything that can read the socket can read the traffic on it. TCP is always 1422: TLS, and has no unencrypted mode. 1423: 1424: =item * 1425: 1426: One backend per directory answers one call at a time, so a slow method delays 1427: every client of that backend, whatever class they are using. 1428: 1429: =item * 1430: 1431: Each frontend is one connection: many objects in one process means many file 1432: descriptors in both processes. 1433: 1434: =item * 1435: 1436: Arguments and return values travel through L<Storable>; filehandles, code 1437: references and tied or inside-out objects do not. 1438: 1439: =item * 1440: 1441: An object cannot outlive its connection, so it cannot be handed to another 1442: process; it lives exactly as long as the frontend that made it. 1443: 1444: =item * 1445: 1446: The backend is a separate process, so nothing about your process reaches it 1447: unless you send it. Setting C<$ENV{FOO}> in a frontend does not set it in the 1448: backend, and a method that reads C<%ENV> (or C<chdir>, or a global, or a file 1449: handle) sees the backend's environment, not yours: its C<%ENV> is the one the 1450: backend was started with, and every client shares those values. Pass such 1451: state as arguments if it matters, or set the environment when you start the 1452: backend. 1453: 1454: =item * 1455: 1456: A client may call a method only by its plain name. A package-qualified name 1457: such as C<Other::Package::function> is refused, and so are C<new>, C<DESTROY> 1458: and C<AUTOLOAD>. This is not a sandbox: a client that has passed the 1459: transport's authentication can already make the backend load any module and 1460: call the methods of what it constructs, so the backend is only as safe as the 1461: clients allowed to reach it. 1462: 1463: =back 1464: 1465: =head1 PLATFORMS 1466: 1467: Unix-like systems only. A backend needs three things this distribution cannot 1468: do without: 1469: 1470: =over 4 1471: 1472: =item * 1473: 1474: unix domain sockets, which is how a frontend reaches a backend on this machine 1475: and starts one when there is none; 1476: 1477: =item * 1478: 1479: POSIX directory permissions, which are the whole of a unix socket's 1480: authentication: the socket is mode 0600 inside a directory only its owner can 1481: write to, so the kernel decides who may connect; 1482: 1483: =item * 1484: 1485: a real C<fork>, which is how a frontend starts a backend in the background. 1486: 1487: =back 1488: 1489: A perl without unix domain sockets can run none of it, so that is the question 1490: the check asks, rather than a list of operating systems: a Cygwin perl has the 1491: sockets and works. Windows has had the sockets since Windows 10, but an 1492: MSWin32 perl still cannot run a backend: its C<stat> reports the same mode bits 1493: for every directory, so none there can be shown to be owner-only and 1494: C<Class::Shared::ensure_dir> refuses them all. 1495: 1496: The refusal names what is missing: C<Class::Shared::ensure_dir> and 1497: C<Class::Shared::socket_path> die with that explanation on a perl without the 1498: sockets, and C<Class::Shared::ensure_dir> refuses the directories it cannot 1499: show to be owner-only. 1500: 1501: It installs there anyway, and F<Makefile.PL> says so as it runs. Refusing to 1502: install would take away L<Class::Shared::Protocol>, which is pure Perl and can 1503: talk to a backend from a machine that cannot run one, and would leave such a 1504: platform with nothing to report but a failed install. 1505: 1506: The wire protocol, L<Class::Shared::Protocol>, is pure Perl and loads on any 1507: platform, so it can be used to talk to a backend from a machine that cannot run 1508: one. 1509: 1510: =head1 SEE ALSO 1511: 1512: =over 4 1513: 1514: =item * L<Test Dashboard|https://nigelhorne.github.io/Class-Shared/coverage/> 1515: 1516: =back 1517: 1518: L<Class::Shared::Frontend>, L<Class::Shared::Backend>, 1519: L<Class::Shared::Protocol>, L<class-shared>. 1520: 1521: =head1 HACKING 1522: 1523: F<README.md> is generated from the POD in this file and must never be edited 1524: by hand. After changing the POD, run C<make readme>, which calls 1525: F<maint/regen-readme>; it converts the POD with L<Pod::Github>'s 1526: C<pod2github>, the converter whose C<##> headings, fenced Perl blocks and 1527: MetaCPAN links are the style the committed front page uses. C<pod2markdown> 1528: does not reproduce it. 1529: 1530: L<Pod::Github> is a development requirement, so C<cpanm --installdeps 1531: --with-develop .> installs everything needed to regenerate the front page. 1532: 1533: =head1 AUTHOR 1534: 1535: Nigel Horne, C<< <njh at nigelhorne.com> >> 1536: 1537: =encoding utf8 1538: 1539: =head1 FORMAL SPECIFICATION 1540: 1541: This section describes what each function does in the Z notation, a way of 1542: writing exactly what a program promises using sets and logic. You do not need 1543: it to use the module. Read the plain text above first. 1544: 1545: Z uses mathematical symbols, so this is the only part of the file that is not 1546: plain ASCII. A short key: 1547: 1548: x? input x! output x' the value of x after the operation 1549: => implies /\ and \/ or ~ not 1550: |-> maps to <-> relation +-> partial function (each input at most one answer) 1551: seq a sequence (an ordered list) # the length of a sequence 1552: 1553: In the Z text below the symbols are the usual Unicode ones. 1554: 1555: =head2 Basic types 1556: 1557: [CHAR] all characters 1558: STRING == seq CHAR 1559: STRING⥠== STRING ⪠{â¥} a string, or undef 1560: PATH == STRING 1561: MAXPATH == 100 1562: RESULT[X] ::= valueâ¨â¨Xâ©â© | diesâ¨â¨STRINGâ©â© a function returns X, or dies with a message 1563: IDENT == the strings that start with a letter or _ and continue with letters, digits or _ 1564: CLASSNAME == { c : STRING | â parts : seqâ IDENT ⢠c = join(â¨"::"â©, parts) } 1565: HEX64 == the strings of exactly 64 hexadecimal digits 1566: FP == { f : STRING | f â HEX64 ⨠f is 32 pairs of hex digits separated by ":" } 1567: 1568: =head2 validate_class 1569: 1570: ââ ValidateClass âââââââââââââââââââââââââââââââââââââââââ 1571: â class? : STRING⥠1572: â out! : RESULT[STRING] 1573: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1574: â class? â CLASSNAME â out! = value(class?) 1575: â class? â CLASSNAME â â m : STRING ⢠out! = dies(m) 1576: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1577: 1578: =head2 read_secret 1579: 1580: ââ ReadSecret ââââââââââââââââââââââââââââââââââââââââââââ 1581: â path? : STRING⥠1582: â files : PATH ⸠STRING the file system, as bytes 1583: â out! : RESULT[STRING] 1584: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1585: â path? â dom files â§ trim(files(path?)) â â¨â© 1586: â â out! = value(trim(files(path?))) 1587: â path? â dom files ⨠trim(files(path?)) = â¨â© ⨠path? = ⥠1588: â â â m : STRING ⢠out! = dies(m) 1589: â files' = files nothing is changed 1590: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1591: 1592: where C<trim> removes white space from the end only. 1593: 1594: =head2 normalise_fingerprint 1595: 1596: ââ NormaliseFingerprint ââââââââââââââââââââââââââââââââââ 1597: â value? : STRING⥠1598: â out! : RESULT[STRING] 1599: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1600: â value? = â¨"sha256$"⩠⢠f ⨠value? = f, for some f â FP 1601: â â out! = value(â¨"sha256$"⩠⢠lower(strip_colons(f))) 1602: â otherwise â â m : STRING ⢠out! = dies(m) 1603: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1604: 1605: =head2 client_tls_options 1606: 1607: ââ ClientTlsOptions ââââââââââââââââââââââââââââââââââââââ 1608: â cert?, key? : STRING 1609: â ca?, fingerprint? : STRING⥠1610: â out! : RESULT[seq (STRING à VALUE)] 1611: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1612: â fingerprint? â ⥠â out! = value(base ⢠pin) 1613: â fingerprint? = ⥠â out! = value(base ⢠â¨("SSL_ca_file", ca?)â©) 1614: â SSL_verify_mode â base is always "verify the peer" 1615: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1616: 1617: =head2 dir_of 1618: 1619: ââ DirOf âââââââââââââââââââââââââââââââââââââââââââââââââ 1620: â path? : STRING 1621: â dir! : STRING 1622: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1623: â dir! = dirname(path?) ("." when path? has no directory part) 1624: â dir! â â¨â© 1625: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1626: 1627: =head2 default_dir 1628: 1629: ââ DefaultDir ââââââââââââââââââââââââââââââââââââââââââââ 1630: â dir? : STRING⥠; env : STRING ⸠STRING ; isdir : â PATH ; euid : â 1631: â dir! : STRING 1632: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1633: â dir? â ⥠⧠dir? â â¨â© â dir! = dir? 1634: â else "CLASS_SHARED_DIR" â dom env â§ env(..) â â¨â© â dir! = env("CLASS_SHARED_DIR") 1635: â else env("XDG_RUNTIME_DIR") â isdir â dir! = env("XDG_RUNTIME_DIR") / "class-shared" 1636: â else "/run/user/" ⢠euid â isdir â dir! = that / "class-shared" 1637: â else dir! = tmpdir / ("class-shared-" ⢠euid) 1638: â isdir' = isdir nothing is created 1639: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1640: 1641: =head2 ensure_dir 1642: 1643: ââ EnsureDir âââââââââââââââââââââââââââââââââââââââââââââ 1644: â dir? : STRING⥠1645: â fs : PATH ⸠(owner : â à mode : â) the directories that exist 1646: â euid : â 1647: â out! : RESULT[ð¹ à STRINGâ¥] 1648: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1649: â dir? = ⥠⨠dir? = â¨â© ⨠¬ UnixSockets â â m ⢠out! = dies(m) 1650: â dir? â dom fs â§ parent(dir?) â dom fs â fs' = fs â {dir? ⦠(euid, 8#700)} 1651: â dir? â dom fs' â§ owner(fs'(dir?)) = euid â§ mode(fs'(dir?)) â§ 8#022 = 0 1652: â â out! = value(true, â¥) 1653: â otherwise (and no dying) â â why : STRING ⢠out! = value(false, why) 1654: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1655: 1656: =head2 socket_path 1657: 1658: ââ SocketPath ââââââââââââââââââââââââââââââââââââââââââââ 1659: â dir? : STRING⥠1660: â out! : RESULT[PATH] 1661: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1662: â ¬ UnixSockets â â m ⢠out! = dies(m) 1663: â let p = default_dir(dir?) / "class-shared.sock" in 1664: â UnixSockets â§ #p ⤠MAXPATH â out! = value(p) 1665: â UnixSockets â§ #p > MAXPATH â â m ⢠out! = dies(m) 1666: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1667: 1668: =head2 check_socket_path 1669: 1670: ââ CheckSocketPath âââââââââââââââââââââââââââââââââââââââ 1671: â path? : STRING⥠1672: â out! : RESULT[PATH] 1673: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1674: â path? â ⥠⧠0 < #path? ⤠MAXPATH â out! = value(path?) 1675: â path? = ⥠⨠#path? = 0 â â m ⢠out! = dies(m) 1676: â path? â ⥠⧠#path? > MAXPATH â â m ⢠out! = dies(m) 1677: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1678: 1679: =head2 poll_pause 1680: 1681: ââ PollPause âââââââââââââââââââââââââââââââââââââââââââââ 1682: â clock, clock' : â 1683: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1684: â clock' ⥠clock + POLL_INTERVAL it returns nothing 1685: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1686: 1687: =head2 detach_standard_streams 1688: 1689: ââ DetachStandardStreams âââââââââââââââââââââââââââââââââ 1690: â log? : PATH ; stdin, stdout, stderr : STREAM ; writable : â PATH 1691: â out! : ð¹ à STRING⥠1692: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1693: â stdin' = null 1694: â log? â writable â stdout' = stderr' = file(log?) â§ out! = (true, â¥) 1695: â log? â writable â stdout' = stderr' = null â§ out! = (true, â¥) 1696: â any reopen fails â â why ⢠out! = (false, why) 1697: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1698: 1699: =head2 The unix socket probe (internal) 1700: 1701: ââ UnixSocketProbe âââââââââââââââââââââââââââââââââââââââ 1702: â cache, cache' : {unknown, yes, no} 1703: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1704: â cache = unknown â cache' â {yes, no} asked once, by naming a socket and 1705: â trying socketpair; both must work 1706: â cache â unknown â cache' = cache the answer never changes in a run 1707: ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 1708: 1709: =head1 STATE DIAGRAM 1710: 1711: The functions in this file keep almost no state. The one piece they do keep is 1712: the answer to "can this perl make a unix socket?", which is worked out once, 1713: the first time it is needed, and then never changes. 1714: 1715: both tries work 1716: +---------+ -------------------------> +---------+ <---+ 1717: | UNKNOWN | | UNIX_OK | | every later call 1718: +---------+ ---+ +---------+ ----+ (no change) 1719: | try fails 1720: | +---------+ <---+ 1721: +---------------------> | NO_UNIX | | every later call 1722: +---------+ ----+ (calls die) 1723: 1724: A directory, as seen by L</ensure_dir>, moves through these states. The 1725: function does not remember them; they describe what it finds and what it does. 1726: 1727: +---------+ mkdir 0700 works +---------+ 1728: | MISSING | --------------------> | PRESENT | (it existed, or was just made) 1729: +---------+ +---------+ 1730: | | | 1731: | mkdir fails | | I own it, and group and 1732: | (parent missing, no | | other cannot write to it 1733: | permission, ...) | v 1734: | | +------+ 1735: | | | SAFE | returns (1, undef) 1736: | | +------+ 1737: | | someone else owns it, or 1738: | | group or other can write to it 1739: v v 1740: +------------------------------------+ 1741: | REFUSED returns (0, $why) | 1742: +------------------------------------+ 1743: 1744: =over 4 1745: 1746: =item UNKNOWN to UNIX_OK or NO_UNIX 1747: 1748: Trigger: the first call to C<socket_path> or C<ensure_dir> (or the check in 1749: F<Makefile.PL>). Action: try to name a unix socket (C<pack_sockaddr_un>) and to 1750: make a pair of connected ones (C<socketpair>). The 1751: answer is stored in C<$Class::Shared::UNIX_SOCKETS> and is never asked again. 1752: 1753: =item UNIX_OK, no change 1754: 1755: Trigger: any later call. Action: none; C<socket_path> and C<ensure_dir> work as 1756: described above. 1757: 1758: =item NO_UNIX, no change 1759: 1760: Trigger: any later call to C<socket_path> or C<ensure_dir>. Action: the call 1761: dies with a message saying that this perl has no unix sockets. 1762: 1763: =item MISSING to PRESENT 1764: 1765: Trigger: C<ensure_dir> on a directory that does not exist but whose parent does. 1766: Action: C<mkdir> with mode 0700. 1767: 1768: =item MISSING to REFUSED 1769: 1770: Trigger: the C<mkdir> fails, for example because the parent does not exist. 1771: Action: return C<(0, $reason)>. 1772: 1773: =item PRESENT to SAFE 1774: 1775: Trigger: C<ensure_dir> finds that this user owns the directory and that group 1776: and other users cannot write to it. Action: return C<(1, undef)>. 1777: 1778: =item PRESENT to REFUSED 1779: 1780: Trigger: C<ensure_dir> finds a different owner, or that group or other users can 1781: write to it. Action: return C<(0, $reason)>. 1782: 1783: =back 1784: 1785: =head1 LICENCE AND COPYRIGHT 1786: 1787: Copyright 2026 Nigel Horne. 1788: 1789: Class::Shared is free software; you can redistribute it and/or modify it under 1790: the terms of the GNU General Public License, version 2, as published by the 1791: Free Software Foundation. 1792: 1793: Class::Shared is distributed in the hope that it will be useful, but WITHOUT 1794: ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 1795: FOR A PARTICULAR PURPOSE. See the F<LICENSE> file for the full text. 1796: 1797: If you use it, please let me know. 1798: 1799: =cut 1800: 1801: 1;