handle non-responsive agents more politely
[monkeysphere-validation-agent.git] / msva-query-agent
1 #!/usr/bin/perl -wT
2
3 # Monkeysphere Validation Agent Client, Perl version
4 # Copyright © 2010 Jameson Greaf Rollins <jrollins@finestructure.net>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 use warnings;
20 use strict;
21
22 use Crypt::Monkeysphere::MSVA qw( msvalog );
23 use Crypt::Monkeysphere::MSVA::Client;
24
25 my $context = shift;
26 my $peer = shift;
27 my $pkctype = shift || 'x509der';
28
29 # load raw pkc data from stdin
30 my $pkcdata = do {
31   local $/; # slurp!
32   <STDIN>;
33 };
34
35 my ($status,$ret) = Crypt::Monkeysphere::MSVA::Client->query_agent($context,$peer,$pkctype, $pkcdata, $ENV{MONKEYSPHERE_VALIDATION_AGENT_SOCKET});
36
37 msvalog('info', "status: %s\n", $status);
38 if (defined $ret) {
39   msvalog('info', "valid: %s\n", $ret->{valid});
40   msvalog('fatal', "message: %s\n", $ret->{message});
41   exit 0
42     if ($ret->{valid});
43 }
44 exit 1;
45
46 __END__
47
48 =head1 NAME
49
50 msva-query-agent - query a Monkeysphere Validation Agent
51
52 =head1 SYNOPSIS
53
54 msva-query-agent CONTEXT PEER PKC_TYPE < /path/to/public_key_carrier
55
56 =head1 ABSTRACT
57
58 msva-query-agent validates certificates for a given use by querying a
59 running Monkeysphere Validation Agent.
60
61 =head1 USAGE
62
63 msva-query-agent reads a certificate from standard input, and posts it
64 to the running Monkeysphere Validation Agent.  The return code
65 indicates the validity (as determined by the agent) of the certificate
66 for the specified purpose.  The agent's return message (if any) is
67 emitted on stderr.
68
69 Three command-line arguments are all required, supplied in order, as
70 follows:
71
72 =over 4
73
74 =item CONTEXT
75
76 Context in which the certificate is being validated (e.g. 'https',
77 'ssh', 'ike')
78
79 =item PEER
80
81 The name of the intended peer.  When validating a certificate for a
82 service, supply the host's full DNS name (e.g.  'foo.example.net')
83
84 =item PKC_TYPE
85
86 The format of public key carrier data provided on standard input
87 (e.g. 'x509der')
88
89 =back
90
91 =head1 RETURN CODE
92
93 If the certificate is valid for the requested peer in the given
94 context, the return code is 0.  Otherwise, the return code is 1.
95
96 =head1 ENVIRONMENT VARIABLES
97
98 msva-query-agent's behavior is controlled by environment variables:
99
100 =over 4
101
102 =item MONKEYSPHERE_VALIDATION_AGENT_SOCKET
103
104 Socket over which to query the validation agent.  If unset, the
105 default value is 'http://localhost:8901'.
106
107 =item MSVA_LOG_LEVEL
108
109 Log messages about its operation to stderr.  MSVA_LOG_LEVEL controls
110 its verbosity, and should be one of (in increasing verbosity): silent,
111 quiet, fatal, error, info, verbose, debug, debug1, debug2, debug3.
112 Default is 'error'.
113
114 =back
115
116 =head1 COMMUNICATION PROTOCOL DETAILS
117
118 Communications with the Monkeysphere Validation Agent are in the form
119 of JSON requests over plain HTTP.  Responses from the agent are also
120 JSON objects.  For details on the structure of the requests and
121 responses, please see
122 http://web.monkeysphere.info/validation-agent/protocol
123
124 =head1 SEE ALSO
125
126 msva-perl(1), monkeysphere(1), monkeysphere(7)
127
128 =head1 BUGS AND FEEDBACK
129
130 Bugs or feature requests for msva-perl and associated tools should be
131 filed with the Monkeysphere project's bug tracker at
132 https://labs.riseup.net/code/projects/monkeysphere/issues/
133
134 =head1 AUTHORS AND CONTRIBUTORS
135
136 Jameson Graef Rollins E<lt>jrollins@finestructure.net<gt>
137 Daniel Kahn Gillmor E<lt>dkg@fifthhorseman.net<gt>
138
139 The Monkeysphere Team http://web.monkeysphere.info/
140
141 =head1 COPYRIGHT AND LICENSE
142
143 Copyright © 2010, Jameson Graef Rollins and others from the Monkeysphere
144 team.  msva-query-agent is free software, distributed under the GNU
145 Public License, version 3 or later.