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    *      https://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   * <p>
35   * The standard output and standard error streams of the remote process are transmitted over the same connection, readable from the input stream returned by
36   * {@link org.apache.commons.net.bsd.RExecClient#getInputStream getInputStream()}
37   * </p>
38   * <p>
39   * Unlike RExecClient and RCommandClient, it is not possible to tell the rlogind daemon to return the standard error stream over a separate connection.
40   * {@link org.apache.commons.net.bsd.RExecClient#getErrorStream getErrorStream()} will always return null. The standard input of the remote process can be
41   * written to through the output stream returned by {@link org.apache.commons.net.bsd.RExecClient#getOutputStream getOutputSream()}
42   * </p>
43   *
44   * @see org.apache.commons.net.SocketClient
45   * @see RExecClient
46   * @see RCommandClient
47   */
48  public class RLoginClient extends RCommandClient {
49  
50      /**
51       * The default rlogin port. Set to 513 in BSD Unix and according to RFC 1282.
52       */
53      public static final int DEFAULT_PORT = 513;
54  
55      /**
56       * The default RLoginClient constructor. Initializes the default port to {@code DEFAULT_PORT}.
57       */
58      public RLoginClient() {
59          setDefaultPort(DEFAULT_PORT);
60      }
61  
62      /**
63       * Same as the other rlogin method, but no terminal speed is defined.
64       *
65       * @param localUserName  the local user
66       * @param remoteUserName the remote user
67       * @param terminalType   the terminal type
68       * @throws IOException on error
69       */
70      public void rlogin(final String localUserName, final String remoteUserName, final String terminalType) throws IOException {
71          rexec(localUserName, remoteUserName, terminalType, false);
72      }
73  
74      /**
75       * 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
76       * 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
77       * typically be able to detect the termination of the remote login shell after reaching end of file on its standard output (accessible through
78       * {@link #getInputStream getInputStream()}). Disconnecting from the server or closing the process streams before reaching end of file will terminate the
79       * remote login shell in most cases.
80       * <p>
81       * 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
82       * output stream of the RLoginClient and write the password to the input stream of the RLoginClient.
83       * </p>
84       *
85       * @param localUserName  The user account on the local machine that is trying to log in to the remote host.
86       * @param remoteUserName The account name on the server that is being logged in to.
87       * @param terminalType   The name of the user's terminal (e.g., "vt100", "network", etc.)
88       * @param terminalSpeed  The speed of the user's terminal, expressed as a baud rate or bps (e.g., 9600 or 38400)
89       * @throws IOException If the rlogin() attempt fails. The exception will contain a message indicating the nature of the failure.
90       */
91      public void rlogin(final String localUserName, final String remoteUserName, final String terminalType, final int terminalSpeed) throws IOException {
92          rexec(localUserName, remoteUserName, terminalType + "/" + terminalSpeed, false);
93      }
94  
95  }