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