View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.bsd;
19  
20  import java.io.IOException;
21  
22  /**
23   * RLoginClient is very similar to {@link org.apache.commons.net.bsd.RCommandClient}, from which it is derived, and uses the rcmd() facility implemented in
24   * RCommandClient to implement the functionality of the rlogin command that first appeared in 4.2BSD Unix. rlogin is a command used to log in to a
25   * remote machine from a trusted host, sometimes without issuing a password. The trust relationship is the same as described in the documentation for
26   * {@link org.apache.commons.net.bsd.RCommandClient}.
27   * <p>
28   * As with virtually all the client classes in org.apache.commons.net, this class derives from SocketClient. But it relies on the connection methods defined
29   * in RcommandClient which ensure that the local Socket will originate from an acceptable rshell port. The way to use RLoginClient is to first connect to the
30   * server, call the {@link #rlogin rlogin() } method, and then fetch the connection's input and output streams. Interaction with the remote command is
31   * controlled entirely through the I/O streams. Once you have finished processing the streams, you should invoke
32   * {@link org.apache.commons.net.bsd.RExecClient#disconnect disconnect() } to clean up properly.
33   * <p>
34   * The standard output and standard error streams of the remote process are transmitted over the same connection, readable from the input stream returned by
35   * {@link org.apache.commons.net.bsd.RExecClient#getInputStream getInputStream() }
36   * <p>
37   * Unlike RExecClient and RCommandClient, it is not possible to tell the rlogind daemon to return the standard error stream over a separate connection.
38   * {@link org.apache.commons.net.bsd.RExecClient#getErrorStream getErrorStream() } will always return null. The standard input of the remote process can be
39   * written to through the output stream returned by {@link org.apache.commons.net.bsd.RExecClient#getOutputStream getOutputSream() }
40   *
41   * @see org.apache.commons.net.SocketClient
42   * @see RExecClient
43   * @see RCommandClient
44   */
45  
46  public class RLoginClient extends RCommandClient {
47      /**
48       * The default rlogin port. Set to 513 in BSD Unix and according to RFC 1282.
49       */
50      public static final int DEFAULT_PORT = 513;
51  
52      /**
53       * The default RLoginClient constructor. Initializes the default port to <code> DEFAULT_PORT </code>.
54       */
55      public RLoginClient() {
56          setDefaultPort(DEFAULT_PORT);
57      }
58  
59      /**
60       * Same as the other rlogin method, but no terminal speed is defined.
61       *
62       * @param localUsername  the local user
63       * @param remoteUsername the remote user
64       * @param terminalType   the terminal type
65       * @throws IOException on error
66       */
67      public void rlogin(final String localUsername, final String remoteUsername, final String terminalType) throws IOException {
68          rexec(localUsername, remoteUsername, terminalType, false);
69      }
70  
71      /**
72       * Logins into a remote machine through the rlogind daemon on the server to which the RLoginClient is connected. After calling this method, you may interact
73       * with the remote login shell through its standard input and output streams. Standard error is sent over the same stream as standard output. You will
74       * typically be able to detect the termination of the remote login shell after reaching end of file on its standard output (accessible through
75       * {@link #getInputStream getInputStream()}). Disconnecting from the server or closing the process streams before reaching end of file will terminate the
76       * remote login shell in most cases.
77       * <p>
78       * If user authentication fails, the rlogind daemon will request that a password be entered interactively. You will be able to read the prompt from the
79       * output stream of the RLoginClient and write the password to the input stream of the RLoginClient.
80       *
81       * @param localUsername  The user account on the local machine that is trying to log in to the remote host.
82       * @param remoteUsername The account name on the server that is being logged in to.
83       * @param terminalType   The name of the user's terminal (e.g., "vt100", "network", etc.)
84       * @param terminalSpeed  The speed of the user's terminal, expressed as a baud rate or bps (e.g., 9600 or 38400)
85       * @throws IOException If the rlogin() attempt fails. The exception will contain a message indicating the nature of the failure.
86       */
87      public void rlogin(final String localUsername, final String remoteUsername, final String terminalType, final int terminalSpeed) throws IOException {
88          rexec(localUsername, remoteUsername, terminalType + "/" + terminalSpeed, false);
89      }
90  
91  }