Access/xnm.pm untested

filter: Statementstmt 0.0% 0 / 133 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 226 CC 21 SCAR 32.8
CC
21
Cov
0%
xnm_send_login_request
90.0
authenticate
30.0
xnm_auth_char
30.0
CRAP 26.6
1 #
2 # $Id: xnm.pm,v 1.6 2003/07/09 19:36:05 trostler Exp $
3 #
4 # COPYRIGHT AND LICENSE
5 # Copyright (c) 2001-2003, Juniper Networks, Inc.
6 # All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 # 1. Redistributions of source code must retain the above
11 # copyright notice, this list of conditions and the following
12 # disclaimer.
13 # 2. Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following disclaimer
15 # in the documentation and/or other materials provided with the
16 # distribution.
17 # 3. The name of the copyright owner may not be used to
18 # endorse or promote products derived from this software without specific
19 # prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 #
32  
33 #==========================================================================
34 #
35 # xnm Access Method: An abstract class for the TCP and SSL Access Methods
36 #
37 # Here are the mandatory input parameter(s) that are needed to
38 # create a connection with the server and optional input parameters
39 # that can change the default behavior of the TCP connection.
40 #
41 # $self->{hostname} = Mandatory. Name of the router to connect to.
42 #
43 # $self->{login} = Conditional on noninteractive. The username to login the server.
44 # If this parameter is not provided by the client application,
45 # this module will prompt for the username unless noninteractive
46 # is specified.
47 #
48 # $self->{password} = Conditional on noninteractive. The password for the username.
49 # If this parameter is not provided by the client
50 # application and it's requested by the server, this
51 # module will prompt for the password unless noninteractive
52 # is specified.
53 #
54 # $self->{noninteractive} = Optional. If this parameter is specified, this module
55 # will not prompt for any information it needs. Instead,
56 # it will fail if any required information is not already
57 # provided by the client application. For example, if
58 # the login value is not provided and noninteractive is
59 # specified, then the TCP connection will fail.
60 #
61 # Jade states maintained by this access method for each TCP/SSL connection, they
62 # can also be read by the client application:
63 #
64 # $self->{xnm_result} = contains the authenticated user if the TCP
65 # connection was successful. Otherwise, it contains the error message.
66 #
67 # $self->{xnm_state} = the current state of the connection
68 #
69 # $self->{xnm_challenge} = the challenge prompt provided by the server
70 #
71 # $self->{xnm_response_element} = a very temporary variable to keep the name
72 # of the element live just long enough so the xnm_auth_char can use
73 # it. It is set everytime, xnm_auth_start is called and unset by
74 # xnm_auth_char after it knows which element the data is meant for.
75 #
76 #==========================================================================
77 package JUNOS::Access::xnm;
78  
79 use strict;
80 use IO::Socket;
81 use XML::Parser;
82 use Term::ReadKey;
83 use JUNOS::Trace;
84 use JUNOS::Access;
85 use vars qw(@ISA);
86 @ISA = qw(JUNOS::Access);
87  
88 #
89 # JUNOS::Access::xnm::start
90 #
91 use constant JUNOSCRIPT_XNM_NOECHO => 'no';
92 sub start
93 {
94 # implemented by subclass
95 return;
96 }
97  
98 #
99 # private: xnm_authenticate
100 #
101 # This subroutine sends a login request to authenticate itself wit the server.
102 # If the response comes back with a challenge, it sends another login request
103 # with the challenge response.
104 #
105 # <init> --> <sent> --> <challenge> --> <sent> --> <recvd> --> <authenticated>
106 # | |
107 # | |------> <failed>
108 # V
109 # <recvd>
110 # |
111 # |
112 # V
113 # -------
114 # | |
115 # | |
116 # V V
117 # <authenticated> <failed>
118 #
119 use constant XNM_STATE_INIT => "init";
120 use constant XNM_STATE_REQUEST_SENT => "sent";
121 use constant XNM_STATE_REPLY_RECEIVED => "recvd";
122 use constant XNM_STATE_CHALLENGE_REQUESTED => "challenge";
123 use constant XNM_STATE_AUTHENTICATED => "authenticated";
124 use constant XNM_STATE_AUTH_FAILED => "failed";
125  
126 sub authenticate
127 {
128 my($self) = @_;
129  
130 my $xnm_parser = new XML::Parser();
131 unless ($xnm_parser) {
132 $self->{JUNOS_Device}->report_error("Failed to create TCP parser: $!");
133 return;
134 }
135  
136 $xnm_parser->setHandlers(
137 Start => \&xnm_auth_start,
138 Char => \&xnm_auth_char);
139  
140 my $data;
141  
142 # send our login request
143 $self->xnm_send_login_request() || return;
144  
145 # check response
146 $self->{xnm_state} = XNM_STATE_REQUEST_SENT;
147 trace("IO", "xnm_authenticate: new state is $self->{xnm_state}\n");
148  
149 # Get rpc-reply
150 $data = $self->recv() || return;
151  
152 # Get auth results
153 trace("Getting authentication results");
154 $data .= $self->recv();
155  
156 eval {
157 unless ($xnm_parser->parse($data, access => $self)) {
158 $self->{JUNOS_Device}->report_error("cannot parse $data: $!");
159 return;
160 }
161 };
162 return if $@;
163 return 1 if ($self->{xnm_state} eq XNM_STATE_AUTHENTICATED);
164  
165 $self->{JUNOS_Device}->report_error("authentication failed: $self->{xnm_result}");
166 return;
167 }
168  
169 #
170 # private: xnm_send_login_request
171 #
172 # This subroutine sends a TCP login request via the TCP socket.
173 #
174 # <rpc>
175 # <request-login>
176 # <!-- AUTH PROTOCOL -->
177 # </request-login>
178 # </rpc>
179 #
180 # Where the AUTH PROTOCOL section from the client side may contain these tags:
181 #
182 # <username>USERNAME</username>
183 # <challenge-response>CHALLENGE-RESPONSE</challenge-response>
184 #
185 # At minimum the client MUST send the <username> tag with the login
186 # name whenever requesting authentication.
187 #
188 use constant XNM_XML_REQ_LOGIN =>"<rpc><request-login>%s</request-login></rpc>";
189 use constant XNM_XML_USER_INFO =>"<username>%s</username>";
190 use constant XNM_XML_CHALLENGE_INFO =>"<challenge-response>%s</challenge-response>";
191 sub xnm_send_login_request
192 {
193 my($self) = @_;
194  
195 unless ($self->{login}) {
196 if ($self->{noninteractive}) {
197 $self->{JUNOS_Device}->report_error("not allowed to prompt user for login name.");
198 return;
199 }
200 unless ($self->{login} = prompt_for_info('Login:', 1)) {
201 $self->{JUNOS_Device}->report_error("Cannot get login: $!");
202 return;
203 }
204 }
205  
206 my $auth_info = sprintf(XNM_XML_USER_INFO, $self->{login});
207  
208 if ($self->{xnm_state} eq XNM_STATE_CHALLENGE_REQUESTED) {
209 my $echotype = $self->{xnm_challenge_type} || JUNOSCRIPT_XNM_NOECHO;
210 $echotype = ($echotype eq JUNOSCRIPT_XNM_NOECHO)?0:1;
211 my $prompt = $self->{xnm_challenge} || 'Password:';
212 if ($self->{noninteractive}) {
213 $self->{JUNOS_Device}->report_error("not allowed to prompt user for $prompt.");
214 return;
215 }
216 my $challenge_response = prompt_for_info($prompt, $echotype);
217 print "\n";
218 unless ($challenge_response) {
219 $self->{JUNOS_Device}->report_error("Cannot get challenge response: $!");
220 return;
221 }
222 $auth_info .= sprintf(XNM_XML_CHALLENGE_INFO, $challenge_response);
223 } elsif ($self->{password}) {
224 $auth_info .= sprintf(XNM_XML_CHALLENGE_INFO, $self->{password});
225 }
226  
227 my $login_request = sprintf(XNM_XML_REQ_LOGIN, $auth_info);
228
229 $self->send($login_request);
230 }
231  
232 #
233 # prompt_for_info
234 #
235 # Prompt user for information.
236 #
237 sub prompt_for_info
238 {
239 my($prompt, $echo) = @_;
240 print "$prompt ";
241 ReadMode 'noecho' unless $echo;
242 my $data = ReadLine 0;
243 ReadMode 'normal' unless $echo;
244 chomp $data;
245 return $data;
246 }
247  
248 #
249 # Callback: xnm_auth_start
250 #
251 # This callback is called by $self->{xnm_parser} to parse the
252 # login response to manage the authentication states. It is
253 # called with the tag name and atttributes of an XML element
254 # in the login response.
255 #
256 # Excepted Response for TCP Authentication:
257 #
258 # <rpc-reply>
259 # <login-challenge>
260 # <challenge echo='no or yes'>
261 # <!-- PROMPT -->
262 # </challenge>
263 # </login-challenge>
264 # </rpc-reply>
265 # Or:
266 #
267 # <rpc-reply>
268 # <status>
269 # <!-- success of fail -->
270 # </status>
271 # <message>
272 # <!-- username or error message -->
273 # </message>
274 # </rpc-reply>
275 #
276 my %xnm_auth_start_state_table = (
277 'status' => XNM_STATE_REPLY_RECEIVED,
278 'challenge' => XNM_STATE_CHALLENGE_REQUESTED
279 );
280 sub xnm_auth_start
281 {
282 my($parser, $element, %attrs) = @_;
283 my $self = $parser->{access} || return;
284 $self->{xnm_response_element} = $element;
285  
286 if ($xnm_auth_start_state_table{$element}) {
287 $self->{xnm_state} = $xnm_auth_start_state_table{$element};
288 if ($self->{xnm_state} eq XNM_STATE_CHALLENGE_REQUESTED) {
289 $self->{xnm_challenge_type} = $attrs{echo};
290 }
291 trace("IO", "xnm_auth_start: new state is $self->{xnm_state}\n");
292 }
293 }
294  
295 #
296 # Callback: xnm_auth_char
297 #
298 # This callback is called by $self->{xnm_parser} to parse the
299 # login response to manage the authentication states. It's
300 # called with the value of an XML element in the login response.
301 #
302 use constant XNM_RESPONSE_MESSAGE => 'message';
303 use constant XNM_SUCCESS_REPLY => "success";
304 sub xnm_auth_char
305 {
306 my($parser, $data) = @_;
307 my $self = $parser->{access} || return;
308  
309 if ($self->{xnm_state} eq XNM_STATE_REPLY_RECEIVED) {
310 if ($data eq XNM_SUCCESS_REPLY) {
311 $self->{xnm_state} = XNM_STATE_AUTHENTICATED;
312 } else {
313 $self->{xnm_state} = XNM_STATE_AUTH_FAILED;
314 }
315 trace("IO", "xnm_auth_char: new state is $self->{xnm_state}\n");
316 } elsif ($self->{xnm_state} eq XNM_STATE_CHALLENGE_REQUESTED) {
317 $self->{xnm_challenge} = $data;
318 } elsif ($self->{xnm_response_element} eq XNM_RESPONSE_MESSAGE) {
319 $self->{xnm_result} = $data;
320 }
321  
322 undef($self->{xnm_response_element}); # have dealt with this tag
323 }
324  
325 #
326 # JUNOS::Access::xnm::incoming
327 #
328 sub incoming
329 {
330 $_[0];
331 }
332  
333 1;
334  
335 __END__