| 1 |
root |
1.1 |
=head1 NAME |
| 2 |
|
|
|
| 3 |
|
|
AnyEvent::MCP::Config - named servers and derived MCP credentials |
| 4 |
|
|
|
| 5 |
|
|
=head1 SYNOPSIS |
| 6 |
|
|
|
| 7 |
|
|
my $config = AnyEvent::MCP::Config->global; |
| 8 |
|
|
my $password = $config->credential ("basic", "agni", "alice"); |
| 9 |
|
|
my $url = $config->url ("agni"); |
| 10 |
|
|
|
| 11 |
|
|
=head1 DESCRIPTION |
| 12 |
|
|
|
| 13 |
root |
1.3 |
An MCP server and its clients often need matching credentials. Rather than |
| 14 |
|
|
maintaining a separate password for each server and user, this module can |
| 15 |
|
|
derive them from one private master secret. A server name, optional username |
| 16 |
|
|
and credential purpose each contribute to the result. A password derived for |
| 17 |
|
|
one server is therefore not the password for another. |
| 18 |
|
|
|
| 19 |
|
|
The shared file format is described in L<AnyEvent::MCP/GLOBAL CONFIGURATION>. |
| 20 |
|
|
Loading this module does not read any files. The first call to C<global> |
| 21 |
|
|
reads the configuration, ensures a master secret exists and caches the object |
| 22 |
|
|
for the process. |
| 23 |
root |
1.1 |
|
| 24 |
|
|
A missing secret is generated using C<AnyEvent::MCP::random_bytes> and saved |
| 25 |
|
|
when possible. Saving failure produces a warning; the generated secret remains |
| 26 |
root |
1.3 |
usable in this process, but another process might derive different credentials. |
| 27 |
|
|
A supplied invalid secret is an error. Changing or losing the master secret |
| 28 |
|
|
invalidates all credentials derived from it. |
| 29 |
root |
1.1 |
|
| 30 |
|
|
Derivation uses HKDF-SHA512 expansion with the random master key as its PRK. |
| 31 |
|
|
Server name, username and credential purpose are separately length-prefixed, |
| 32 |
|
|
so credentials for different servers, users or authentication methods differ. |
| 33 |
root |
1.3 |
Deriving a password does not create an account or configure the server to |
| 34 |
|
|
accept it: authentication still belongs to the server. |
| 35 |
root |
1.1 |
|
| 36 |
|
|
=head1 METHODS |
| 37 |
|
|
|
| 38 |
|
|
=head2 $config = AnyEvent::MCP::Config->global (file => $file) |
| 39 |
|
|
|
| 40 |
|
|
Returns the cached global configuration. The first call accepts the same |
| 41 |
|
|
arguments as C<new>; later calls return the same object without rereading |
| 42 |
|
|
the file. Call this explicitly during program startup. |
| 43 |
|
|
|
| 44 |
|
|
=head2 $config = AnyEvent::MCP::Config->new (file => $file) |
| 45 |
|
|
|
| 46 |
|
|
Reads C<$file>, or the default file when omitted, and ensures a master |
| 47 |
|
|
secret exists. Unlike C<global>, each call creates an independent object. |
| 48 |
|
|
A missing file is allowed; other read errors are fatal. Sections are available |
| 49 |
|
|
as C<< $config->{section}{$section} >>, each a hash reference of string |
| 50 |
|
|
values. Named servers use C<[server NAME]>. |
| 51 |
|
|
|
| 52 |
|
|
=head2 $config = AnyEvent::MCP::Config->init (file => $file) |
| 53 |
|
|
|
| 54 |
|
|
Explicitly creates the configuration file with a random secret, mode 0600. |
| 55 |
|
|
Creates missing parent directories with mode 0700. Fails if the file already |
| 56 |
|
|
exists, so it cannot accidentally replace an existing secret. |
| 57 |
|
|
|
| 58 |
|
|
=head2 $credential = $config->credential ($purpose, $name, $username) |
| 59 |
|
|
|
| 60 |
|
|
Derives a 32-byte credential, returned as unpadded URL-safe Base64. |
| 61 |
|
|
C<$purpose> is C<basic>, C<bearer> or C<cookie>. C<$name> is the server |
| 62 |
|
|
name. C<$username> can be omitted for a server-wide credential. |
| 63 |
|
|
|
| 64 |
|
|
This method does not require a corresponding server section. |
| 65 |
|
|
|
| 66 |
|
|
=head2 $url = $config->url ($name, $username) |
| 67 |
|
|
|
| 68 |
|
|
Returns the configured C<url> for C<$name>, replacing C<{cookie}> with the |
| 69 |
|
|
derived cookie credential. C<$username> can be omitted. |
| 70 |
|
|
|
| 71 |
|
|
The template supplies scheme, host, port, path and cookie placement explicitly. |
| 72 |
|
|
This method neither contacts a server nor infers an ephemeral listening port. |
| 73 |
|
|
The HTTP server does not consume this configuration automatically. |
| 74 |
|
|
|
| 75 |
root |
1.4 |
=head2 $name = AnyEvent::MCP::Config::server_name ($name) |
| 76 |
|
|
|
| 77 |
|
|
Validate a service-instance name, or use the program basename if undefined. |
| 78 |
|
|
Names consist of an ASCII letter or underscore followed by letters, digits, |
| 79 |
|
|
underscores or hyphens. |
| 80 |
|
|
|
| 81 |
|
|
=head2 @options = AnyEvent::MCP::Config::http_getopt (\$name, \$bind, \$print_url) |
| 82 |
|
|
|
| 83 |
|
|
Return standard C<Getopt::Long> options referring to the caller's scalars. |
| 84 |
|
|
|
| 85 |
|
|
=head2 $port = $config->server_port ($name) |
| 86 |
|
|
|
| 87 |
|
|
Derive a stable default port in 16384..32767 from the master secret and |
| 88 |
|
|
service-instance name. Collisions produce ordinary listener bind errors. |
| 89 |
|
|
|
| 90 |
|
|
=head2 %settings = $config->http_settings ($name, $bind) |
| 91 |
|
|
|
| 92 |
|
|
Resolve HTTP settings from C<[server NAME]> and an optional command-line |
| 93 |
|
|
C<bind> override. The default is IPv4 loopback and the name-derived port. |
| 94 |
|
|
The bind setting accepts C<ADDRESS:PORT>, C<[IPv6]:PORT>, C<ADDRESS>, |
| 95 |
|
|
and C<:PORT> (which keeps the default loopback address). |
| 96 |
|
|
Hostnames are resolved once to a numeric address at startup; their original |
| 97 |
|
|
spelling is advertised unless C<url_host> overrides it. |
| 98 |
|
|
|
| 99 |
|
|
=head2 $url = $config->http_url ($name, $bind, %options) |
| 100 |
|
|
|
| 101 |
|
|
Format the cookie URL without opening a listener. Port zero cannot be used |
| 102 |
|
|
because the kernel has not yet selected a port. |
| 103 |
|
|
|
| 104 |
root |
1.1 |
=cut |
| 105 |
|
|
|
| 106 |
|
|
package AnyEvent::MCP::Config; |
| 107 |
|
|
|
| 108 |
|
|
use common::sense; |
| 109 |
|
|
use AnyEvent::MCP (); |
| 110 |
|
|
use MIME::Base64 (); |
| 111 |
|
|
use File::Basename (); |
| 112 |
|
|
use File::Path (); |
| 113 |
|
|
use Fcntl qw(O_WRONLY O_RDWR O_CREAT O_EXCL LOCK_EX); |
| 114 |
|
|
use Errno qw(ENOENT); |
| 115 |
root |
1.4 |
use AnyEvent::Socket (); |
| 116 |
|
|
use Socket (); |
| 117 |
root |
1.1 |
|
| 118 |
|
|
our @ISA = qw(AnyEvent::MCP::Base); |
| 119 |
|
|
|
| 120 |
|
|
sub _file { |
| 121 |
|
|
my $dir = $ENV{XDG_CONFIG_HOME} |
| 122 |
|
|
|| ($ENV{HOME} || die "HOME is not set\n") . "/.config"; |
| 123 |
|
|
|
| 124 |
|
|
"$dir/anyevent-mcp/global.ini" |
| 125 |
|
|
} |
| 126 |
|
|
|
| 127 |
|
|
my $global; |
| 128 |
|
|
|
| 129 |
|
|
sub global { |
| 130 |
|
|
my $class = shift; |
| 131 |
|
|
|
| 132 |
|
|
$global ||= $class->new (@_) |
| 133 |
|
|
} |
| 134 |
|
|
|
| 135 |
|
|
sub new { |
| 136 |
|
|
my ($class, %kv) = @_; |
| 137 |
|
|
my $self = $class->SUPER::new (section => {}, %kv); |
| 138 |
|
|
$self->{file} = _file unless exists $self->{file}; |
| 139 |
|
|
|
| 140 |
|
|
if (open my $fh, "<:encoding(UTF-8)", $self->{file}) { |
| 141 |
|
|
$self->_read ($fh); |
| 142 |
|
|
close $fh or die "$self->{file}: $!\n"; |
| 143 |
|
|
} else { |
| 144 |
|
|
die "$self->{file}: $!\n" unless $! == ENOENT; |
| 145 |
|
|
} |
| 146 |
|
|
|
| 147 |
|
|
$self->_ensure_secret; |
| 148 |
|
|
|
| 149 |
|
|
die "$self->{file}: invalid secret; supply 128 hexadecimal digits\n" |
| 150 |
|
|
unless $self->{section}{global}{secret} =~ /\A[0-9a-fA-F]{128}\z/; |
| 151 |
|
|
|
| 152 |
|
|
$self |
| 153 |
|
|
} |
| 154 |
|
|
|
| 155 |
|
|
sub _read { |
| 156 |
|
|
my ($self, $fh) = @_; |
| 157 |
|
|
|
| 158 |
|
|
my @stat = stat $fh; |
| 159 |
|
|
die "$self->{file}: configuration must be private and owned by this user\n" |
| 160 |
|
|
if $stat[4] != $< || $stat[2] & 077; |
| 161 |
|
|
|
| 162 |
|
|
my $section = $self->{section}{global} ||= {}; |
| 163 |
|
|
|
| 164 |
|
|
while (<$fh>) { |
| 165 |
|
|
next if /\A\s*(?:[#;]|\z)/; |
| 166 |
|
|
|
| 167 |
|
|
if (/\A\s*\[([^\]\r\n]+)\]\s*\z/) { |
| 168 |
|
|
$section = $self->{section}{$1} ||= {}; |
| 169 |
|
|
} elsif (/\A\s*([^=\s]+)\s*=\s*(.*?)\s*\z/) { |
| 170 |
|
|
$section->{$1} = $2; |
| 171 |
|
|
} else { |
| 172 |
|
|
die "$self->{file}:$.: invalid configuration line\n"; |
| 173 |
|
|
} |
| 174 |
|
|
} |
| 175 |
|
|
|
| 176 |
|
|
} |
| 177 |
|
|
|
| 178 |
|
|
sub _ensure_secret { |
| 179 |
|
|
my ($self) = @_; |
| 180 |
|
|
|
| 181 |
|
|
return if exists $self->{section}{global}{secret}; |
| 182 |
|
|
|
| 183 |
|
|
my $secret = eval { unpack "H*", AnyEvent::MCP::random_bytes 64 }; |
| 184 |
|
|
die "$@Supply a securely generated 128-digit hexadecimal secret in $self->{file}\n" |
| 185 |
|
|
if $@; |
| 186 |
|
|
|
| 187 |
|
|
eval { |
| 188 |
|
|
File::Path::mkpath (File::Basename::dirname ($self->{file}), 0, 0700); |
| 189 |
|
|
sysopen my $fh, $self->{file}, O_RDWR | O_CREAT, 0600 |
| 190 |
|
|
or die "$self->{file}: $!\n"; |
| 191 |
|
|
flock $fh, LOCK_EX or die "$self->{file}: $!\n"; |
| 192 |
|
|
binmode $fh, ":encoding(UTF-8)"; |
| 193 |
|
|
|
| 194 |
|
|
my $disk = bless { file => $self->{file}, section => {} }, ref $self; |
| 195 |
|
|
$disk->_read ($fh); |
| 196 |
|
|
|
| 197 |
|
|
unless (exists $disk->{section}{global}{secret}) { |
| 198 |
|
|
seek $fh, 0, 2 or die "$self->{file}: $!\n"; |
| 199 |
|
|
print {$fh} "\n[global]\nsecret = $secret\n" |
| 200 |
|
|
or die "$self->{file}: $!\n"; |
| 201 |
|
|
$disk->{section}{global}{secret} = $secret; |
| 202 |
|
|
} |
| 203 |
|
|
|
| 204 |
|
|
close $fh or die "$self->{file}: $!\n"; |
| 205 |
|
|
$self->{section} = $disk->{section}; |
| 206 |
|
|
1 |
| 207 |
|
|
} or warn "Cannot save MCP master secret; credentials may only be stable for this process: $@"; |
| 208 |
|
|
|
| 209 |
|
|
$self->{section}{global}{secret} = $secret |
| 210 |
|
|
unless exists $self->{section}{global}{secret}; |
| 211 |
|
|
} |
| 212 |
|
|
|
| 213 |
|
|
sub init { |
| 214 |
|
|
my ($class, %kv) = @_; |
| 215 |
|
|
my $file = exists $kv{file} ? $kv{file} : _file; |
| 216 |
|
|
|
| 217 |
|
|
my $secret = AnyEvent::MCP::random_bytes 64; |
| 218 |
|
|
|
| 219 |
|
|
File::Path::mkpath (File::Basename::dirname ($file), 0, 0700); |
| 220 |
|
|
|
| 221 |
|
|
sysopen my $fh, $file, O_WRONLY | O_CREAT | O_EXCL, 0600 |
| 222 |
|
|
or die "$file: $!\n"; |
| 223 |
|
|
|
| 224 |
|
|
print {$fh} "[global]\nsecret = ", unpack ("H*", $secret), "\n" |
| 225 |
|
|
or die "$file: $!\n"; |
| 226 |
|
|
close $fh or die "$file: $!\n"; |
| 227 |
|
|
|
| 228 |
|
|
$class->new (file => $file) |
| 229 |
|
|
} |
| 230 |
|
|
|
| 231 |
|
|
sub credential { |
| 232 |
|
|
my ($self, $purpose, $name, $username) = @_; |
| 233 |
|
|
|
| 234 |
|
|
$username = "" unless defined $username; |
| 235 |
|
|
|
| 236 |
|
|
die "Unknown credential purpose '$purpose'\n" |
| 237 |
|
|
unless $purpose =~ /\A(?:basic|bearer|cookie)\z/; |
| 238 |
|
|
|
| 239 |
|
|
my $secret = $self->{section}{global}{secret}; |
| 240 |
|
|
die "$self->{file}: missing or invalid secret\n" |
| 241 |
|
|
unless $secret =~ /\A[0-9a-fA-F]{128}\z/; |
| 242 |
|
|
|
| 243 |
|
|
my $info = pack "N/a*N/a*N/a*N/a*", |
| 244 |
|
|
"AnyEvent::MCP credential v1", $purpose, |
| 245 |
root |
1.2 |
AnyEvent::MCP::utf8enc ($name), AnyEvent::MCP::utf8enc ($username); |
| 246 |
root |
1.1 |
|
| 247 |
|
|
my $credential = MIME::Base64::encode_base64 ( |
| 248 |
|
|
AnyEvent::MCP::hkdf_sha512_expand (pack ("H*", $secret), $info, 32), "" |
| 249 |
|
|
); |
| 250 |
|
|
$credential =~ y!+/=!-_!d; |
| 251 |
|
|
|
| 252 |
|
|
$credential |
| 253 |
|
|
} |
| 254 |
|
|
|
| 255 |
root |
1.4 |
sub server_name { |
| 256 |
|
|
my ($name) = @_; |
| 257 |
|
|
|
| 258 |
|
|
unless (defined $name) { |
| 259 |
|
|
require File::Basename; |
| 260 |
|
|
$name = File::Basename::basename ($0); |
| 261 |
|
|
$name =~ s/\.(?:pl|perl)\z//; |
| 262 |
|
|
$name = "perl" if $name eq "-e"; |
| 263 |
|
|
} |
| 264 |
|
|
|
| 265 |
|
|
$name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/ |
| 266 |
|
|
or die "Invalid MCP server name '$name'\n"; |
| 267 |
|
|
|
| 268 |
|
|
$name |
| 269 |
|
|
} |
| 270 |
|
|
|
| 271 |
|
|
sub http_getopt { |
| 272 |
|
|
my ($name, $bind, $print_url) = @_; |
| 273 |
|
|
|
| 274 |
|
|
"name=s" => $name, "bind=s" => $bind, "print-url" => $print_url |
| 275 |
|
|
} |
| 276 |
|
|
|
| 277 |
|
|
sub server_port { |
| 278 |
|
|
my ($self, $name) = @_; |
| 279 |
|
|
|
| 280 |
|
|
$name = server_name $name; |
| 281 |
|
|
|
| 282 |
|
|
my $info = pack "N/a*N/a*", "AnyEvent::MCP port v1", $name; |
| 283 |
|
|
16384 + ((unpack "n", AnyEvent::MCP::hkdf_sha512_expand ( |
| 284 |
|
|
pack ("H*", $self->{section}{global}{secret}), $info, 2 |
| 285 |
|
|
)) & 16383) |
| 286 |
|
|
} |
| 287 |
|
|
|
| 288 |
|
|
sub http_settings { |
| 289 |
|
|
my ($self, $name, $bind) = @_; |
| 290 |
|
|
|
| 291 |
|
|
$name = server_name $name; |
| 292 |
|
|
my $settings = $self->{section}{"server $name"} || {}; |
| 293 |
|
|
my $port = $self->server_port ($name); |
| 294 |
|
|
my $configured = $settings->{bind} // "127.0.0.1:$port"; |
| 295 |
|
|
$configured = "127.0.0.1$configured" if $configured =~ /\A:[0-9]+\z/; |
| 296 |
|
|
my ($configured_host, $configured_port) = AnyEvent::Socket::parse_hostport $configured, $port; |
| 297 |
|
|
defined $configured_host && defined $configured_port |
| 298 |
|
|
&& $configured_port =~ /\A[0-9]+\z/ && $configured_port <= 65535 |
| 299 |
|
|
or die "Invalid MCP bind address '$configured'\n"; |
| 300 |
|
|
|
| 301 |
|
|
$bind = $configured unless defined $bind; |
| 302 |
|
|
$bind = "127.0.0.1$bind" if $bind =~ /\A:[0-9]+\z/; |
| 303 |
|
|
|
| 304 |
|
|
my ($host, $service) = AnyEvent::Socket::parse_hostport $bind, $configured_port; |
| 305 |
|
|
defined $host && defined $service && $service =~ /\A[0-9]+\z/ && $service <= 65535 |
| 306 |
|
|
or die "Invalid MCP bind address '$bind'\n"; |
| 307 |
|
|
|
| 308 |
|
|
my %kv = ( |
| 309 |
|
|
(map { exists $settings->{$_} ? ($_ => $settings->{$_}) : () } |
| 310 |
|
|
qw(path url_host url_scheme cookie bearer users)), |
| 311 |
|
|
name => $name, |
| 312 |
|
|
host => $host, |
| 313 |
|
|
port => $service, |
| 314 |
|
|
); |
| 315 |
|
|
|
| 316 |
|
|
unless (AnyEvent::Socket::parse_address $host) { |
| 317 |
|
|
my ($error, @addresses) = Socket::getaddrinfo ($host, undef, { socktype => Socket::SOCK_STREAM () }); |
| 318 |
|
|
!$error && @addresses |
| 319 |
|
|
or die "Cannot resolve MCP bind host '$host': $error\n"; |
| 320 |
|
|
|
| 321 |
|
|
my ($error2, $address) = Socket::getnameinfo ($addresses[0]{addr}, Socket::NI_NUMERICHOST ()); |
| 322 |
|
|
!$error2 && length $address |
| 323 |
|
|
or die "Cannot format MCP bind host '$host': $error2\n"; |
| 324 |
|
|
|
| 325 |
|
|
$kv{url_host} //= $host; |
| 326 |
|
|
$kv{host} = $address; |
| 327 |
|
|
} |
| 328 |
|
|
|
| 329 |
|
|
unless (grep exists $kv{$_}, qw(cookie bearer users)) { |
| 330 |
|
|
my $token = $self->credential ("cookie", $name); |
| 331 |
|
|
@kv{qw(cookie bearer)} = ($token, $token); |
| 332 |
|
|
} |
| 333 |
|
|
|
| 334 |
|
|
%kv |
| 335 |
|
|
} |
| 336 |
|
|
|
| 337 |
|
|
sub http_url { |
| 338 |
|
|
my ($self, $name, $bind, %options) = @_; |
| 339 |
|
|
|
| 340 |
|
|
# A printed URL needs a credential that survives the next process. |
| 341 |
|
|
my $stored = ref ($self)->new (file => $self->{file}); |
| 342 |
|
|
$stored->{section}{global}{secret} eq $self->{section}{global}{secret} |
| 343 |
|
|
or die "MCP master secret is not persistently stored in $self->{file}\n"; |
| 344 |
|
|
|
| 345 |
|
|
require AnyEvent::MCP::Server::HTTP; |
| 346 |
|
|
my $server = AnyEvent::MCP::Server::HTTP->new ($self->http_settings ($name, $bind), %options); |
| 347 |
|
|
$server->url |
| 348 |
|
|
} |
| 349 |
|
|
|
| 350 |
root |
1.1 |
sub url { |
| 351 |
|
|
my ($self, $name, $username) = @_; |
| 352 |
|
|
|
| 353 |
|
|
my $url = $self->{section}{"server $name"}{url} |
| 354 |
|
|
or die "No URL configured for server '$name'\n"; |
| 355 |
|
|
|
| 356 |
|
|
$url =~ s/\{cookie\}/$self->credential ("cookie", $name, $username)/ge; |
| 357 |
|
|
|
| 358 |
|
|
$url |
| 359 |
|
|
} |
| 360 |
|
|
|
| 361 |
|
|
1 |