| 1 | # | ||
| 2 | # $Id: clear_text.pm,v 1.9 2003/03/02 11:12:10 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 | # | ||
| 35 | # clear-text Access Method for the JUNOS::Access object | ||
| 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 clear-text 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 clear-text connection will fail. | ||
| 60 | # | ||
| 61 | # $self->{clear_text_port} = Optional. The port number of the clear-text server. It's | ||
| 62 | # JUNOSCRIPT_CLEAR_TEXT_PORT by default. | ||
| 63 | # | ||
| 64 | # States maintained by this access method for each clear-text connection, they | ||
| 65 | # can also be read by the client application: | ||
| 66 | # | ||
| 67 | # $self->{xnm_result} = contains the authenticated user if the clear-text | ||
| 68 | # connection was successful. Otherwise, it contains the error message. | ||
| 69 | # | ||
| 70 | # $self->{clear_text_socket} = the file handle of the socket attached to the clear-text | ||
| 71 | # layer. | ||
| 72 | # | ||
| 73 | #========================================================================== | ||
| 74 | package JUNOS::Access::clear_text; | ||
| 75 | |||
| 76 | use strict; | ||
| 77 | use IO::Socket; | ||
| 78 | use XML::Parser; | ||
| 79 | use Term::ReadKey; | ||
| 80 | use JUNOS::Trace; | ||
| 81 | use JUNOS::Access; | ||
| 82 | use JUNOS::Access::xnm; | ||
| 83 | use vars qw(@ISA); | ||
| 84 | @ISA = qw(JUNOS::Access::xnm); | ||
| 85 | |||
| 86 | # | ||
| 87 | # JUNOS::Access::clear_text::start | ||
| 88 | # | ||
| 89 | # Initialize and create a connection to $self->{hostname} | ||
| 90 | # The clear_text port is JUNOSCRIPT_CLEAR_TEXT_PORT unless $self->{clear_text_port} is | ||
| 91 | # already defined to a port number. | ||
| 92 | # | ||
| 93 | use constant JUNOSCRIPT_CLEAR_TEXT_PORT => 3221; | ||
| 94 | sub start | ||
| 95 | { | ||
| 96 | tracept("IO"); | ||
| 97 | my($self) = @_; | ||
| 98 | |||
| 99 | # If port has not been defined, take the default port. | ||
| 100 | my $port = $self->{clear_text_port} || JUNOSCRIPT_CLEAR_TEXT_PORT; | ||
| 101 | |||
| 102 | my $mysock = IO::Socket::INET->new( | ||
| 103 | PeerAddr => $self->{hostname}, | ||
| 104 | PeerPort => $port, | ||
| 105 | Proto => "tcp", | ||
| 106 | Type => SOCK_STREAM, | ||
| 107 | ); | ||
| 108 | |||
| 109 | unless ($mysock) { | ||
| 110 | $self->{JUNOS_Device}->report_error("cannot create socket : $!"); | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | $self->{clear_text_socket} = $mysock; | ||
| 114 | |||
| 115 | # use $|=1 to eleminate buffering on the socket | ||
| 116 | $self->{clear_text_socket}->autoflush(1); | ||
| 117 | |||
| 118 | trace("IO", "start: Connected to $self->{hostname}\n"); | ||
| 119 | |||
| 120 | return 1; | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | # | ||
| 125 | # JUNOS::Access::xnm::send | ||
| 126 | # | ||
| 127 | # You can send an array of data buffers, this subroutine just concatenate | ||
| 128 | # them and send them all at once. | ||
| 129 | # | ||
| 130 | sub send | ||
| 131 | { | ||
| 132 | tracept("IO"); | ||
| 133 | my($self, @data) = @_; | ||
| 134 | return unless ($self->{clear_text_socket}); | ||
| 135 | my $msg = join("", @data); | ||
| 136 | my $res = $self->{clear_text_socket}->send($msg); | ||
| 137 | unless ($res) { | ||
| 138 | $self->{JUNOS_Device}->report_error("Failed to write data: $!"); | ||
| 139 | trace("IO", "send: Failed to write \n"); | ||
| 140 | return; | ||
| 141 | } | ||
| 142 | trace("IO", "send: sent $res bytes:\n"); | ||
| 143 | trace("IO", "send: [[[[[$msg]]]]]\n"); | ||
| 144 | return $res; | ||
| 145 | } | ||
| 146 | |||
| 147 | # | ||
| 148 | # JUNOS::Access::xnm::recv | ||
| 149 | # | ||
| 150 | # This subroutine returns the data it receives. | ||
| 151 | # | ||
| 152 | sub recv | ||
| 153 | { | ||
| 154 | tracept("IO"); | ||
| 155 | my $self = shift; | ||
| 156 | my $data; | ||
| 157 | return unless ($self->{clear_text_socket}); | ||
| 158 | $self->{clear_text_socket}->recv($data, 1024); | ||
| 159 | my $len = length($data)if $data; | ||
| 160 | trace("IO", "recv: received $len bytes:\n"); | ||
| 161 | trace("IO", "recv: [[[[[$data]]]]]\n"); | ||
| 162 | return $data; | ||
| 163 | } | ||
| 164 | |||
| 165 | # | ||
| 166 | # disconnect the clear-text socket and free the context | ||
| 167 | # | ||
| 168 | sub disconnect | ||
| 169 | { | ||
| 170 | tracept("IO"); | ||
| 171 | my($self) = @_; | ||
| 172 | |||
| 173 | |||
| 174 | if ($self->{clear_text_socket}) { | ||
| 175 | close $self->{clear_text_socket}; | ||
| 176 | undef($self->{clear_text_socket}); | ||
| 177 | } | ||
| 178 | |||
| 179 | trace("IO", "disconnect: Disconnected from $self->{hostname}\n"); | ||
| 180 | } | ||
| 181 | |||
| 182 | # | ||
| 183 | # JUNOS::Access::clear_text::incoming | ||
| 184 | # | ||
| 185 | sub incoming | ||
| 186 | { | ||
| 187 | $_[0]; | ||
| 188 | } | ||
| 189 | |||
| 190 | 1; | ||
| 191 | |||
| 192 | __END__ |