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