allow clean re-exec for smoother upgrades (closes MS #2532)
[monkeysphere-validation-agent.git] / Crypt / Monkeysphere / MSVA.pm
1 # Monkeysphere Validation Agent, Perl version
2 # Copyright © 2010 Daniel Kahn Gillmor <dkg@fifthhorseman.net>
3 #
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.
8 #
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.
13 #
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/>.
16
17 { package Crypt::Monkeysphere::MSVA;
18
19   use strict;
20   use warnings;
21
22   BEGIN {
23     use Exporter   ();
24     our (@EXPORT_OK,@ISA);
25     @ISA = qw(Exporter);
26     @EXPORT_OK = qw( &msvalog );
27   }
28   our @EXPORT_OK;
29
30   use Crypt::Monkeysphere::MSVA::MarginalUI;
31   use parent qw(HTTP::Server::Simple::CGI);
32   require Crypt::X509;
33   use Regexp::Common qw /net/;
34   use Convert::ASN1;
35   use MIME::Base64;
36   use IO::Socket;
37   use IO::File;
38   use Socket;
39   use File::Spec;
40   use File::HomeDir;
41   use Config::General;
42
43   use JSON;
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;
47
48   my $version = '0.1';
49
50   my $gnupg = GnuPG::Interface->new();
51   $gnupg->options->quiet(1);
52   $gnupg->options->batch(1);
53
54   my %dispatch = (
55                   '/' => { handler => \&noop,
56                            methods => { 'GET' => 1 },
57                          },
58                   '/reviewcert' => { handler => \&reviewcert,
59                                      methods => { 'POST' => 1 },
60                                    },
61                   '/extracerts' => { handler => \&extracerts,
62                                      methods => { 'POST' => 1 },
63                                    },
64                  );
65
66   my $default_keyserver = 'hkp://pool.sks-keyservers.net';
67   my $default_keyserver_policy = 'unlessvalid';
68
69 # Net::Server log_level goes from 0 to 4
70 # this is scaled to match.
71   my %loglevels = (
72                    'silent' => 0,
73                    'quiet' => 0.25,
74                    'fatal' => 0.5,
75                    'error' => 1,
76                    'info' => 2,
77                    'verbose' => 3,
78                    'debug' => 4,
79                    'debug1' => 4,
80                    'debug2' => 5,
81                    'debug3' => 6,
82                   );
83
84   my $rsa_decoder = Convert::ASN1->new;
85   $rsa_decoder->prepare(q<
86
87    SEQUENCE {
88         modulus INTEGER,
89         exponent INTEGER
90    }
91           >);
92
93   sub msvalog {
94     my $msglevel = shift;
95
96     my $level = $loglevels{lc($ENV{MSVA_LOG_LEVEL})};
97     $level = $loglevels{error} if (! defined $level);
98
99     if ($loglevels{lc($msglevel)} <= $level) {
100       printf STDERR @_;
101     }
102   };
103
104   sub get_log_level {
105     my $level = $loglevels{lc($ENV{MSVA_LOG_LEVEL})};
106     $level = $loglevels{error} if (! defined $level);
107     return $level;
108   }
109
110   sub net_server {
111     return 'Net::Server::MSVA';
112   };
113
114   sub new {
115     my $class = shift;
116
117     my $port = 0;
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);
122     }
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.
130       $self->port(0);
131     }
132
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
140         } else {
141           ($name,$passwd,$uid) = getpwnam($user);
142         }
143         if (defined $uid) {
144           msvalog('verbose', "Allowing access from user ID %d\n", $uid);
145           $self->{allowed_uids}->{$uid} = $user;
146         } else {
147           msvalog('error', "Could not find user '%d'; not allowing\n", $user);
148         }
149       }
150     } else {
151       # default is to allow access only to the current user
152       $self->{allowed_uids}->{POSIX::getuid()} = 'self';
153     }
154
155     bless ($self, $class);
156     return $self;
157   }
158
159   sub noop {
160     my $self = shift;
161     my $cgi = shift;
162     return '200 OK', { available => JSON::true,
163                        protoversion => 1,
164                        server => "MSVA-Perl ".$version };
165   }
166
167   # returns an empty list if bad key found.
168   sub parse_openssh_pubkey {
169     my $data = shift;
170     my ($label, $prop) = split(/ +/, $data);
171     $prop = decode_base64($prop) or return ();
172
173     msvalog('debug', "key properties: %s\n", unpack('H*', $prop));
174     my @out;
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);
181     }
182     return () if ($label ne $out[0]);
183     return @out;
184   }
185
186
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 {
190     my $sockid = shift;
191
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);
195     my @pids;
196
197     my $procfs;
198     if (opendir($procfs, '/proc')) {
199       foreach my $pid (grep { /^\d+$/ } readdir($procfs)) {
200         my $procdir = sprintf('/proc/%d', $pid);
201         if (-d $procdir) {
202           my $procfds;
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);
206               if (-l $fd) {
207                 #my ($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($fd);
208                 my $targ = readlink($fd);
209                 push @pids, $pid
210                   if ($targ eq $socktarget);
211               }
212             }
213             closedir($procfds);
214           }
215         }
216       }
217       closedir($procfs);
218     }
219
220     # FIXME: this whole business is very linux-specific, i think.  i
221     # wonder how to get this info in other OSes?
222
223     return \@pids;
224   }
225
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 {
230     my $socket = shift;
231
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.
236
237     my $clientid;
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);
242     } else {
243       msvalog('verbose', "sockopt(SO_TYPE) returned undefined.\n");
244     }
245
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
249
250     msvalog('verbose', "socket family: %d\nsocket type: %d\n", $family, $socktype);
251
252     if ($peercred) {
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);
256
257       msvalog('verbose', "SO_PEERCRED: pid: %u, uid: %u, gid: %u\n",
258               $pid, $uid, $gid,
259              );
260       if ($pid != 0 && $uid != 0) { # then we can accept it:
261         $clientid = $uid;
262       }
263       # FIXME: can we get the socket inode as well this way?
264     }
265
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
268     # information.
269     if (! defined $clientid) {
270       msvalog('verbose', "SO_PEERCRED failed, digging around in /proc/net/tcp\n");
271       my $proto;
272       if ($family == AF_INET) {
273         $proto = '';
274       } elsif ($family == AF_INET6) {
275         $proto = '6';
276       }
277       if (defined $proto) {
278         if ($socktype == &SOCK_STREAM) {
279           $proto = 'tcp'.$proto;
280         } elsif ($socktype == &SOCK_DGRAM) {
281           $proto = 'udp'.$proto;
282         } else {
283           undef $proto;
284         }
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);
295             my $ix = 0;
296             my $skipcount = 0;
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
302               $ix++;
303             }
304             if (!defined $localaddrix) {
305               msvalog('info', "Could not find local_address field in %s; unable to determine peer UID\n",
306                       $infofile);
307             } elsif (!defined $uidix) {
308               msvalog('info', "Could not find uid field in %s; unable to determine peer UID\n",
309                       $infofile);
310             } elsif (!defined $inodeix) {
311               msvalog('info', "Could not find inode field in %s; unable to determine peer network socket inode\n",
312                       $infofile);
313             } else {
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]);
319                   } else {
320                     $clientid = $line[$uidix];
321                     $remotesocketinode = $line[$inodeix];
322                     msvalog('info', "remote peer is uid %d (inode %d)\n",
323                             $clientid, $remotesocketinode);
324                   }
325                 }
326               }
327             msvalog('error', "Warning! could not find peer information in %s.  Not verifying.\n", $infofile) unless defined $clientid;
328             }
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",
332                     $infofile);
333           }
334         }
335       }
336     }
337     return { 'uid' => $clientid,
338              'inode' => $remotesocketinode };
339   }
340
341   sub handle_request {
342     my $self = shift;
343     my $cgi  = shift;
344
345     my $clientinfo = get_client_info(select);
346     my $clientuid = $clientinfo->{uid};
347
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});
352       } else {
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())),);
356         return;
357       }
358     }
359
360     my $path = $cgi->path_info();
361     my $handler = $dispatch{$path};
362
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())));
371       } else {
372         my $data = {};
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'));
380           }
381         };
382
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",
387                $status,
388                strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time())),
389                $ret);
390       }
391     } else {
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) );
395     }
396   }
397
398   sub keycomp {
399     my $rsakey = shift;
400     my $gpgkey = shift;
401
402     if ($gpgkey->algo_num != 1) {
403       msvalog('verbose', "Monkeysphere only does RSA keys.  This key is algorithm #%d\n", $gpgkey->algo_num);
404     } else {
405       if ($rsakey->{exponent}->bcmp($gpgkey->pubkey_data->[1]) == 0 &&
406           $rsakey->{modulus}->bcmp($gpgkey->pubkey_data->[0]) == 0) {
407         return 1;
408       }
409     }
410     return 0;
411   }
412
413   sub getuid {
414     my $data = shift;
415     if ($data->{context} =~ /^(https|ssh)$/) {
416       $data->{context} = $1;
417       if ($data->{peer} =~ /^($RE{net}{domain})$/) {
418         $data->{peer} = $1;
419         return $data->{context}.'://'.$data->{peer};
420       }
421     }
422   }
423
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)$/) {
427         return $1;
428       }
429       msvalog('error', "Not a valid MSVA_KEYSERVER_POLICY):\n  %s\n", $ENV{MSVA_KEYSERVER_POLICY});
430     }
431     return $default_keyserver_policy;
432   }
433
434   sub get_keyserver {
435     # We should read from (first hit wins):
436     # the environment
437     if (exists $ENV{MSVA_KEYSERVER} and $ENV{MSVA_KEYSERVER} ne '') {
438       if ($ENV{MSVA_KEYSERVER} =~ /^(((hkps?|finger|ldap):\/\/)?$RE{net}{domain})$/) {
439         return $1;
440       }
441       msvalog('error', "Not a valid keyserver (from MSVA_KEYSERVER):\n  %s\n", $ENV{MSVA_KEYSERVER});
442     }
443
444     # FIXME: some msva.conf or monkeysphere.conf file (system and user?)
445
446     # or else read from the relevant gnupg.conf:
447     my $gpghome;
448     if (exists $ENV{GNUPGHOME} and $ENV{GNUPGHOME} ne '') {
449       $gpghome = untaint($ENV{GNUPGHOME});
450     } else {
451       $gpghome = File::Spec->catfile(File::HomeDir->my_home, '.gnupg');
452     }
453     my $gpgconf = File::Spec->catfile($gpghome, 'gpg.conf');
454     if (-f $gpgconf) {
455       if (-r $gpgconf) {
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);
459           return $1;
460         } else {
461           msvalog('error', "Not a valid keyserver (from gpg config %s):\n  %s\n", $gpgconf, $gpgconfig{keyserver});
462         }
463       } else {
464         msvalog('error', "The GnuPG configuration file (%s) is not readable\n", $gpgconf);
465       }
466     } else {
467       msvalog('info', "Did not find GnuPG configuration file while looking for keyserver '%s'\n", $gpgconf);
468     }
469
470     # the default_keyserver
471     return $default_keyserver;
472   }
473
474   sub fetch_uid_from_keyserver {
475     my $uid = shift;
476
477     my $cmd = IO::Handle->new();
478     my $out = IO::Handle->new();
479     my $nul = IO::File->new("< /dev/null");
480
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',
487                       $ks,
488                       qw( --no-tty --with-colons --search ) ]
489       );
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");
495         last;
496       }
497     }
498     # FIXME: can we do something to avoid hanging forever?
499     waitpid($pid, 0);
500     msvalog('debug', "ks query returns %d\n", POSIX::WEXITSTATUS($?));
501   }
502
503   sub reviewcert {
504     my $data  = shift;
505     my $clientinfo  = shift;
506     return if !ref $data;
507
508     my $status = '200 OK';
509     my $ret =  { valid => JSON::false,
510                  message => 'Unknown failure',
511                };
512
513     my $uid = getuid($data);
514     if ($uid eq []) {
515         msvalog('error', "invalid peer/context: %s/%s\n", $data->{context}, $data->{peer});
516         $ret->{message} = sprintf('invalid peer/context');
517         return $status, $ret;
518     }
519
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()));
526
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);
530     } else {
531       my $key = $rsa_decoder->decode($cert->pubkey());
532       if ($key) {
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(),
539                );
540
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());
543         } else {
544           $ret->{message} = sprintf('Failed to validate "%s" through the OpenPGP Web of Trust.', $uid);
545           my $lastloop = 0;
546           msvalog('debug', "keyserver policy: %s\n", get_keyserver_policy);
547           # needed because $gnupg spawns child processes
548           $ENV{PATH} = '/usr/local/bin:/usr/bin:/bin';
549           if (get_keyserver_policy() eq 'always') {
550             fetch_uid_from_keyserver($uid);
551             $lastloop = 1;
552           } elsif (get_keyserver_policy() eq 'never') {
553             $lastloop = 1;
554           }
555           my $foundvalid = 0;
556
557           # fingerprints of keys that are not fully-valid for this User ID, but match
558           # the key from the queried certificate:
559           my @subvalid_key_fprs;
560
561           while (1) {
562             foreach my $gpgkey ($gnupg->get_public_keys('='.$uid)) {
563               my $validity = '-';
564               foreach my $tryuid ($gpgkey->user_ids) {
565                 if ($tryuid->as_string eq $uid) {
566                   $validity = $tryuid->validity;
567                 }
568               }
569               # treat primary keys just like subkeys:
570               foreach my $subkey ($gpgkey, @{$gpgkey->subkeys}) {
571                 my $primarymatch = keycomp($key, $subkey);
572                 if ($primarymatch) {
573                   if ($subkey->usage_flags =~ /a/) {
574                     msvalog('verbose', "key matches, and 0x%s is authentication-capable\n", $subkey->hex_id);
575                     if ($validity =~ /^[fu]$/) {
576                       $foundvalid = 1;
577                       msvalog('verbose', "...and it matches!\n");
578                       $ret->{valid} = JSON::true;
579                       $ret->{message} = sprintf('Successfully validated "%s" through the OpenPGP Web of Trust.', $uid);
580                     } else {
581                       push(@subvalid_key_fprs, { fpr => $subkey->fingerprint, val => $validity }) if $lastloop;
582                     }
583                   } else {
584                     msvalog('verbose', "key matches, but 0x%s is not authentication-capable\n", $subkey->hex_id);
585                   }
586                 }
587               }
588             }
589             if ($lastloop) {
590               last;
591             } else {
592               fetch_uid_from_keyserver($uid) if (!$foundvalid);
593               $lastloop = 1;
594             }
595           }
596
597           # only show the marginal UI if the UID of the corresponding
598           # key is not fully valid.
599           if (!$foundvalid) {
600             my $resp = Crypt::Monkeysphere::MSVA::MarginalUI->ask_the_user($gnupg,
601                                                                            $uid,
602                                                                            \@subvalid_key_fprs,
603                                                                            getpidswithsocketinode($clientinfo->{inode}));
604             msvalog('info', "response: %s\n", $resp);
605             if ($resp) {
606               $ret->{valid} = JSON::true;
607               $ret->{message} = sprintf('Manually validated "%s" through the OpenPGP Web of Trust.', $uid);
608             }
609           }
610         }
611       } else {
612         msvalog('error', "failed to decode %s\n", unpack('H*', $cert->pubkey()));
613         $ret->{message} = sprintf('failed to decode the public key', $uid);
614       }
615     }
616
617     return $status, $ret;
618   }
619
620   sub child_dies {
621     my $self = shift;
622     my $pid = shift;
623     my $server = shift;
624
625     msvalog('debug', "Subprocess %d terminated.\n", $pid);
626
627     if (exists $self->{child_pid} &&
628         ($self->{child_pid} == 0 ||
629          $self->{child_pid} == $pid)) {
630       my $exitstatus = POSIX::WEXITSTATUS($?);
631       msvalog('verbose', "Subprocess %d terminated; exiting %d.\n", $pid, $exitstatus);
632       $server->set_exit_status($exitstatus);
633       $server->server_close();
634     }
635   }
636
637   # use sparingly!  We want to keep taint mode around for the data we
638   # get over the network.  this is only here because we want to treat
639   # the command line arguments differently for the subprocess.
640   sub untaint {
641     my $x = shift;
642     $x =~ /^(.*)$/ ;
643     return $1;
644   }
645
646   sub post_bind_hook {
647     my $self = shift;
648     my $server = shift;
649
650     my $socketcount = @{ $server->{server}->{sock} };
651     if ( $socketcount != 1 ) {
652       msvalog('error', "%d sockets open; should have been 1.\n", $socketcount);
653       $server->set_exit_status(10);
654       $server->server_close();
655     }
656     my $port = @{ $server->{server}->{sock} }[0]->sockport();
657     if ((! defined $port) || ($port < 1) || ($port >= 65536)) {
658       msvalog('error', "got nonsense port: %d.\n", $port);
659       $server->set_exit_status(11);
660       $server->server_close();
661     }
662     if ((exists $ENV{MSVA_PORT}) && (($ENV{MSVA_PORT} + 0) != $port)) {
663       msvalog('error', "Explicitly requested port %d, but got port: %d.", ($ENV{MSVA_PORT}+0), $port);
664       $server->set_exit_status(13);
665       $server->server_close();
666     }
667     $self->port($port);
668
669     if ((exists $ENV{MSVA_CHILD_PID}) && ($ENV{MSVA_CHILD_PID} ne '')) {
670       # this is most likely a re-exec.
671       msvalog('info', "This appears to be a re-exec, monitoring child pid %d\n", $ENV{MSVA_CHILD_PID});
672       $self->{child_pid} = $ENV{MSVA_CHILD_PID} + 0;
673     } elsif ($#ARGV >= 0) {
674       $self->{child_pid} = 0; # indicate that we are planning to fork.
675       my $fork = fork();
676       if (! defined $fork) {
677         msvalog('error', "could not fork\n");
678       } else {
679         if ($fork) {
680           msvalog('debug', "Child process has PID %d\n", $fork);
681           $self->{child_pid} = $fork;
682           $ENV{MSVA_CHILD_PID} = $fork;
683         } else {
684           msvalog('verbose', "PID %d executing: \n", $$);
685           for my $arg (@ARGV) {
686             msvalog('verbose', " %s\n", $arg);
687           }
688           # untaint the environment for the subprocess
689           # see: https://labs.riseup.net/code/issues/2461
690           foreach my $e (keys %ENV) {
691             $ENV{$e} = untaint($ENV{$e});
692           }
693           my @args;
694           foreach (@ARGV) {
695             push @args, untaint($_);
696           }
697           # restore default SIGCHLD handling:
698           $SIG{CHLD} = 'DEFAULT';
699           $ENV{MONKEYSPHERE_VALIDATION_AGENT_SOCKET} = sprintf('http://localhost:%d', $self->port);
700           exec(@args) or exit 111;
701         }
702       }
703     } else {
704       printf("MONKEYSPHERE_VALIDATION_AGENT_SOCKET=http://localhost:%d;\nexport MONKEYSPHERE_VALIDATION_AGENT_SOCKET;\n", $self->port);
705       # FIXME: consider daemonizing here to behave more like
706       # ssh-agent.  maybe avoid backgrounding by setting
707       # MSVA_NO_BACKGROUND.
708     };
709   }
710
711   sub extracerts {
712     my $data = shift;
713
714     return '500 not yet implemented', { };
715   }
716
717   1;
718 }