ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/AnyEvent-MCP/lib/AnyEvent/MCP/Config.pm
Revision: 1.22
Committed: Tue Oct 27 21:33:37 2026 UTC (3 days, 3 hours ago) by root
Branch: MAIN
CVS Tags: rel-0_7, rel-0_71, HEAD
Changes since 1.21: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3     AnyEvent::MCP::Config - named servers and derived MCP credentials
4    
5     =head1 SYNOPSIS
6    
7 root 1.12 use AnyEvent::MCP::Config;
8    
9 root 1.1 my $config = AnyEvent::MCP::Config->global;
10 root 1.8 my $token = $config->credential ("token", "agni");
11 root 1.6 my $url = $config->http_url ("agni", undef, path => "/");
12 root 1.1
13     =head1 DESCRIPTION
14    
15 root 1.3 An MCP server and its clients often need matching credentials. Rather than
16     maintaining a separate password for each server and user, this module can
17 root 1.14 derive them from one private master secret. The invoked program basename,
18     instance name, optional username and credential purpose each contribute to
19     the result. Instances default to C<default>; different programs still derive
20     different credentials for that instance name.
21 root 1.3
22     The shared file format is described in L<AnyEvent::MCP/GLOBAL CONFIGURATION>.
23     Loading this module does not read any files. The first call to C<global>
24     reads the configuration, ensures a master secret exists and caches the object
25     for the process.
26 root 1.1
27     A missing secret is generated using C<AnyEvent::MCP::random_bytes> and saved
28     when possible. Saving failure produces a warning; the generated secret remains
29 root 1.3 usable in this process, but another process might derive different credentials.
30     A supplied invalid secret is an error. Changing or losing the master secret
31     invalidates all credentials derived from it.
32 root 1.1
33 root 1.13 Derivation uses C<derive_secret> with the random master key.
34 root 1.14 The application namespace, purpose, program, instance name and username are
35     separately length-prefixed, so distinct contexts derive different credentials.
36 root 1.3 Deriving a password does not create an account or configure the server to
37     accept it: authentication still belongs to the server.
38 root 1.1
39     =head1 METHODS
40    
41 root 1.12 =over
42    
43     =item $config = AnyEvent::MCP::Config->global (file => $file)
44 root 1.1
45     Returns the cached global configuration. The first call accepts the same
46     arguments as C<new>; later calls return the same object without rereading
47     the file. Call this explicitly during program startup.
48    
49 root 1.12 =item $config = AnyEvent::MCP::Config->new (file => $file)
50 root 1.1
51     Reads C<$file>, or the default file when omitted, and ensures a master
52     secret exists. Unlike C<global>, each call creates an independent object.
53     A missing file is allowed; other read errors are fatal. Sections are available
54     as C<< $config->{section}{$section} >>, each a hash reference of string
55 root 1.10 values. Named servers use C<[instance NAME]>.
56 root 1.1
57 root 1.12 =item $config = AnyEvent::MCP::Config->init (file => $file)
58 root 1.1
59     Explicitly creates the configuration file with a random secret, mode 0600.
60     Creates missing parent directories with mode 0700. Fails if the file already
61     exists, so it cannot accidentally replace an existing secret.
62    
63 root 1.13 =item $bytes = $config->derive_secret ($length, @info)
64    
65     =item $bytes = AnyEvent::MCP::Config->derive_secret ($length, @info)
66    
67     Derive C<$length> secret bytes from the configuration's master secret and
68     an ordered list of context byte strings C<@info>. The class form uses
69     C<global>, loading the default configuration on first use and reusing it
70     thereafter. The object form uses that object's configuration, including
71     one loaded from an explicit file.
72    
73     The same master secret and context always produce the same bytes. Use a
74     distinctive application/purpose string as the first context field, followed
75     by names or other values identifying the result:
76    
77     my $secret = AnyEvent::MCP::Config->derive_secret (
78     32, "my application token v1", AnyEvent::MCP::utf8enc ($name)
79     );
80    
81     The result contains raw bytes; encode it yourself if a printable token is
82     needed. This method does not add the result to a server's accepted tokens.
83    
84 root 1.14 The fixed C<anyevent-mcp> context field is prepended automatically.
85 root 1.13 Context fields are separately length-prefixed, so C<("ab", "c")> and
86     C<("a", "bc")> have different contexts. Encode character strings explicitly,
87     for example with C<AnyEvent::MCP::utf8enc>. Context arguments are left
88     unchanged.
89    
90     C<$length> must be an integer in 0..16320; zero returns an empty string.
91     Shorter results are prefixes of longer results for the same master secret
92     and context. Use distinct context fields for different purposes, rather
93     than relying on different lengths to separate them.
94    
95     Derivation uses HKDF-SHA512 expansion, with each context field prefixed by
96     its four-byte big-endian byte length. Changing or losing the master secret
97     changes every result derived from it.
98    
99 root 1.12 =item $credential = $config->credential ($purpose, $name, $username)
100 root 1.1
101     Derives a 32-byte credential, returned as unpadded URL-safe Base64.
102 root 1.14 C<$purpose> is C<token>. C<$name> is the instance name, and C<$username>
103 root 1.8 is optional. The default server credential is derived with no username.
104 root 1.1
105 root 1.14 Credentials use the context C<("anyevent-mcp", "token", PROGRAM, NAME, USER)>.
106     PROGRAM is the invoked basename, with a trailing C<.pl> or C<.perl> removed.
107 root 1.18 Changing the program or instance name changes the credential.
108 root 1.12 A credential derived with C<$username> is B<not> automatically accepted
109     by the server. This method neither requires a server section nor configures
110     authentication.
111 root 1.1
112 root 1.14 =item $program = AnyEvent::MCP::Config::program_name ()
113    
114     Returns the invoked program basename, stripping C<.pl> or C<.perl>.
115     An inline Perl invocation uses C<perl>. This identity also selects the
116     C<[program PROGRAM]> configuration section; it is independent of routing aliases.
117    
118 root 1.12 =item $name = AnyEvent::MCP::Config::server_name ($name)
119 root 1.4
120 root 1.14 Validate a service-instance name, or use C<default> if undefined.
121 root 1.4 Names consist of an ASCII letter or underscore followed by letters, digits,
122     underscores or hyphens.
123    
124 root 1.12 =item @options = AnyEvent::MCP::Config::http_getopt (\$name, \$bind, \$print_url, \@tokens)
125 root 1.4
126     Return standard C<Getopt::Long> options referring to the caller's scalars.
127    
128 root 1.12 =item $port = $config->server_port ($name)
129 root 1.4
130     Derive a stable default port in 16384..32767 from the master secret and
131 root 1.14 program basename and service-instance name. Collisions produce ordinary listener bind errors.
132 root 1.4
133 root 1.12 =item %settings = $config->http_settings ($name, $bind)
134 root 1.4
135 root 1.10 Resolve HTTP settings from C<[instance NAME]> and an optional command-line
136 root 1.4 C<bind> override. The default is IPv4 loopback and the name-derived port.
137     The bind setting accepts C<ADDRESS:PORT>, C<[IPv6]:PORT>, C<ADDRESS>,
138     and C<:PORT> (which keeps the default loopback address).
139 root 1.12 Hostnames are resolved once when settings are obtained; their original
140     spelling is advertised unless C<url_host> overrides it. The returned options
141     include C<name>, C<host>, C<port> and C<tokens>, plus configured C<path>,
142     C<url_host> and C<url_scheme>. This does not start a listener.
143 root 1.4
144 root 1.17 =item @rules = $config->auto_rules ($name)
145    
146     Return ordered automatic permission rules from C<[program PROGRAM]> and
147     C<[instance NAME]>. C<auto-rule.SUFFIX = allow|deny TOOL[,ACTION[,CAPABILITY]]>
148     entries are sorted by key. The suffix is an arbitrary ordering label; instance
149     entries override matching program entries, and an empty value disables an
150     inherited entry. Command-line rules take precedence over these defaults.
151    
152 root 1.12 =item $url = $config->http_url ($name, $bind, %options)
153 root 1.4
154 root 1.8 Format a URL containing the first accepted token, without listening.
155     Port zero cannot be used because the kernel has not yet selected a port.
156     When the token is derived, the master secret must be persistently stored,
157     so the next process derives the same URL. Explicit static tokens and
158     C<auth =E<gt> "none"> do not require that persistence check.
159 root 1.4
160 root 1.12 =back
161    
162 root 1.1 =cut
163    
164     package AnyEvent::MCP::Config;
165    
166     use common::sense;
167     use AnyEvent::MCP ();
168 root 1.13 use Digest::SHA ();
169 root 1.1 use MIME::Base64 ();
170     use File::Basename ();
171     use File::Path ();
172     use Fcntl qw(O_WRONLY O_RDWR O_CREAT O_EXCL LOCK_EX);
173     use Errno qw(ENOENT);
174 root 1.4 use AnyEvent::Socket ();
175 root 1.1
176     our @ISA = qw(AnyEvent::MCP::Base);
177    
178     sub _file {
179     my $dir = $ENV{XDG_CONFIG_HOME}
180     || ($ENV{HOME} || die "HOME is not set\n") . "/.config";
181    
182     "$dir/anyevent-mcp/global.ini"
183     }
184    
185     my $global;
186    
187     sub global {
188     my $class = shift;
189    
190     $global ||= $class->new (@_)
191     }
192    
193 root 1.16 sub new {
194 root 1.1 my ($class, %kv) = @_;
195 root 1.11
196 root 1.1 my $self = $class->SUPER::new (section => {}, %kv);
197     $self->{file} = _file unless exists $self->{file};
198    
199     if (open my $fh, "<:encoding(UTF-8)", $self->{file}) {
200     $self->_read ($fh);
201     close $fh or die "$self->{file}: $!\n";
202     } else {
203     die "$self->{file}: $!\n" unless $! == ENOENT;
204     }
205    
206     $self->_ensure_secret;
207    
208 root 1.11 $self->{section}{global}{secret} =~ /\A[0-9a-fA-F]{128}\z/
209     or die "$self->{file}: invalid secret; supply 128 hexadecimal digits\n";
210 root 1.1
211     $self
212     }
213    
214     sub _read {
215     my ($self, $fh) = @_;
216    
217 root 1.15 local $/;
218     AnyEvent::MCP::parse_ini scalar (<$fh>), sub {
219     my ($name, $kv) = @_;
220     my $section = $self->{section}{$name || "global"} ||= {};
221     %$section = (%$section, %$kv);
222     }, $self->{file};
223 root 1.1
224     }
225    
226     sub _ensure_secret {
227     my ($self) = @_;
228    
229     return if exists $self->{section}{global}{secret};
230    
231     my $secret = eval { unpack "H*", AnyEvent::MCP::random_bytes 64 };
232     die "$@Supply a securely generated 128-digit hexadecimal secret in $self->{file}\n"
233     if $@;
234    
235     eval {
236     File::Path::mkpath (File::Basename::dirname ($self->{file}), 0, 0700);
237     sysopen my $fh, $self->{file}, O_RDWR | O_CREAT, 0600
238     or die "$self->{file}: $!\n";
239     flock $fh, LOCK_EX or die "$self->{file}: $!\n";
240     binmode $fh, ":encoding(UTF-8)";
241    
242     my $disk = bless { file => $self->{file}, section => {} }, ref $self;
243     $disk->_read ($fh);
244    
245     unless (exists $disk->{section}{global}{secret}) {
246     seek $fh, 0, 2 or die "$self->{file}: $!\n";
247     print {$fh} "\n[global]\nsecret = $secret\n"
248     or die "$self->{file}: $!\n";
249     $disk->{section}{global}{secret} = $secret;
250     }
251    
252     close $fh or die "$self->{file}: $!\n";
253     $self->{section} = $disk->{section};
254     1
255     } or warn "Cannot save MCP master secret; credentials may only be stable for this process: $@";
256    
257     $self->{section}{global}{secret} = $secret
258     unless exists $self->{section}{global}{secret};
259     }
260    
261     sub init {
262     my ($class, %kv) = @_;
263 root 1.11
264 root 1.1 my $file = exists $kv{file} ? $kv{file} : _file;
265    
266     my $secret = AnyEvent::MCP::random_bytes 64;
267    
268     File::Path::mkpath (File::Basename::dirname ($file), 0, 0700);
269    
270     sysopen my $fh, $file, O_WRONLY | O_CREAT | O_EXCL, 0600
271     or die "$file: $!\n";
272    
273     print {$fh} "[global]\nsecret = ", unpack ("H*", $secret), "\n"
274     or die "$file: $!\n";
275     close $fh or die "$file: $!\n";
276    
277     $class->new (file => $file)
278     }
279    
280 root 1.13 sub _prk_derive_secret($$@) {
281     my ($prk, $length, @info) = @_;
282    
283     64 == length $prk
284     or die "derive_secret: key length must be 64 octets\n";
285     $length =~ /\A[0-9]+\z/ && $length <= 255 * 64
286     or die "derive_secret: length must be an integer in 0..16320\n";
287    
288 root 1.14 my $info = pack "(N/a*)*", "anyevent-mcp", @info;
289 root 1.13 my $t = "";
290     my $okm = "";
291     my $i = 0;
292    
293     while ($length > length $okm) {
294     $t = Digest::SHA::hmac_sha512 $t . $info . chr ++$i, $prk;
295     $okm .= $t;
296     }
297    
298     substr $okm, 0, $length
299     }
300    
301     sub derive_secret {
302     my ($self, $length, @info) = @_;
303    
304     $self = $self->global unless ref $self;
305    
306     my $secret = $self->{section}{global}{secret};
307     $secret =~ /\A[0-9a-fA-F]{128}\z/
308     or die "$self->{file}: missing or invalid secret\n";
309    
310     _prk_derive_secret (pack ("H*", $secret), $length, @info)
311     }
312    
313 root 1.21 sub program_name {
314     my $name = File::Basename::basename ($0);
315     $name =~ s/\.(?:pl|perl)\z//;
316     $name eq "-e" ? "perl" : $name
317     }
318    
319     sub server_name {
320     my ($name) = @_;
321    
322     $name = "default" unless defined $name;
323    
324     $name =~ /\A[A-Za-z_][A-Za-z0-9_-]*\z/
325     or die "Invalid MCP server name '$name'\n";
326    
327     $name
328     }
329    
330 root 1.1 sub credential {
331     my ($self, $purpose, $name, $username) = @_;
332    
333     $username = "" unless defined $username;
334    
335 root 1.11 $purpose eq "token"
336     or die "Unknown credential purpose '$purpose'\n";
337 root 1.1
338     my $credential = MIME::Base64::encode_base64 (
339 root 1.13 $self->derive_secret (
340 root 1.21 32,
341     $purpose,
342     (AnyEvent::MCP::utf8enc program_name),
343     (AnyEvent::MCP::utf8enc $name),
344     (AnyEvent::MCP::utf8enc $username)
345 root 1.13 ), ""
346 root 1.1 );
347     $credential =~ y!+/=!-_!d;
348    
349     $credential
350     }
351    
352 root 1.4 sub http_getopt {
353 root 1.5 my ($name, $bind, $print_url, $tokens) = @_;
354 root 1.4
355 root 1.5 "name=s" => $name, "bind=s" => $bind, "print-url" => $print_url, "token=s@" => $tokens
356 root 1.4 }
357    
358 root 1.17 sub auto_rules {
359     my ($self, $name) = @_;
360     my %settings = (
361     %{$self->{section}{"program " . program_name ()} || {}},
362     %{$self->{section}{"instance " . server_name ($name)} || {}},
363     );
364     my @rules;
365    
366     for my $key (sort grep /\Aauto-rule\./, keys %settings) {
367     $key =~ /\Aauto-rule\.(.+)\z/
368     or die "Invalid automatic permission rule key '$key'\n";
369     my $value = $settings{$key};
370     next unless length $value;
371     my ($decision, $spec) = $value =~ /\A(allow|deny)\s+(\S+)\z/
372     or die "Invalid configured automatic permission rule '$key'\n";
373     push @rules, [$decision, $spec];
374     }
375    
376     @rules
377     }
378    
379 root 1.4 sub server_port {
380     my ($self, $name) = @_;
381    
382     $name = server_name $name;
383    
384 root 1.21 16384 + (
385     (unpack "n", $self->derive_secret (
386     2,
387     "port",
388     (AnyEvent::MCP::utf8enc program_name),
389     (AnyEvent::MCP::utf8enc $name))
390     ) & 16383)
391 root 1.4 }
392    
393     sub http_settings {
394     my ($self, $name, $bind) = @_;
395    
396     $name = server_name $name;
397 root 1.14 my $settings = {
398     %{ $self->{section}{"program " . program_name ()} },
399     %{ $self->{section}{"instance $name"} },
400     };
401 root 1.4 my $port = $self->server_port ($name);
402     my $configured = $settings->{bind} // "127.0.0.1:$port";
403     $configured = "127.0.0.1$configured" if $configured =~ /\A:[0-9]+\z/;
404     my ($configured_host, $configured_port) = AnyEvent::Socket::parse_hostport $configured, $port;
405     defined $configured_host && defined $configured_port
406     && $configured_port =~ /\A[0-9]+\z/ && $configured_port <= 65535
407     or die "Invalid MCP bind address '$configured'\n";
408    
409     $bind = $configured unless defined $bind;
410     $bind = "127.0.0.1$bind" if $bind =~ /\A:[0-9]+\z/;
411    
412     my ($host, $service) = AnyEvent::Socket::parse_hostport $bind, $configured_port;
413     defined $host && defined $service && $service =~ /\A[0-9]+\z/ && $service <= 65535
414     or die "Invalid MCP bind address '$bind'\n";
415    
416     my %kv = (
417     (map { exists $settings->{$_} ? ($_ => $settings->{$_}) : () }
418 root 1.5 qw(path url_host url_scheme)),
419 root 1.4 name => $name,
420     host => $host,
421     port => $service,
422     );
423    
424     unless (AnyEvent::Socket::parse_address $host) {
425 root 1.20 my $cv = AnyEvent->condvar;
426     AnyEvent::Socket::resolve_sockaddr $host, $service, "tcp", 0, 0, $cv;
427     my ($resolved) = $cv->recv;
428     $resolved or die "Cannot resolve MCP bind host '$host'\n";
429 root 1.4
430 root 1.20 my (undef, $address) = AnyEvent::Socket::unpack_sockaddr $resolved->[3];
431 root 1.4 $kv{url_host} //= $host;
432 root 1.9 $kv{host} = AnyEvent::Socket::format_address $address;
433 root 1.4 }
434    
435 root 1.11 $kv{tokens} = [exists $settings->{tokens}
436     ? split /\s*,\s*/, $settings->{tokens}, -1
437     : $self->credential ("token", $name)];
438 root 1.4
439     %kv
440     }
441    
442     sub http_url {
443     my ($self, $name, $bind, %options) = @_;
444    
445 root 1.14 $name = server_name $name;
446    
447 root 1.11 # only a derived token depends on the master secret surviving this process.
448 root 1.8 if (($options{auth} || "") ne "none"
449     && !exists $options{tokens}
450 root 1.14 && !exists $self->{section}{"instance $name"}{tokens}
451     && !exists $self->{section}{"program " . program_name ()}{tokens}) {
452 root 1.22 my $stored = (ref $self)->new (file => $self->{file});
453 root 1.8 $stored->{section}{global}{secret} eq $self->{section}{global}{secret}
454     or die "MCP master secret is not persistently stored in $self->{file}\n";
455     }
456 root 1.4
457     require AnyEvent::MCP::Server::HTTP;
458 root 1.8 my %http = $self->http_settings ($name, $bind);
459     delete $http{tokens} if ($options{auth} || "") eq "none";
460 root 1.11
461     AnyEvent::MCP::Server::HTTP->new (%http, %options)->url
462 root 1.4 }
463    
464 root 1.1
465     1