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 *      http://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
024 * {@link org.apache.commons.net.bsd.RCommandClient},
025 * from which it is derived, and uses the rcmd() facility implemented
026 * in RCommandClient to implement the functionality of the rlogin command that
027 * first appeared in 4.2BSD Unix.  rlogin is a command used to login to
028 * a remote machine from a trusted host, sometimes without issuing a
029 * password.  The trust relationship is the same as described in
030 * the documentation for
031 * {@link org.apache.commons.net.bsd.RCommandClient}.
032 * <p>
033 * As with virtually all of the client classes in org.apache.commons.net, this
034 * class derives from SocketClient.  But it relies on the connection
035 * methods defined  in RcommandClient which ensure that the local Socket
036 * will originate from an acceptable rshell port.  The way to use
037 * RLoginClient is to first connect
038 * to the server, call the {@link #rlogin  rlogin() } method,
039 * and then
040 * fetch the connection's input and output streams.
041 * Interaction with the remote command is controlled entirely through the
042 * I/O streams.  Once you have finished processing the streams, you should
043 * invoke {@link org.apache.commons.net.bsd.RExecClient#disconnect disconnect() }
044 *  to clean up properly.
045 * <p>
046 * The standard output and standard error streams of the
047 * remote process are transmitted over the same connection, readable
048 * from the input stream returned by
049 * {@link org.apache.commons.net.bsd.RExecClient#getInputStream getInputStream() }
050 * .  Unlike RExecClient and RCommandClient, it is
051 * not possible to tell the rlogind daemon to return the standard error
052 * stream over a separate connection.
053 * {@link org.apache.commons.net.bsd.RExecClient#getErrorStream getErrorStream() }
054 *  will always return null.
055 * The standard input of the remote process can be written to through
056 * the output stream returned by
057 * {@link org.apache.commons.net.bsd.RExecClient#getOutputStream getOutputSream() }
058 * .
059 * <p>
060 * <p>
061 * @see org.apache.commons.net.SocketClient
062 * @see RExecClient
063 * @see RCommandClient
064 ***/
065
066public class RLoginClient extends RCommandClient
067{
068    /***
069     * The default rlogin port.  Set to 513 in BSD Unix and according
070     * to RFC 1282.
071     ***/
072    public static final int DEFAULT_PORT = 513;
073
074    /***
075     * The default RLoginClient constructor.  Initializes the
076     * default port to <code> DEFAULT_PORT </code>.
077     ***/
078    public RLoginClient()
079    {
080        setDefaultPort(DEFAULT_PORT);
081    }
082
083
084    /***
085     * Logins into a remote machine through the rlogind daemon on the server
086     * to which the RLoginClient is connected.  After calling this method,
087     * you may interact with the remote login shell through its standard input
088     * and output streams.  Standard error is sent over the same stream as
089     * standard output.  You will typically be able to detect
090     * the termination of the remote login shell after reaching end of file
091     * on its standard output (accessible through
092     * {@link #getInputStream  getInputStream() }.  Disconnecting
093     * from the server or closing the process streams before reaching
094     * end of file will terminate the remote login shell in most cases.
095     * <p>
096     * If user authentication fails, the rlogind daemon will request that
097     * a password be entered interactively.  You will be able to read the
098     * prompt from the output stream of the RLoginClient and write the
099     * password to the input stream of the RLoginClient.
100     * <p>
101     * @param localUsername  The user account on the local machine that is
102     *        trying to login to the remote host.
103     * @param remoteUsername  The account name on the server that is
104     *        being logged in to.
105     * @param terminalType   The name of the user's terminal (e.g., "vt100",
106     *        "network", etc.)
107     * @param terminalSpeed  The speed of the user's terminal, expressed
108     *        as a baud rate or bps (e.g., 9600 or 38400)
109     * @exception IOException If the rlogin() attempt fails.  The exception
110     *            will contain a message indicating the nature of the failure.
111     ***/
112    public void rlogin(String localUsername, String remoteUsername,
113                       String terminalType, int terminalSpeed)
114    throws IOException
115    {
116        rexec(localUsername, remoteUsername, terminalType + "/" + terminalSpeed,
117              false);
118    }
119
120    /***
121     * Same as the other rlogin method, but no terminal speed is defined.
122     ***/
123    public void rlogin(String localUsername, String remoteUsername,
124                       String terminalType)
125    throws IOException
126    {
127        rexec(localUsername, remoteUsername, terminalType, false);
128    }
129
130}