1 # Monkeysphere Validation Agent, Perl version
2 # Copyright © 2010 Daniel Kahn Gillmor <dkg@fifthhorseman.net>
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 { package Crypt::Monkeysphere::MSVA;
24 our (@EXPORT_OK,@ISA);
26 @EXPORT_OK = qw( &msvalog );
30 use Crypt::Monkeysphere::MSVA::MarginalUI;
31 use parent qw(HTTP::Server::Simple::CGI);
33 use Regexp::Common qw /net/;
44 use POSIX qw(strftime);
45 # we need the version of GnuPG::Interface that knows about pubkey_data, etc:
46 use GnuPG::Interface 0.42.02;
50 my $gnupg = GnuPG::Interface->new();
51 $gnupg->options->quiet(1);
52 $gnupg->options->batch(1);
55 '/' => { handler => \&noop,
56 methods => { 'GET' => 1 },
58 '/reviewcert' => { handler => \&reviewcert,
59 methods => { 'POST' => 1 },
61 '/extracerts' => { handler => \&extracerts,
62 methods => { 'POST' => 1 },
66 my $default_keyserver = 'hkp://pool.sks-keyservers.net';
67 my $default_keyserver_policy = 'unlessvalid';
69 # Net::Server log_level goes from 0 to 4
70 # this is scaled to match.
84 my $rsa_decoder = Convert::ASN1->new;
85 $rsa_decoder->prepare(q<
96 my $level = $loglevels{lc($ENV{MSVA_LOG_LEVEL})};
97 $level = $loglevels{error} if (! defined $level);
99 if ($loglevels{lc($msglevel)} <= $level) {
105 my $level = $loglevels{lc($ENV{MSVA_LOG_LEVEL})};
106 $level = $loglevels{error} if (! defined $level);
111 return 'Net::Server::MSVA';
118 if (exists $ENV{MSVA_PORT} and $ENV{MSVA_PORT} ne '') {
119 msvalog('debug', "MSVA_PORT set to %s\n", $ENV{MSVA_PORT});
120 $port = $ENV{MSVA_PORT} + 0;
121 die sprintf("not a reasonable port %d", $port) if (($port >= 65536) || $port <= 0);
123 # start the server on requested port
124 my $self = $class->SUPER::new($port);
125 if (! exists $ENV{MSVA_PORT}) {
126 # we can't pass port 0 to the constructor because it evaluates
127 # to false, so HTTP::Server::Simple just uses its internal
128 # default of 8080. But if we want to select an arbitrary open
129 # port, we *can* set it here.
133 $self->{allowed_uids} = {};
134 if (exists $ENV{MSVA_ALLOWED_USERS} and $ENV{MSVA_ALLOWED_USERS} ne '') {
135 msvalog('verbose', "MSVA_ALLOWED_USERS environment variable is set.\nLimiting access to specified users.\n");
136 foreach my $user (split(/ +/, $ENV{MSVA_ALLOWED_USERS})) {
137 my ($name, $passwd, $uid);
138 if ($user =~ /^[0-9]+$/) {
139 $uid = $user + 0; # force to integer
141 ($name,$passwd,$uid) = getpwnam($user);
144 msvalog('verbose', "Allowing access from user ID %d\n", $uid);
145 $self->{allowed_uids}->{$uid} = $user;
147 msvalog('error', "Could not find user '%d'; not allowing\n", $user);
151 # default is to allow access only to the current user
152 $self->{allowed_uids}->{POSIX::getuid()} = 'self';
155 bless ($self, $class);
162 return '200 OK', { available => JSON::true,
164 server => "MSVA-Perl ".$version };
167 # returns an empty list if bad key found.
168 sub parse_openssh_pubkey {
170 my ($label, $prop) = split(/ +/, $data);
171 $prop = decode_base64($prop) or return ();
173 msvalog('debug', "key properties: %s\n", unpack('H*', $prop));
175 while (length($prop) > 4) {
176 my $size = unpack('N', substr($prop, 0, 4));
177 msvalog('debug', "size: 0x%08x\n", $size);
178 return () if (length($prop) < $size + 4);
179 push(@out, substr($prop, 4, $size));
180 $prop = substr($prop, 4 + $size);
182 return () if ($label ne $out[0]);
187 # return an arrayref of processes which we can detect that have the
188 # given socket open (the socket is specified with its inode)
189 sub getpidswithsocketinode {
192 # this appears to be how Linux symlinks open sockets in /proc/*/fd,
193 # as of at least 2.6.26:
194 my $socktarget = sprintf('socket:[%d]', $sockid);
198 if (opendir($procfs, '/proc')) {
199 foreach my $pid (grep { /^\d+$/ } readdir($procfs)) {
200 my $procdir = sprintf('/proc/%d', $pid);
203 if (opendir($procfds, sprintf('/proc/%d/fd', $pid))) {
204 foreach my $procfd (grep { /^\d+$/ } readdir($procfds)) {
205 my $fd = sprintf('/proc/%d/fd/%d', $pid, $procfd);
207 #my ($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($fd);
208 my $targ = readlink($fd);
210 if ($targ eq $socktarget);
220 # FIXME: this whole business is very linux-specific, i think. i
221 # wonder how to get this info in other OSes?
226 # return {uid => X, inode => Y}, meaning the numeric ID of the peer
227 # on the other end of $socket, "socket inode" identifying the peer's
228 # open network socket. each value could be undef if unknown.
229 sub get_client_info {
232 my $sock = IO::Socket->new_from_fd($socket, 'r');
233 # check SO_PEERCRED -- if this was a TCP socket, Linux
234 # might not be able to support SO_PEERCRED (even on the loopback),
235 # though apparently some kernels (Solaris?) are able to.
238 my $remotesocketinode;
239 my $socktype = $sock->sockopt(SO_TYPE) or die "could not get SO_TYPE info";
240 if (defined $socktype) {
241 msvalog('debug', "sockopt(SO_TYPE) = %d\n", $socktype);
243 msvalog('verbose', "sockopt(SO_TYPE) returned undefined.\n");
246 my $peercred = $sock->sockopt(SO_PEERCRED) or die "could not get SO_PEERCRED info";
247 my $client = $sock->peername();
248 my $family = sockaddr_family($client); # should be AF_UNIX (a.k.a. AF_LOCAL) or AF_INET
250 msvalog('verbose', "socket family: %d\nsocket type: %d\n", $family, $socktype);
253 # FIXME: on i386 linux, this appears to be three ints, according to
254 # /usr/include/linux/socket.h. What about other platforms?
255 my ($pid, $uid, $gid) = unpack('iii', $peercred);
257 msvalog('verbose', "SO_PEERCRED: pid: %u, uid: %u, gid: %u\n",
260 if ($pid != 0 && $uid != 0) { # then we can accept it:
263 # FIXME: can we get the socket inode as well this way?
266 # another option in Linux would be to parse the contents of
267 # /proc/net/tcp to find the uid of the peer process based on that
269 if (! defined $clientid) {
270 msvalog('verbose', "SO_PEERCRED failed, digging around in /proc/net/tcp\n");
272 if ($family == AF_INET) {
274 } elsif ($family == AF_INET6) {
277 if (defined $proto) {
278 if ($socktype == &SOCK_STREAM) {
279 $proto = 'tcp'.$proto;
280 } elsif ($socktype == &SOCK_DGRAM) {
281 $proto = 'udp'.$proto;
285 if (defined $proto) {
286 my ($port, $iaddr) = unpack_sockaddr_in($client);
287 my $iaddrstring = unpack("H*", reverse($iaddr));
288 msvalog('verbose', "Port: %04x\nAddr: %s\n", $port, $iaddrstring);
289 my $remmatch = lc(sprintf("%s:%04x", $iaddrstring, $port));
290 my $infofile = '/proc/net/'.$proto;
291 my $f = new IO::File;
292 if ( $f->open('< '.$infofile)) {
293 my @header = split(/ +/, <$f>);
294 my ($localaddrix, $uidix, $inodeix);
297 while ($ix <= $#header) {
298 $localaddrix = $ix - $skipcount if (lc($header[$ix]) eq 'local_address');
299 $uidix = $ix - $skipcount if (lc($header[$ix]) eq 'uid');
300 $inodeix = $ix - $skipcount if (lc($header[$ix]) eq 'inode');
301 $skipcount++ if (lc($header[$ix]) eq 'tx_queue') or (lc($header[$ix]) eq 'tr'); # these headers don't actually result in a new column during the data rows
304 if (!defined $localaddrix) {
305 msvalog('info', "Could not find local_address field in %s; unable to determine peer UID\n",
307 } elsif (!defined $uidix) {
308 msvalog('info', "Could not find uid field in %s; unable to determine peer UID\n",
310 } elsif (!defined $inodeix) {
311 msvalog('info', "Could not find inode field in %s; unable to determine peer network socket inode\n",
314 msvalog('debug', "local_address: %d; uid: %d\n", $localaddrix,$uidix);
315 while (my @line = split(/ +/,<$f>)) {
316 if (lc($line[$localaddrix]) eq $remmatch) {
317 if (defined $clientid) {
318 msvalog('error', "Warning! found more than one remote uid! (%s and %s\n", $clientid, $line[$uidix]);
320 $clientid = $line[$uidix];
321 $remotesocketinode = $line[$inodeix];
322 msvalog('info', "remote peer is uid %d (inode %d)\n",
323 $clientid, $remotesocketinode);
327 msvalog('error', "Warning! could not find peer information in %s. Not verifying.\n", $infofile) unless defined $clientid;
329 } else { # FIXME: we couldn't read the file. what should we
330 # do besides warning?
331 msvalog('info', "Could not read %s; unable to determine peer UID\n",
337 return { 'uid' => $clientid,
338 'inode' => $remotesocketinode };
345 my $clientinfo = get_client_info(select);
346 my $clientuid = $clientinfo->{uid};
348 if (defined $clientuid) {
349 # test that this is an allowed user:
350 if (exists $self->{allowed_uids}->{$clientuid}) {
351 msvalog('verbose', "Allowing access from uid %d (%s)\n", $clientuid, $self->{allowed_uids}->{$clientuid});
353 msvalog('error', "MSVA client connection from uid %d, forbidden.\n", $clientuid);
354 printf("HTTP/1.0 403 Forbidden -- peer does not match local user ID\r\nContent-Type: text/plain\r\nDate: %s\r\n\r\nHTTP/1.1 403 Not Found -- peer does not match the local user ID. Are you sure the agent is running as the same user?\r\n",
355 strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time())),);
360 my $path = $cgi->path_info();
361 my $handler = $dispatch{$path};
363 if (ref($handler) eq "HASH") {
364 if (! exists $handler->{methods}->{$cgi->request_method()}) {
365 printf("HTTP/1.0 405 Method not allowed\r\nAllow: %s\r\nDate: %s\r\n",
366 join(', ', keys(%{$handler->{methods}})),
367 strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time())));
368 } elsif (ref($handler->{handler}) ne "CODE") {
369 printf("HTTP/1.0 500 Server Error\r\nDate: %s\r\n",
370 strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time())));
373 my $ctype = $cgi->content_type();
374 msvalog('verbose', "Got %s %s (Content-Type: %s)\n", $cgi->request_method(), $path, defined $ctype ? $ctype : '**none supplied**');
375 if (defined $ctype) {
376 my @ctypes = split(/; */, $ctype);
377 $ctype = shift @ctypes;
378 if ($ctype eq 'application/json') {
379 $data = from_json($cgi->param('POSTDATA'));
383 my ($status, $object) = $handler->{handler}($data, $clientinfo);
384 my $ret = to_json($object);
385 msvalog('info', "returning: %s\n", $ret);
386 printf("HTTP/1.0 %s\r\nDate: %s\r\nContent-Type: application/json\r\n\r\n%s",
388 strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time())),
392 printf("HTTP/1.0 404 Not Found -- not handled by Monkeysphere validation agent\r\nContent-Type: text/plain\r\nDate: %s\r\n\r\nHTTP/1.0 404 Not Found -- the path:\r\n %s\r\nis not handled by the MonkeySphere validation agent.\r\nPlease try one of the following paths instead:\r\n\r\n%s\r\n",
393 strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time())),
394 $path, ' * '.join("\r\n * ", keys %dispatch) );
402 if ($gpgkey->algo_num != 1) {
403 msvalog('verbose', "Monkeysphere only does RSA keys. This key is algorithm #%d\n", $gpgkey->algo_num);
405 if ($rsakey->{exponent}->bcmp($gpgkey->pubkey_data->[1]) == 0 &&
406 $rsakey->{modulus}->bcmp($gpgkey->pubkey_data->[0]) == 0) {
415 if ($data->{context} =~ /^(https|ssh)$/) {
416 $data->{context} = $1;
417 if ($data->{peer} =~ /^($RE{net}{domain})$/) {
419 return $data->{context}.'://'.$data->{peer};
424 sub get_keyserver_policy {
425 if (exists $ENV{MSVA_KEYSERVER_POLICY} and $ENV{MSVA_KEYSERVER_POLICY} ne '') {
426 if ($ENV{MSVA_KEYSERVER_POLICY} =~ /^(always|never|unlessvalid)$/) {
429 msvalog('error', "Not a valid MSVA_KEYSERVER_POLICY):\n %s\n", $ENV{MSVA_KEYSERVER_POLICY});
431 return $default_keyserver_policy;
435 # We should read from (first hit wins):
437 if (exists $ENV{MSVA_KEYSERVER} and $ENV{MSVA_KEYSERVER} ne '') {
438 if ($ENV{MSVA_KEYSERVER} =~ /^(((hkps?|finger|ldap):\/\/)?$RE{net}{domain})$/) {
441 msvalog('error', "Not a valid keyserver (from MSVA_KEYSERVER):\n %s\n", $ENV{MSVA_KEYSERVER});
444 # FIXME: some msva.conf or monkeysphere.conf file (system and user?)
446 # or else read from the relevant gnupg.conf:
448 if (exists $ENV{GNUPGHOME} and $ENV{GNUPGHOME} ne '') {
449 $gpghome = untaint($ENV{GNUPGHOME});
451 $gpghome = File::Spec->catfile(File::HomeDir->my_home, '.gnupg');
453 my $gpgconf = File::Spec->catfile($gpghome, 'gpg.conf');
456 my %gpgconfig = Config::General::ParseConfig($gpgconf);
457 if ($gpgconfig{keyserver} =~ /^(((hkps?|finger|ldap):\/\/)?$RE{net}{domain})$/) {
458 msvalog('debug', "Using keyserver %s from the GnuPG configuration file (%s)\n", $1, $gpgconf);
461 msvalog('error', "Not a valid keyserver (from gpg config %s):\n %s\n", $gpgconf, $gpgconfig{keyserver});
464 msvalog('error', "The GnuPG configuration file (%s) is not readable\n", $gpgconf);
467 msvalog('info', "Did not find GnuPG configuration file while looking for keyserver '%s'\n", $gpgconf);
470 # the default_keyserver
471 return $default_keyserver;
474 sub fetch_uid_from_keyserver {
477 my $cmd = IO::Handle->new();
478 my $out = IO::Handle->new();
479 my $nul = IO::File->new("< /dev/null");
481 my $ks = get_keyserver();
482 msvalog('debug', "start ks query to %s for UserID: %s\n", $ks, $uid);
483 my $pid = $gnupg->wrap_call
484 ( handles => GnuPG::Handles->new( command => $cmd, stdout => $out, stderr => $nul ),
485 command_args => [ '='.$uid ],
486 commands => [ '--keyserver',
488 qw( --no-tty --with-colons --search ) ]
490 while (my $line = $out->getline()) {
491 msvalog('debug', "from ks query: (%d) %s", $cmd->fileno, $line);
492 if ($line =~ /^info:(\d+):(\d+)/ ) {
493 $cmd->print(join(' ', ($1..$2))."\n");
494 msvalog('debug', 'to ks query: '.join(' ', ($1..$2))."\n");
498 # FIXME: can we do something to avoid hanging forever?
500 msvalog('debug', "ks query returns %d\n", POSIX::WEXITSTATUS($?));
505 my $clientinfo = shift;
506 return if !ref $data;
508 my $status = '200 OK';
509 my $ret = { valid => JSON::false,
510 message => 'Unknown failure',
513 my $uid = getuid($data);
515 msvalog('error', "invalid peer/context: %s/%s\n", $data->{context}, $data->{peer});
516 $ret->{message} = sprintf('invalid peer/context');
517 return $status, $ret;
520 my $rawdata = join('', map(chr, @{$data->{pkc}->{data}}));
521 my $cert = Crypt::X509->new(cert => $rawdata);
522 msvalog('verbose', "cert subject: %s\n", $cert->subject_cn());
523 msvalog('verbose', "cert issuer: %s\n", $cert->issuer_cn());
524 msvalog('verbose', "cert pubkey algo: %s\n", $cert->PubKeyAlg());
525 msvalog('verbose', "cert pubkey: %s\n", unpack('H*', $cert->pubkey()));
527 if ($cert->PubKeyAlg() ne 'RSA') {
528 $ret->{message} = sprintf('public key was algo "%s" (OID %s). MSVA.pl only supports RSA',
529 $cert->PubKeyAlg(), $cert->pubkey_algorithm);
531 my $key = $rsa_decoder->decode($cert->pubkey());
533 # make sure that the returned integers are Math::BigInts:
534 $key->{exponent} = Math::BigInt->new($key->{exponent}) unless (ref($key->{exponent}));
535 $key->{modulus} = Math::BigInt->new($key->{modulus}) unless (ref($key->{modulus}));
536 msvalog('debug', "cert info:\nmodulus: %s\nexponent: %s\n",
537 $key->{modulus}->as_hex(),
538 $key->{exponent}->as_hex(),
541 if ($key->{modulus}->copy()->blog(2) < 1000) { # FIXME: this appears to be the full pubkey, including DER overhead
542 $ret->{message} = sprintf('public key size is less than 1000 bits (was: %d bits)', $cert->pubkey_size());
544 $ret->{message} = sprintf('Failed to validate "%s" through the OpenPGP Web of Trust.', $uid);
547 if (defined $data->{keyserverpolicy} &&
548 $data->{keyserverpolicy} =~ /^(always|never|unlessvalid)$/) {
550 msvalog("verbose", "using requested keyserver policy: %s\n", $1);
552 $kspolicy = get_keyserver_policy();
554 msvalog('debug', "keyserver policy: %s\n", $kspolicy);
555 # needed because $gnupg spawns child processes
556 $ENV{PATH} = '/usr/local/bin:/usr/bin:/bin';
557 if ($kspolicy eq 'always') {
558 fetch_uid_from_keyserver($uid);
560 } elsif ($kspolicy eq 'never') {
565 # fingerprints of keys that are not fully-valid for this User ID, but match
566 # the key from the queried certificate:
567 my @subvalid_key_fprs;
570 foreach my $gpgkey ($gnupg->get_public_keys('='.$uid)) {
572 foreach my $tryuid ($gpgkey->user_ids) {
573 if ($tryuid->as_string eq $uid) {
574 $validity = $tryuid->validity;
577 # treat primary keys just like subkeys:
578 foreach my $subkey ($gpgkey, @{$gpgkey->subkeys}) {
579 my $primarymatch = keycomp($key, $subkey);
581 if ($subkey->usage_flags =~ /a/) {
582 msvalog('verbose', "key matches, and 0x%s is authentication-capable\n", $subkey->hex_id);
583 if ($validity =~ /^[fu]$/) {
585 msvalog('verbose', "...and it matches!\n");
586 $ret->{valid} = JSON::true;
587 $ret->{message} = sprintf('Successfully validated "%s" through the OpenPGP Web of Trust.', $uid);
589 push(@subvalid_key_fprs, { fpr => $subkey->fingerprint, val => $validity }) if $lastloop;
592 msvalog('verbose', "key matches, but 0x%s is not authentication-capable\n", $subkey->hex_id);
600 fetch_uid_from_keyserver($uid) if (!$foundvalid);
605 # only show the marginal UI if the UID of the corresponding
606 # key is not fully valid.
608 my $resp = Crypt::Monkeysphere::MSVA::MarginalUI->ask_the_user($gnupg,
611 getpidswithsocketinode($clientinfo->{inode}));
612 msvalog('info', "response: %s\n", $resp);
614 $ret->{valid} = JSON::true;
615 $ret->{message} = sprintf('Manually validated "%s" through the OpenPGP Web of Trust.', $uid);
620 msvalog('error', "failed to decode %s\n", unpack('H*', $cert->pubkey()));
621 $ret->{message} = sprintf('failed to decode the public key', $uid);
625 return $status, $ret;
633 msvalog('debug', "Subprocess %d terminated.\n", $pid);
635 if (exists $self->{child_pid} &&
636 ($self->{child_pid} == 0 ||
637 $self->{child_pid} == $pid)) {
638 my $exitstatus = POSIX::WEXITSTATUS($?);
639 msvalog('verbose', "Subprocess %d terminated; exiting %d.\n", $pid, $exitstatus);
640 $server->set_exit_status($exitstatus);
641 $server->server_close();
645 # use sparingly! We want to keep taint mode around for the data we
646 # get over the network. this is only here because we want to treat
647 # the command line arguments differently for the subprocess.
658 $server->{server}->{leave_children_open_on_hup} = 1;
660 my $socketcount = @{ $server->{server}->{sock} };
661 if ( $socketcount != 1 ) {
662 msvalog('error', "%d sockets open; should have been 1.\n", $socketcount);
663 $server->set_exit_status(10);
664 $server->server_close();
666 my $port = @{ $server->{server}->{sock} }[0]->sockport();
667 if ((! defined $port) || ($port < 1) || ($port >= 65536)) {
668 msvalog('error', "got nonsense port: %d.\n", $port);
669 $server->set_exit_status(11);
670 $server->server_close();
672 if ((exists $ENV{MSVA_PORT}) && (($ENV{MSVA_PORT} + 0) != $port)) {
673 msvalog('error', "Explicitly requested port %d, but got port: %d.", ($ENV{MSVA_PORT}+0), $port);
674 $server->set_exit_status(13);
675 $server->server_close();
679 if ((exists $ENV{MSVA_CHILD_PID}) && ($ENV{MSVA_CHILD_PID} ne '')) {
680 # this is most likely a re-exec.
681 msvalog('info', "This appears to be a re-exec, monitoring child pid %d\n", $ENV{MSVA_CHILD_PID});
682 $self->{child_pid} = $ENV{MSVA_CHILD_PID} + 0;
683 } elsif ($#ARGV >= 0) {
684 $self->{child_pid} = 0; # indicate that we are planning to fork.
686 if (! defined $fork) {
687 msvalog('error', "could not fork\n");
690 msvalog('debug', "Child process has PID %d\n", $fork);
691 $self->{child_pid} = $fork;
692 $ENV{MSVA_CHILD_PID} = $fork;
694 msvalog('verbose', "PID %d executing: \n", $$);
695 for my $arg (@ARGV) {
696 msvalog('verbose', " %s\n", $arg);
698 # untaint the environment for the subprocess
699 # see: https://labs.riseup.net/code/issues/2461
700 foreach my $e (keys %ENV) {
701 $ENV{$e} = untaint($ENV{$e});
705 push @args, untaint($_);
707 # restore default SIGCHLD handling:
708 $SIG{CHLD} = 'DEFAULT';
709 $ENV{MONKEYSPHERE_VALIDATION_AGENT_SOCKET} = sprintf('http://localhost:%d', $self->port);
710 exec(@args) or exit 111;
714 printf("MONKEYSPHERE_VALIDATION_AGENT_SOCKET=http://localhost:%d;\nexport MONKEYSPHERE_VALIDATION_AGENT_SOCKET;\n", $self->port);
715 # FIXME: consider daemonizing here to behave more like
716 # ssh-agent. maybe avoid backgrounding by setting
717 # MSVA_NO_BACKGROUND.
724 return '500 not yet implemented', { };