fix up the pem-handling code, and test it
[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::Client;
23
24 my $context = shift;
25 my $peer = shift;
26 my $pkctype = shift || 'x509der';
27
28 # load raw pkc data from stdin
29 my $pkcdata = do {
30   local $/; # slurp!
31   <STDIN>;
32 };
33
34 my $client = Crypt::Monkeysphere::MSVA::Client->new(
35                                                     socket => $ENV{MONKEYSPHERE_VALIDATION_AGENT_SOCKET},
36                                                     log_level => $ENV{MSVA_LOG_LEVEL},
37                                                    );
38
39 my ($status,$ret) = $client->query_agent($context,$peer,$pkctype,$pkcdata);
40
41 $client->log('info', "status: %s\n", $status);
42 if (defined $ret) {
43   $client->log('info', "valid: %s\n", $ret->{valid});
44   $client->log('fatal', "message: %s\n", $ret->{message});
45   $client->log('info', "server: %s\n", $ret->{server});
46   exit 0
47     if ($ret->{valid});
48 }
49 exit 1;
50
51 __END__
52
53 =head1 NAME
54
55 msva-query-agent - query a Monkeysphere Validation Agent
56
57 =head1 SYNOPSIS
58
59 msva-query-agent CONTEXT PEER PKC_TYPE < /path/to/public_key_carrier
60
61 =head1 ABSTRACT
62
63 msva-query-agent validates certificates for a given use by querying a
64 running Monkeysphere Validation Agent.
65
66 =head1 USAGE
67
68 msva-query-agent reads a certificate from standard input, and posts it
69 to the running Monkeysphere Validation Agent.  The return code
70 indicates the validity (as determined by the agent) of the certificate
71 for the specified purpose.  The agent's return message (if any) is
72 emitted on stderr.
73
74 Three command-line arguments are all required, supplied in order, as
75 follows:
76
77 =over 4
78
79 =item CONTEXT
80
81 Context in which the certificate is being validated (e.g. 'https',
82 'ssh', 'ike')
83
84 =item PEER
85
86 The name of the intended peer.  When validating a certificate for a
87 service, supply the host's full DNS name (e.g.  'foo.example.net')
88
89 =item PKC_TYPE
90
91 The format of public key carrier data provided on standard input
92 (e.g. 'x509der')
93
94 =back
95
96 =head1 RETURN CODE
97
98 If the certificate is valid for the requested peer in the given
99 context, the return code is 0.  Otherwise, the return code is 1.
100
101 =head1 ENVIRONMENT VARIABLES
102
103 msva-query-agent's behavior is controlled by environment variables:
104
105 =over 4
106
107 =item MONKEYSPHERE_VALIDATION_AGENT_SOCKET
108
109 Socket over which to query the validation agent.  If unset, the
110 default value is 'http://localhost:8901'.
111
112 =item MSVA_LOG_LEVEL
113
114 Log messages about its operation to stderr.  MSVA_LOG_LEVEL controls
115 its verbosity, and should be one of (in increasing verbosity): silent,
116 quiet, fatal, error, info, verbose, debug, debug1, debug2, debug3.
117 Default is 'error'.
118
119 =back
120
121 =head1 COMMUNICATION PROTOCOL DETAILS
122
123 Communications with the Monkeysphere Validation Agent are in the form
124 of JSON requests over plain HTTP.  Responses from the agent are also
125 JSON objects.  For details on the structure of the requests and
126 responses, please see
127 http://web.monkeysphere.info/validation-agent/protocol
128
129 =head1 SEE ALSO
130
131 msva-perl(1), monkeysphere(1), monkeysphere(7)
132
133 =head1 BUGS AND FEEDBACK
134
135 Bugs or feature requests for msva-perl and associated tools should be
136 filed with the Monkeysphere project's bug tracker at
137 https://labs.riseup.net/code/projects/monkeysphere/issues/
138
139 =head1 AUTHORS AND CONTRIBUTORS
140
141 Jameson Graef Rollins E<lt>jrollins@finestructure.net<gt>
142 Daniel Kahn Gillmor E<lt>dkg@fifthhorseman.net<gt>
143
144 The Monkeysphere Team http://web.monkeysphere.info/
145
146 =head1 COPYRIGHT AND LICENSE
147
148 Copyright © 2010, Jameson Graef Rollins and others from the Monkeysphere
149 team.  msva-query-agent is free software, distributed under the GNU
150 Public License, version 3 or later.