    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.net.bsd;
    019
    020import java.io.IOException;
    021
    022/**
    023 * RLoginClient is very similar to {@link org.apache.commons.net.bsd.RCommandClient}, from which it is derived, and uses the rcmd() facility implemented in
    024 * 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
    025 * remote machine from a trusted host, sometimes without issuing a password. The trust relationship is the same as described in the documentation for
    026 * {@link org.apache.commons.net.bsd.RCommandClient}.
    027 * <p>
    028 * 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
    029 * 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
    030 * server, call the {@link #rlogin rlogin()} method, and then fetch the connection's input and output streams. Interaction with the remote command is
    031 * controlled entirely through the I/O streams. Once you have finished processing the streams, you should invoke
    032 * {@link org.apache.commons.net.bsd.RExecClient#disconnect disconnect()} to clean up properly.
    033 * </p>
    034 * <p>
    035 * The standard output and standard error streams of the remote process are transmitted over the same connection, readable from the input stream returned by
    036 * {@link org.apache.commons.net.bsd.RExecClient#getInputStream getInputStream()}
    037 * </p>
    038 * <p>
    039 * Unlike RExecClient and RCommandClient, it is not possible to tell the rlogind daemon to return the standard error stream over a separate connection.
    040 * {@link org.apache.commons.net.bsd.RExecClient#getErrorStream getErrorStream()} will always return null. The standard input of the remote process can be
    041 * written to through the output stream returned by {@link org.apache.commons.net.bsd.RExecClient#getOutputStream getOutputSream()}
    042 * </p>
    043 *
    044 * @see org.apache.commons.net.SocketClient
    045 * @see RExecClient
    046 * @see RCommandClient
    047 */
    048public class RLoginClient extends RCommandClient {
    049
    050    /**
    051     * The default rlogin port. Set to 513 in BSD Unix and according to RFC 1282.
    052     */
    053    public static final int DEFAULT_PORT = 513;
    054
    055    /**
    056     * The default RLoginClient constructor. Initializes the default port to {@code DEFAULT_PORT}.
    057     */
    058    public RLoginClient() {
    059        setDefaultPort(DEFAULT_PORT);
    060    }
    061
    062    /**
    063     * Same as the other rlogin method, but no terminal speed is defined.
    064     *
    065     * @param localUserName  the local user
    066     * @param remoteUserName the remote user
    067     * @param terminalType   the terminal type
    068     * @throws IOException on error
    069     */
    070    public void rlogin(final String localUserName, final String remoteUserName, final String terminalType) throws IOException {
    071        rexec(localUserName, remoteUserName, terminalType, false);
    072    }
    073
    074    /**
    075     * 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
    076     * 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
    077     * typically be able to detect the termination of the remote login shell after reaching end of file on its standard output (accessible through
    078     * {@link #getInputStream getInputStream()}). Disconnecting from the server or closing the process streams before reaching end of file will terminate the
    079     * remote login shell in most cases.
    080     * <p>
    081     * 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
    082     * output stream of the RLoginClient and write the password to the input stream of the RLoginClient.
    083     * </p>
    084     *
    085     * @param localUserName  The user account on the local machine that is trying to log in to the remote host.
    086     * @param remoteUserName The account name on the server that is being logged in to.
    087     * @param terminalType   The name of the user's terminal (e.g., "vt100", "network", etc.)
    088     * @param terminalSpeed  The speed of the user's terminal, expressed as a baud rate or bps (e.g., 9600 or 38400)
    089     * @throws IOException If the rlogin() attempt fails. The exception will contain a message indicating the nature of the failure.
    090     */
    091    public void rlogin(final String localUserName, final String remoteUserName, final String terminalType, final int terminalSpeed) throws IOException {
    092        rexec(localUserName, remoteUserName, terminalType + "/" + terminalSpeed, false);
    093    }
    094
    095}