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