Class-Shared

 view release on metacpan or  search on metacpan

lib/Class/Shared.pm  view on Meta::CPAN

		# process about 1.2 MB, and the backend and the command load it
		# themselves when they need it.
		require Socket;

		Socket::pack_sockaddr_un('probe');

		socketpair(my $reader, my $writer, Socket::AF_UNIX(), Socket::SOCK_STREAM(), 0)
			? 1
			: 0;
	} ? 1 : 0;

	return $UNIX_SOCKETS;
}

# Nothing here can work without unix domain sockets, so say that where it is
# tried rather than blaming the directory the caller passed: on a platform
# without them there is no directory that would have helped.  Whether they
# exist can be given as an argument, so the refusal is exercised by the suite
# on machines that have them.
sub _require_unix_sockets {
	my ($what, $has_unix_sockets) = @_;

	$has_unix_sockets = _has_unix_sockets() unless defined $has_unix_sockets;

	return if $has_unix_sockets;

	die "Class::Shared: $what is not available here: this perl has no unix "
		. "domain sockets, so a backend could neither be reached nor started.  "
		. "This distribution is Unix-only (see PLATFORMS)\n";
}

# Create $dir if needed and make sure nobody else can drop a socket in it.
# Returns (1, undef), or (0, $why_not), as the POD documents.
sub ensure_dir {
	my ($dir) = @_;

	die "Class::Shared: no directory given\n" unless defined $dir && length $dir;

	_require_unix_sockets('a backend directory');

	if (!-d $dir) {
		# Losing a race to make it is not a failure; what the winner made is
		# checked below like any directory that was already there.
		if (!mkdir($dir, 0700)) {
			my $why = "$!";
			return (0, "cannot create $dir: $why") unless -d $dir;
		}
	}

	return _check_dir($dir);
}

# Purpose:       say whether $dir is somewhere only this user can put a socket.
# Entry:         $dir is a directory name; nothing is created.
# Exit status:   (1, undef) when it is ours and not writable by group or
#                others, otherwise (0, $why_not).
# Side effects:  none.
sub _check_dir {
	my ($dir) = @_;

	my @stat = stat($dir);
	return (0, "cannot stat $dir: $!") unless @stat;

	return (0, "$dir is owned by uid $stat[4], not by uid $>")
		if $stat[4] != $>;

	return (0, "$dir is writable by group or others; refusing to use it")
		if $stat[2] & 0022;

	return (1, undef);
}

# Purpose:       decide whether to talk to the socket at $path.  What comes back
#                over it is thawed by Storable, so a socket made by another user
#                must never be trusted: it could answer with whatever it likes,
#                and would be told every argument the client sends.
# Entry:         $path names a socket that is known to exist.
# Exit status:   (1, undef) when its directory is safe (see _check_dir) and the
#                socket is a socket owned by this user, otherwise (0, $why_not).
# Side effects:  none.
# Why both:      a safe directory means nobody else could have put the socket
#                there; the owner check catches a socket that was already there.
sub _check_endpoint {
	my ($path) = @_;

	my ($safe, $why) = _check_dir(dir_of($path));
	return (0, $why) unless $safe;

	my @stat = stat($path);
	return (0, "cannot stat $path: $!") unless @stat;

	return (0, "$path is not a socket") unless -S _;

	return (0, "$path is owned by uid $stat[4], not by uid $>")
		if $stat[4] != $>;

	return (1, undef);
}

# One socket for every class.  The backend behind it is told which class a
# client wants when that client connects, so the path names no class and one
# backend can serve as many as are asked for.
sub socket_path {
	my ($dir) = @_;

	_require_unix_sockets('a unix socket');

	$dir = default_dir(dir => $dir);

	return check_socket_path(File::Spec->catfile($dir, 'class-shared.sock'));
}

# A path that will not fit in a sockaddr_un is silently truncated by the
# operating system, so a client and a backend could end up on different
# sockets.  Refuse it instead.  Applies to a path given explicitly as well as
# one socket_path built.
sub check_socket_path {
	my ($path) = @_;

	die "Class::Shared: no socket path given\n" unless defined $path && length $path;

	if (length($path) > $MAX_SOCKET_PATH) {
		die sprintf("Class::Shared: socket path '%s' is %d characters, more than the %d allowed; "
			. "set CLASS_SHARED_DIR or pass dir => to a shorter directory\n",
			$path, length($path), $MAX_SOCKET_PATH);
	}

	return $path;
}

1;

__END__

=head2 normalise_fingerprint

    my $pin = Class::Shared::normalise_fingerprint(
        'AB:CD:...:EF'    # 32 pairs of hex digits, as openssl prints them
    );

Turns the SHA-256 fingerprint of a certificate into the one form the TLS layer
wants: C<sha256$> followed by 64 lower case hexadecimal digits.

It accepts 64 hex digits, or 32 pairs of hex digits separated by colons, with or
without a leading C<sha256$>.  Upper or lower case digits are fine.  The prefix
itself must be lower case.  To get a fingerprint from a certificate file, run
C<openssl x509 -in server.pem -noout -fingerprint -sha256> and copy the part
after the equals sign.



( run in 0.886 second using v1.01-cache-2.11-cpan-eb134014566 )