| 1 | # | ||
| 2 | # $Id: Device.pm,v 1.29 2003/03/02 11:12:09 dsw 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 | # JUNOS::Device: This package implements an interface to the JUNOScript (tm) | ||
| 35 | # XML-based API supported by Juniper Networks. Objects of this class represent | ||
| 36 | # the local side of connection to a Juniper Networks device running JUNOS, | ||
| 37 | # over which the JUNOScript protocol will be spoken. JUNOScript is | ||
| 38 | # described on http://xml.juniper.net. | ||
| 39 | # | ||
| 40 | |||
| 41 | package JUNOS::Device; | ||
| 42 | |||
| 43 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 44 | 2 | ▶ | use vars qw(@ISA $VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 45 | 2 | ▶ | use JUNOS::Access; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 46 | 2 | ▶ | use JUNOS::DOM::Parser; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 47 | 0 | ▶ | use JUNOS::Response; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 48 | use JUNOS::Trace; | ||
| 49 | use JUNOS::Methods; | ||
| 50 | use XML::Parser; | ||
| 51 | |||
| 52 | @ISA = qw(JUNOS::Methods); | ||
| 53 | $| = 1; | ||
| 54 | |||
| 55 | use JUNOS::version; | ||
| 56 | |||
| 57 | # | ||
| 58 | # Create a new JUNOS::Device. | ||
| 59 | # | ||
| 60 | sub new | ||
| 61 | { | ||
| 62 | my($class, %args) = @_; | ||
| 63 | |||
| 64 | my $self = { %args }; | ||
| 65 | my $version = $self->{version} || $VERSION; | ||
| 66 | |||
| 67 | # Initialize Methods | ||
| 68 | JUNOS::Methods::init($version); | ||
| 69 | |||
| 70 | # Register the default callbacks | ||
| 71 | $self->{JUNOS_CallbackHandler} ||= \&callbackHandler; | ||
| 72 | $self->{JUNOS_ReplyHandler} ||= \&replyHandler; | ||
| 73 | $self->{JUNOS_CharHandler} ||= \&charHandler; | ||
| 74 | |||
| 75 | # Mark ourselves with the proper class | ||
| 76 | $class = ref($class) || $class; | ||
| 77 | bless $self, $class; | ||
| 78 | |||
| 79 | # Bring up the connection | ||
| 80 | unless ($self->{Do_Not_Connect}) { | ||
| 81 | return $self->connect(); | ||
| 82 | } | ||
| 83 | |||
| 84 | $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | # | ||
| 88 | # Open the connection to the JUNOScript server. This is done by the 'new' | ||
| 89 | # operator, but can be performed explicitly by hand. | ||
| 90 | # | ||
| 91 | sub connect | ||
| 92 | { | ||
| 93 | my($self) = @_; | ||
| 94 | |||
| 95 | $self->clear_errors() if caller() ne __PACKAGE__; | ||
| 96 | |||
| 97 | # Have we already connected? Silently succeed | ||
| 98 | return $self if $self->{JUNOS_Conn}; | ||
| 99 | |||
| 100 | # The access field/class/key/type is the handle we | ||
| 101 | # use to talk to the Juniper box. | ||
| 102 | |||
| 103 | my $conn = new JUNOS::Access($self); | ||
| 104 | |||
| 105 | # Need better error handling here.... | ||
| 106 | unless (ref($conn)){ | ||
| 107 | $self->report_error("Could not open connection"); | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | eval "use " . ref($conn); | ||
| 111 | |||
| 112 | # Record the connection; connect it, mark it | ||
| 113 | $self->{JUNOS_Conn} = $conn; | ||
| 114 | unless ($conn->connect()) { | ||
| 115 | $self->report_error("Could not connect"); | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | $self->{JUNOS_Connected} = 1; | ||
| 119 | |||
| 120 | # Kick off the XML parser | ||
| 121 | $self->parse_start(); | ||
| 122 | |||
| 123 | trace("Trace", "starting connect::\n"); | ||
| 124 | |||
| 125 | # We need to receive the server side of the initial handshake first | ||
| 126 | # (at least the <?xml?> part), so that we can avoid sending our | ||
| 127 | # handshake to the ssh processes initial prompts (password/etc). | ||
| 128 | # So we wait til we see the start of the real XML data flow.... | ||
| 129 | until ($self->{JUNOS_Active}) { | ||
| 130 | my $in = $conn->recv(); | ||
| 131 | |||
| 132 | my $waiting = 'waiting for xml'; | ||
| 133 | if( $conn->{seen_xml} ) { $waiting = 'found xml'; } | ||
| 134 | trace("IO", "during connect - ($waiting) input:\n\t$in\n" ); | ||
| 135 | |||
| 136 | if ($conn->{seen_xml}) { | ||
| 137 | # After we've seen xml, parse anything | ||
| 138 | } elsif ($in =~ /<\s*\?/) { | ||
| 139 | $in =~ s/^[\d\D]*(<\s*\?)/$1/; | ||
| 140 | $conn->{seen_xml} = 1; | ||
| 141 | } else { | ||
| 142 | if (not $conn->incoming($in) or $conn->eof) { | ||
| 143 | $self->report_error("initial handshake with JUNOScript server failed"); | ||
| 144 | $self->disconnect; | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | next; | ||
| 148 | } | ||
| 149 | |||
| 150 | if ($conn->eof) { | ||
| 151 | $self->parse_done($in); | ||
| 152 | last; | ||
| 153 | } else { | ||
| 154 | $self->parse_more($in); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | # Send our half of the initial handshake | ||
| 159 | my $xml_decl = '<?xml version="1.0" encoding="us-ascii"?>'; | ||
| 160 | my $junoscript = '<junoscript version="1.0" os="perl-api">'; | ||
| 161 | |||
| 162 | $conn->send($xml_decl . "\n" . $junoscript . "\n"); | ||
| 163 | |||
| 164 | if (!$conn->authenticate) { | ||
| 165 | $self->report_error("Authentication error"); | ||
| 166 | exit(1); | ||
| 167 | } | ||
| 168 | |||
| 169 | return $self; | ||
| 170 | } | ||
| 171 | |||
| 172 | # | ||
| 173 | # Disconnect from the JUNOScript server. Destroy the parser and | ||
| 174 | # disconnect (and close) the connection. | ||
| 175 | # User program should first call 'request_end_session' | ||
| 176 | # | ||
| 177 | sub disconnect | ||
| 178 | { | ||
| 179 | my($self) = @_; | ||
| 180 | |||
| 181 | $self->clear_errors() if caller() ne __PACKAGE__; | ||
| 182 | |||
| 183 | my $conn = $self->{JUNOS_Conn}; | ||
| 184 | |||
| 185 | $conn->disconnect if ($conn and $self->{JUNOS_Connected}); | ||
| 186 | |||
| 187 | $self->parse_destroy(); | ||
| 188 | |||
| 189 | $self->{JUNOS_Conn} = $self->{JUNOS_Connected} = undef; | ||
| 190 | 1; | ||
| 191 | } | ||
| 192 | |||
| 193 | # | ||
| 194 | # Automatic form of destory | ||
| 195 | # | ||
| 196 | sub DESTROY | ||
| 197 | { | ||
| 198 | my($self) = @_; | ||
| 199 | |||
| 200 | $self->disconnect() if $self->{JUNOS_Connected}; | ||
| 201 | } | ||
| 202 | |||
| 203 | # | ||
| 204 | # Send a request to the JUNOScript server and return the result. | ||
| 205 | # Since the normal DOM top level will always contain only the | ||
| 206 | # node for the rpc-reply, we assume the caller really just wants | ||
| 207 | # that node. If called in an scalar context, we only return this | ||
| 208 | # node. In an array context, we return the document and the node | ||
| 209 | # as an array. | ||
| 210 | # | ||
| 211 | sub request | ||
| 212 | { | ||
| 213 | my $rpc; | ||
| 214 | my($self, $request) = @_; | ||
| 215 | |||
| 216 | $self->clear_errors() if caller() ne __PACKAGE__; | ||
| 217 | |||
| 218 | tracept("Request"); | ||
| 219 | |||
| 220 | $self->connect() || return unless $self->{JUNOS_Connected}; | ||
| 221 | |||
| 222 | my $conn = $self->{JUNOS_Conn}; | ||
| 223 | |||
| 224 | # Catches calling 'request_end_session' twice amongst other calamities | ||
| 225 | if ($conn->{seen_eof}) { | ||
| 226 | $self->report_error("connection ended unexpectedly"); | ||
| 227 | return; | ||
| 228 | } | ||
| 229 | |||
| 230 | # If the caller gives us an object, turn it into a string. | ||
| 231 | if (ref($request)) { | ||
| 232 | $rpc = $request->toString; | ||
| 233 | } else { | ||
| 234 | $rpc = $request; | ||
| 235 | } | ||
| 236 | trace("Trace", "starting rpc; ", ref($request), "sending::\n", $rpc); | ||
| 237 | |||
| 238 | trace("Verbose", "--- begin request---\n", | ||
| 239 | $rpc, ($rpc =~ /\n$/) ? "" : "\n", "--- end request ---\n"); | ||
| 240 | |||
| 241 | # Send the request to the JUNOScript server | ||
| 242 | my $back = $conn->send($rpc); | ||
| 243 | |||
| 244 | # Pull data off the server until we get a complete reply (or eof). | ||
| 245 | until ($self->{JUNOS_Reply}) { | ||
| 246 | my $in = $conn->recv(); | ||
| 247 | trace("Trace", "during rpc; got::\n $in"); | ||
| 248 | |||
| 249 | if ($conn->eof) { | ||
| 250 | # Make sure it's legit before handing it off | ||
| 251 | $self->parse_done($in) if (contains_end_tag($in)); | ||
| 252 | $conn->{seen_eof} = 1; | ||
| 253 | last; | ||
| 254 | } else { | ||
| 255 | # This is for the case when using telnet & after we're done | ||
| 256 | # running 'junoscript' we get the shell char back | ||
| 257 | if ($self->parse_more($in)) | ||
| 258 | { | ||
| 259 | $conn->{seen_eof} = 1; | ||
| 260 | last; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | } | ||
| 265 | |||
| 266 | tracept("Request"); | ||
| 267 | |||
| 268 | # Fetch the XML::DOM::Document, as saved by replyHandler() | ||
| 269 | my $doc = $self->{JUNOS_Reply}; | ||
| 270 | trace("Trace", "reply is ", ref($doc) || "empty", "::", $doc); | ||
| 271 | unless ($doc) { | ||
| 272 | $self->report_error("reply is empty"); | ||
| 273 | return; | ||
| 274 | } | ||
| 275 | |||
| 276 | # Clear the reply | ||
| 277 | undef $self->{JUNOS_Reply}; | ||
| 278 | |||
| 279 | # Turn the response into a JUNOS::Response, allowing us | ||
| 280 | # to add methods on top of the DOM methods. | ||
| 281 | my $response = JUNOS::Response->new($doc->getDocumentElement()); | ||
| 282 | |||
| 283 | trace("Verbose", "--- begin reply---\n", | ||
| 284 | $response->toString, ($response =~ /\n$/) ? "" : "\n", | ||
| 285 | "--- end reply ---\n"); | ||
| 286 | |||
| 287 | # If the caller wants an array, we give them both the document and | ||
| 288 | # the response, which they must dispose of. | ||
| 289 | return ($doc, $response) if wantarray; | ||
| 290 | |||
| 291 | # Otherwise dispose of the document, which was only needed for DOM. | ||
| 292 | if ($#$response >= 0) { | ||
| 293 | $doc->removeChild($response); | ||
| 294 | $response->setOwnerDocument(undef); | ||
| 295 | $doc->dispose; | ||
| 296 | } | ||
| 297 | |||
| 298 | return $response; | ||
| 299 | } | ||
| 300 | |||
| 301 | # | ||
| 302 | # Perform an rpc using a raw command string. This is a mostly unsupported | ||
| 303 | # way of getting to any JUNOS command that is currently unsupported in | ||
| 304 | # JUNOScript. Caveat coder. | ||
| 305 | # | ||
| 306 | sub command | ||
| 307 | { | ||
| 308 | tracept("Request"); | ||
| 309 | my($self, $request) = @_; | ||
| 310 | $self->clear_errors() if caller() ne __PACKAGE__; | ||
| 311 | |||
| 312 | my $rpc = "<rpc><command>" . $request . "</command></rpc>\n"; | ||
| 313 | $self->request($rpc); | ||
| 314 | } | ||
| 315 | |||
| 316 | # These callback handlers are called by the parser to let | ||
| 317 | # us know important things about the connection. | ||
| 318 | |||
| 319 | # | ||
| 320 | # unsupported.callback | ||
| 321 | # | ||
| 322 | sub callbackHandler | ||
| 323 | { | ||
| 324 | my($self, $parser) = @_; | ||
| 325 | |||
| 326 | $self->report_error("unsupported callback"); | ||
| 327 | } | ||
| 328 | |||
| 329 | # | ||
| 330 | # A reply is complete; record it for the main loop above. | ||
| 331 | # | ||
| 332 | sub replyHandler | ||
| 333 | { | ||
| 334 | tracept("Reply"); | ||
| 335 | |||
| 336 | my($self, $parser, $reply) = @_; | ||
| 337 | |||
| 338 | $self->{JUNOS_Reply} = $reply; | ||
| 339 | } | ||
| 340 | |||
| 341 | # | ||
| 342 | # Raw character data is available that the parser does not want. This is | ||
| 343 | # normally because the connection is not yet in XML mode. Hand the data | ||
| 344 | # off to the access method so that it can deal with it. | ||
| 345 | # | ||
| 346 | sub charHandler | ||
| 347 | { | ||
| 348 | my($self, $parser, $data) = @_; | ||
| 349 | |||
| 350 | my $conn = $self->{JUNOS_Conn}; | ||
| 351 | $conn->incoming($data); | ||
| 352 | } | ||
| 353 | |||
| 354 | # | ||
| 355 | # These functions are wrappers for the parser | ||
| 356 | # | ||
| 357 | |||
| 358 | # | ||
| 359 | # Start the parser: create an XML::Parser with style==JUNOS, pull | ||
| 360 | # a parser instance off this expat-parser-instance, and make | ||
| 361 | # the world even more confusing by making these objects refer | ||
| 362 | # to each other. | ||
| 363 | # | ||
| 364 | sub parse_start | ||
| 365 | { | ||
| 366 | tracept("Parse"); | ||
| 367 | my($self, %args) = @_; | ||
| 368 | |||
| 369 | $args{Style} = "JUNOS::DOM::Parser"; | ||
| 370 | #$args{Style} = "Debug"; | ||
| 371 | |||
| 372 | my $expat = new XML::Parser(%args); | ||
| 373 | $self->{JUNOS_Expat} = $expat; | ||
| 374 | |||
| 375 | my $parser = $expat->parse_start(); | ||
| 376 | $parser->{JUNOS_Device} = $self; | ||
| 377 | $self->{JUNOS_Parser} = $parser; | ||
| 378 | } | ||
| 379 | |||
| 380 | sub contains_end_tag | ||
| 381 | { | ||
| 382 | $_[0] =~ m#</junoscript>#m; | ||
| 383 | } | ||
| 384 | |||
| 385 | # | ||
| 386 | # Parse some more input: toss the given input data to the parser. | ||
| 387 | # | ||
| 388 | sub parse_more | ||
| 389 | { | ||
| 390 | tracept("Parse"); | ||
| 391 | my($self, $input) = @_; | ||
| 392 | |||
| 393 | my $done = 0; | ||
| 394 | |||
| 395 | # Get rid of any xtra stuff after closing tag if it's there | ||
| 396 | $done = 1 if ($input =~ s#</junoscript>(.*)$#</junoscript>#ms); | ||
| 397 | |||
| 398 | $self->{sofar} .= $input; | ||
| 399 | my $parser = $self->{JUNOS_Parser}; | ||
| 400 | |||
| 401 | while ($input =~ /<\/xnm:error>(?:\s*<output>)/) { | ||
| 402 | # Add any 'output' tags to the <xnm:error> mix | ||
| 403 | $input =~ s/(.*?)(<\/xnm:error>)(?:\s*(<output>.*?<\/output>\s*))(.*)/$1$3$2$4/s; | ||
| 404 | } | ||
| 405 | |||
| 406 | # Catch bad XML | ||
| 407 | eval { | ||
| 408 | $parser->parse_more($input); | ||
| 409 | }; | ||
| 410 | if ($@) { | ||
| 411 | my($col) = $@ =~ /column (\d+)/; | ||
| 412 | my($error) = $@ =~ /^(.*?)\s+at/m; | ||
| 413 | my $mismatched = $error =~ /mismatched/; | ||
| 414 | |||
| 415 | # Check out the previous 50 characters | ||
| 416 | my $bad_area = substr $self->{sofar}, $col-50; | ||
| 417 | |||
| 418 | print "Something bad has happened - XML got garbled.\n"; | ||
| 419 | print "Here's what it did not like:\n\t",$bad_area,"\n"; | ||
| 420 | |||
| 421 | print "The error was: $error\n"; | ||
| 422 | print "This tag doesn't has a start tag!\n" if $mismatched; | ||
| 423 | |||
| 424 | die "Parse failure"; | ||
| 425 | } | ||
| 426 | |||
| 427 | $parser->parse_done if ($done); | ||
| 428 | $done; | ||
| 429 | } | ||
| 430 | |||
| 431 | # | ||
| 432 | # Mark the end of parsing, normally caused by end-of-file | ||
| 433 | # | ||
| 434 | sub parse_done | ||
| 435 | { | ||
| 436 | tracept("Parse"); | ||
| 437 | my($self, $input) = @_; | ||
| 438 | |||
| 439 | my $done = $self->parse_more($input) if ($input); | ||
| 440 | return if $done; | ||
| 441 | |||
| 442 | my $parser = $self->{JUNOS_Parser}; | ||
| 443 | $parser->parse_done; | ||
| 444 | } | ||
| 445 | |||
| 446 | # | ||
| 447 | # Destory the parser and clean up the self-referential nature of the objects. | ||
| 448 | sub parse_destroy | ||
| 449 | { | ||
| 450 | my ($self) = @_; | ||
| 451 | my $parser = $self->{JUNOS_Parser}; | ||
| 452 | $parser->{JUNOS_Device} = undef; | ||
| 453 | $self->{JUNOS_Parser} = undef; | ||
| 454 | undef $parser; | ||
| 455 | } | ||
| 456 | |||
| 457 | # | ||
| 458 | # These functions interact with the DOM parser. In order to pull | ||
| 459 | # complete DOM objects from the DOM parser, we need to reinitialize | ||
| 460 | # it before each <rpc-reply> and terminate it after each </rpc-reply>. | ||
| 461 | # | ||
| 462 | |||
| 463 | # | ||
| 464 | # Open a reply by reinitializing the DOM parser, passing it | ||
| 465 | # the information we learned at connection startup. | ||
| 466 | # | ||
| 467 | sub openReply | ||
| 468 | { | ||
| 469 | tracept("Reply"); | ||
| 470 | my($self, $parser) = @_; | ||
| 471 | |||
| 472 | XML::Parser::Dom::Init($parser); | ||
| 473 | XML::Parser::Dom::XMLDecl($parser, $self->{XMLDecl_Version}, | ||
| 474 | $self->{XMLDecl_Encoding}, $self->{XMLDecl_Standalone}); | ||
| 475 | $self->{JUNOS_expect_1st_element} = 1; | ||
| 476 | undef $self->{tag}; | ||
| 477 | } | ||
| 478 | |||
| 479 | # | ||
| 480 | # Close a reply by terminating the parser and passing back the DOM | ||
| 481 | # document that it returned to us. | ||
| 482 | sub closeReply | ||
| 483 | { | ||
| 484 | tracept("Reply"); | ||
| 485 | my($self, $parser) = @_; | ||
| 486 | |||
| 487 | my $reply = XML::Parser::Dom::Final($parser); | ||
| 488 | |||
| 489 | return $reply; | ||
| 490 | } | ||
| 491 | |||
| 492 | sub clear_errors | ||
| 493 | { | ||
| 494 | my($self) = @_; | ||
| 495 | $self->{Errors} = []; | ||
| 496 | return 1; | ||
| 497 | } | ||
| 498 | |||
| 499 | sub report_error | ||
| 500 | { | ||
| 501 | my($self, $error) = @_; | ||
| 502 | clear_errors unless $self->{Errors}; | ||
| 503 | push(@{$self->{Errors}}, $error); | ||
| 504 | trace("Always", "ERROR[" . scalar(@{$self->{Errors}}) . "]: $error\n"); | ||
| 505 | } | ||
| 506 | |||
| 507 | sub getErrors | ||
| 508 | { | ||
| 509 | my($self) = @_; | ||
| 510 | return $self->{Errors}; | ||
| 511 | } | ||
| 512 | |||
| 513 | sub getFirstError | ||
| 514 | { | ||
| 515 | my($self) = @_; | ||
| 516 | return $self->{Errors}[0] if $self->{Errors}; | ||
| 517 | return; | ||
| 518 | } | ||
| 519 | |||
| 520 | 1; | ||
| 521 | |||
| 522 | __END__ | ||