Exemple #1
0
        internal static void delPort(Session session, int rport)
        {
            lock (pool) {
                Object[] foo = null;
                for (int i = 0; i < pool.Count; i++)
                {
                    Object[] bar = (Object[])(pool[i]);
                    if (bar[0] != session)
                    {
                        continue;
                    }
                    if (((int)bar[1]) != rport)
                    {
                        continue;
                    }
                    foo = bar;
                    break;
                }
                if (foo == null)
                {
                    return;
                }
                pool.Remove(foo);
            }

            Buffer buf    = new Buffer(100);          // ??
            Packet packet = new Packet(buf);

            try {
                // byte SSH_MSG_GLOBAL_REQUEST 80
                // string "cancel-tcpip-forward"
                // boolean want_reply
                // string  address_to_bind (e.g. "127.0.0.1")
                // uint32  port number to bind
                packet.reset();
                buf.putByte((byte)80 /*SSH_MSG_GLOBAL_REQUEST*/);
                buf.putString(StringAux.getBytesUTF8("cancel-tcpip-forward"));
                buf.putByte((byte)0);
                buf.putString(StringAux.getBytesUTF8("0.0.0.0"));
                buf.putInt(rport);
                session.write(packet);
            }
            catch            //(Exception e)
            {
                //    throw new JSchException(e.toString());
            }
        }
Exemple #2
0
        public void request(Session session, AChannel channel)
        {
            Packet packet = session.packet;
            Buffer buf    = session.buf;

            // send
            // byte     SSH_MSG_CHANNEL_REQUEST(98)
            // uint32 recipient channel
            // string request type       // "exec"
            // boolean want reply        // 0
            // string command
            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.getRecipient());
            buf.putString(StringAux.getBytesUTF8("exec"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putString(StringAux.getBytesUTF8(command));
            session.write(packet);
        }
Exemple #3
0
        public void request(Session session, AChannel channel)
        {
            Buffer buf    = new Buffer();
            Packet packet = new Packet(buf);

            bool reply = waitForReply();

            if (reply)
            {
                channel.reply = -1;
            }

            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.getRecipient());
            buf.putString(StringAux.getBytesUTF8("subsystem"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putString(StringAux.getBytesUTF8(subsystem));
            session.write(packet);

            if (reply)
            {
                while (channel.reply == -1)
                {
                    try {
                        ThreadAux.Sleep(10);
                    }
                    catch (System.Exception) {
                    }
                }
                if (channel.reply == 0)
                {
                    throw new SshClientException("failed to send subsystem request");
                }
            }
        }
Exemple #4
0
        public void connect(ISocketFactory socket_factory, String host, int port, int timeout)
        {
            try {
                if (socket_factory == null)
                {
                    socket = TcpSocketCreator.CreateSocket(proxy_host, proxy_port, timeout);
                    ins    = socket.getInputStream();
                    outs   = socket.getOutputStream();
                }
                else
                {
                    socket = socket_factory.createSocket(proxy_host, proxy_port);
                    ins    = socket_factory.getInputStream(socket);
                    outs   = socket_factory.getOutputStream(socket);
                }
                if (timeout > 0)
                {
                    socket.setSoTimeout(timeout);
                }
                socket.setTcpNoDelay(true);

                StreamAux.write(outs, StringAux.getBytesUTF8("CONNECT " + host + ":" + port + " HTTP/1.0\r\n"));

                if (user != null && passwd != null)
                {
                    byte[] _code = StringAux.getBytesUTF8((user + ":" + passwd));
                    _code = StringAux.toBase64(_code, 0, _code.Length);
                    StreamAux.write(outs, StringAux.getBytesUTF8("Proxy-Authorization: Basic "));
                    StreamAux.write(outs, _code);
                    StreamAux.write(outs, StringAux.getBytesUTF8("\r\n"));
                }

                StreamAux.write(outs, StringAux.getBytesUTF8("\r\n"));
                StreamAux.flush(outs);

                int foo = 0;

                StringBuilder sb = new StringBuilder();
                while (foo >= 0)
                {
                    foo = ins.ReadByte();
                    if (foo != 13)
                    {
                        sb.Append((char)foo);
                        continue;
                    }
                    foo = ins.ReadByte();
                    if (foo != 10)
                    {
                        continue;
                    }
                    break;
                }
                if (foo < 0)
                {
                    throw new System.IO.IOException();
                }

                String response = sb.ToString();
                String reason   = "Unknow reason";
                int    code     = -1;
                try {
                    foo = response.IndexOf(' ');
                    int bar = response.IndexOf(' ', foo + 1);
                    code   = int.Parse(response.Substring(foo + 1, bar - (foo + 1)));
                    reason = response.Substring(bar + 1);
                }
                catch                //(JException e)
                {
                }
                if (code != 200)
                {
                    throw new System.IO.IOException("proxy error: " + reason);
                }

                /*
                 * while(foo>=0){
                 * foo=in.read(); if(foo!=13) continue;
                 * foo=in.read(); if(foo!=10) continue;
                 * foo=in.read(); if(foo!=13) continue;
                 * foo=in.read(); if(foo!=10) continue;
                 * break;
                 * }
                 */

                int count = 0;
                while (true)
                {
                    count = 0;
                    while (foo >= 0)
                    {
                        foo = ins.ReadByte();
                        if (foo != 13)
                        {
                            count++;
                            continue;
                        }
                        foo = ins.ReadByte();
                        if (foo != 10)
                        {
                            continue;
                        }
                        break;
                    }
                    if (foo < 0)
                    {
                        throw new System.IO.IOException();
                    }
                    if (count == 0)
                    {
                        break;
                    }
                }
            }
            catch (RuntimeException e) {
                try {
                    if (socket != null)
                    {
                        socket.close();
                    }
                }
                catch                //(JException eee)
                {
                }
                String message = "ProxyHTTP: " + e.ToString();
                throw e;
            }
        }
Exemple #5
0
        public override bool start(Session session)
        {
            //    super.start(session);
            //System.out.println("UserAuthPassword: start");
            Packet packet   = session.packet;
            Buffer buf      = session.buf;
            String username = session.username;
            String password = session.password;
            String dest     = username + "@" + session.host;

            if (session.port != 22)
            {
                dest += (":" + session.port);
            }

            while (true)
            {
                if (password == null)
                {
                    if (userinfo == null)
                    {
                        //throw new JSchException("USERAUTH fail");
                        return(false);
                    }
                    if (!userinfo.promptPassword("Password for " + dest))
                    {
                        throw new SshClientAuthCancelException("password");
                        //break;
                    }
                    password = userinfo.getPassword();
                    if (password == null)
                    {
                        throw new SshClientAuthCancelException("password");
                        //break;
                    }
                }

                byte[] _username = null;
                try {
                    _username = StringAux.getBytesUTF8(username);
                }
                catch {                //(java.io.UnsupportedEncodingException e){
                    _username = StringAux.getBytes(username);
                }

                byte[] _password = null;
                try {
                    _password = StringAux.getBytesUTF8(password);
                }
                catch {                //(java.io.UnsupportedEncodingException e){
                    _password = StringAux.getBytes(password);
                }

                // send
                // byte      SSH_MSG_USERAUTH_REQUEST(50)
                // string    user name
                // string    service name ("ssh-connection")
                // string    "password"
                // boolen    FALSE
                // string    plaintext password (ISO-10646 UTF-8)
                packet.reset();
                buf.putByte((byte)Session.SSH_MSG_USERAUTH_REQUEST);
                buf.putString(_username);
                buf.putString(StringAux.getBytes("ssh-connection"));
                buf.putString(StringAux.getBytes("password"));
                buf.putByte((byte)0);
                buf.putString(_password);
                session.write(packet);

loop:
                while (true)
                {
                    // receive
                    // byte      SSH_MSG_USERAUTH_SUCCESS(52)
                    // string    service name
                    buf = session.read(buf);
                    //System.out.println("read: 52 ? "+    buf.buffer[5]);
                    if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_SUCCESS)
                    {
                        return(true);
                    }
                    if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_BANNER)
                    {
                        buf.getInt();
                        buf.getByte();
                        buf.getByte();
                        byte[] _message = buf.getString();
                        byte[] lang     = buf.getString();
                        String message  = null;
                        try {
                            message = StringAux.getStringUTF8(_message);
                        }
                        catch {                        //(java.io.UnsupportedEncodingException e){
                            message = StringAux.getString(_message);
                        }
                        if (userinfo != null)
                        {
                            userinfo.showMessage(message);
                        }
                        goto loop;
                    }
                    if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_FAILURE)
                    {
                        buf.getInt();
                        buf.getByte();
                        buf.getByte();
                        byte[] foo             = buf.getString();
                        int    partial_success = buf.getByte();
                        //System.out.println(new String(foo)+
                        //		 " partial_success:"+(partial_success!=0));
                        if (partial_success != 0)
                        {
                            throw new SshClientPartialAuthException(StringAux.getString(foo));
                        }
                        break;
                    }
                    else
                    {
                        //        System.out.println("USERAUTH fail ("+buf.buffer[5]+")");
                        //	  throw new JSchException("USERAUTH fail ("+buf.buffer[5]+")");
                        return(false);
                    }
                }
                password = null;
            }
            //throw new JSchException("USERAUTH fail");
            //return false;
        }
Exemple #6
0
        public override bool start(Session session)
        {
            //super.start(session);

            //Vector identities=JSch.identities;
            System.Collections.ArrayList identities = session.jsch.identities;

            Packet packet = session.packet;
            Buffer buf    = session.buf;

            String passphrase = null;
            String username   = session.username;

            byte[] _username = null;
            try {
                _username = StringAux.getBytesUTF8(username);
            }
            catch {            //(java.io.UnsupportedEncodingException e){
                _username = StringAux.getBytes(username);
            }

            for (int i = 0; i < identities.Count; i++)
            {
                IIdentity identity   = (IIdentity)(identities[i]);
                byte[]    pubkeyblob = identity.getPublicKeyBlob();

                //System.out.println("UserAuthPublicKey: "+identity+" "+pubkeyblob);

                if (pubkeyblob != null)
                {
                    // send
                    // byte      SSH_MSG_USERAUTH_REQUEST(50)
                    // string    user name
                    // string    service name ("ssh-connection")
                    // string    "publickey"
                    // boolen    FALSE
                    // string    plaintext password (ISO-10646 UTF-8)
                    packet.reset();
                    buf.putByte((byte)Session.SSH_MSG_USERAUTH_REQUEST);
                    buf.putString(_username);
                    buf.putString(StringAux.getBytes("ssh-connection"));
                    buf.putString(StringAux.getBytes("publickey"));
                    buf.putByte((byte)0);
                    buf.putString(StringAux.getBytes(identity.getAlgName()));
                    buf.putString(pubkeyblob);
                    session.write(packet);

loop1:
                    while (true)
                    {
                        // receive
                        // byte      SSH_MSG_USERAUTH_PK_OK(52)
                        // string    service name
                        buf = session.read(buf);
                        //System.out.println("read: 60 ? "+    buf.buffer[5]);
                        if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_PK_OK)
                        {
                            break;
                        }
                        else if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_FAILURE)
                        {
                            //	System.out.println("USERAUTH publickey "+session.getIdentity()+
                            //			   " is not acceptable.");
                            break;
                        }
                        else if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_BANNER)
                        {
                            buf.getInt();
                            buf.getByte();
                            buf.getByte();
                            byte[] _message = buf.getString();
                            byte[] lang     = buf.getString();
                            String message  = null;
                            try {
                                message = StringAux.getStringUTF8(_message);
                            }
                            catch {                            //(java.io.UnsupportedEncodingException e){
                                message = StringAux.getString(_message);
                            }
                            if (userinfo != null)
                            {
                                userinfo.showMessage(message);
                            }
                            goto loop1;
                        }
                        else
                        {
                            //System.out.println("USERAUTH fail ("+buf.buffer[5]+")");
                            //throw new JSchException("USERAUTH fail ("+buf.buffer[5]+")");
                            break;
                        }
                    }
                    if (buf.buffer[5] != Session.SSH_MSG_USERAUTH_PK_OK)
                    {
                        continue;
                    }
                }

                //System.out.println("UserAuthPublicKey: identity.isEncrypted()="+identity.isEncrypted());

                int count = 5;
                while (true)
                {
                    if ((identity.isEncrypted() && passphrase == null))
                    {
                        if (userinfo == null)
                        {
                            throw new SshClientException("USERAUTH fail");
                        }
                        if (identity.isEncrypted() &&
                            !userinfo.promptPassphrase("Passphrase for " + identity.getName()))
                        {
                            throw new SshClientAuthCancelException("publickey");
                            //throw new JSchException("USERAUTH cancel");
                            //break;
                        }
                        passphrase = userinfo.getPassphrase();
                    }

                    if (!identity.isEncrypted() || passphrase != null)
                    {
                        //System.out.println("UserAuthPublicKey: @1 "+passphrase);
                        if (identity.setPassphrase(passphrase))
                        {
                            break;
                        }
                    }
                    passphrase = null;
                    count--;
                    if (count == 0)
                    {
                        break;
                    }
                }

                //System.out.println("UserAuthPublicKey: identity.isEncrypted()="+identity.isEncrypted());

                if (identity.isEncrypted())
                {
                    continue;
                }
                if (pubkeyblob == null)
                {
                    pubkeyblob = identity.getPublicKeyBlob();
                }

                //System.out.println("UserAuthPublicKey: pubkeyblob="+pubkeyblob);

                if (pubkeyblob == null)
                {
                    continue;
                }

                // send
                // byte      SSH_MSG_USERAUTH_REQUEST(50)
                // string    user name
                // string    service name ("ssh-connection")
                // string    "publickey"
                // boolen    TRUE
                // string    plaintext password (ISO-10646 UTF-8)
                packet.reset();
                buf.putByte((byte)Session.SSH_MSG_USERAUTH_REQUEST);
                buf.putString(_username);
                buf.putString(StringAux.getBytes("ssh-connection"));
                buf.putString(StringAux.getBytes("publickey"));
                buf.putByte((byte)1);
                buf.putString(StringAux.getBytes(identity.getAlgName()));
                buf.putString(pubkeyblob);

                //      byte[] tmp=new byte[buf.index-5];
                //      System.arraycopy(buf.buffer, 5, tmp, 0, tmp.length);
                //      buf.putString(signature);

                byte[] sid    = session.getSessionId();
                uint   sidlen = (uint)sid.Length;
                byte[] tmp    = new byte[4 + sidlen + buf.index - 5];
                tmp[0] = (byte)(sidlen >> 24);
                tmp[1] = (byte)(sidlen >> 16);
                tmp[2] = (byte)(sidlen >> 8);
                tmp[3] = (byte)(sidlen);
                Array.Copy(sid, 0, tmp, 4, sidlen);
                Array.Copy(buf.buffer, 5, tmp, 4 + sidlen, buf.index - 5);

                byte[] signature = identity.getSignature(session, tmp);
                if (signature == null)                    // for example, too long key length.
                {
                    break;
                }
                buf.putString(signature);

                session.write(packet);

loop2:
                while (true)
                {
                    // receive
                    // byte      SSH_MSG_USERAUTH_SUCCESS(52)
                    // string    service name
                    buf = session.read(buf);
                    //System.out.println("read: 52 ? "+    buf.buffer[5]);
                    if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_SUCCESS)
                    {
                        return(true);
                    }
                    else if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_BANNER)
                    {
                        buf.getInt();
                        buf.getByte();
                        buf.getByte();
                        byte[] _message = buf.getString();
                        byte[] lang     = buf.getString();
                        String message  = null;
                        try {
                            message = StringAux.getStringUTF8(_message);
                        }
                        catch {                        //(java.io.UnsupportedEncodingException e){
                            message = StringAux.getString(_message);
                        }
                        if (userinfo != null)
                        {
                            userinfo.showMessage(message);
                        }
                        goto loop2;
                    }
                    else if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_FAILURE)
                    {
                        buf.getInt();
                        buf.getByte();
                        buf.getByte();
                        byte[] foo             = buf.getString();
                        int    partial_success = buf.getByte();
                        //System.out.println(new String(foo)+
                        //                   " partial_success:"+(partial_success!=0));
                        if (partial_success != 0)
                        {
                            throw new SshClientPartialAuthException(StringAux.getString(foo));
                        }
                        break;
                    }
                    //System.out.println("USERAUTH fail ("+buf.buffer[5]+")");
                    //throw new JSchException("USERAUTH fail ("+buf.buffer[5]+")");
                    break;
                }
            }
            return(false);
        }
        public override bool start(Session session)
        {
            base.start(session);
            //System.out.println("UserAuthNone: start");
            Packet packet   = session.packet;
            Buffer buf      = session.buf;
            String username = session.username;

            byte[] _username = null;
            try {
                _username = StringAux.getBytesUTF8(username);
            }
            catch {            //(java.io.UnsupportedEncodingException e){
                _username = StringAux.getBytes(username);
            }

            // send
            // byte      SSH_MSG_USERAUTH_REQUEST(50)
            // string    user name
            // string    service name ("ssh-connection")
            // string    "none"
            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_USERAUTH_REQUEST);
            buf.putString(_username);
            buf.putString(StringAux.getBytes("ssh-connection"));
            buf.putString(StringAux.getBytes("none"));
            session.write(packet);

loop:
            while (true)
            {
                // receive
                // byte      SSH_MSG_USERAUTH_SUCCESS(52)
                // string    service name
                buf = session.read(buf);
                //System.out.println("UserAuthNone: read: 52 ? "+    buf.buffer[5]);
                if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_SUCCESS)
                {
                    return(true);
                }
                if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_BANNER)
                {
                    buf.getInt();
                    buf.getByte();
                    buf.getByte();
                    byte[] _message = buf.getString();
                    byte[] lang     = buf.getString();
                    String message  = null;
                    try {
                        message = StringAux.getStringUTF8(_message);
                    }
                    catch {                    //(java.io.UnsupportedEncodingException e){
                        message = StringAux.getString(_message);
                    }
                    if (userinfo != null)
                    {
                        userinfo.showMessage(message);
                    }
                    goto loop;
                }
                if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_FAILURE)
                {
                    buf.getInt();
                    buf.getByte();
                    buf.getByte();
                    byte[] foo             = buf.getString();
                    int    partial_success = buf.getByte();
                    methods = StringAux.getString(foo);
                    //System.out.println("UserAuthNONE: "+methods+
                    //		   " partial_success:"+(partial_success!=0));
                    //	if(partial_success!=0){
                    //	  throw new JSchPartialAuthException(new String(foo));
                    //	}
                    break;
                }
                else
                {
                    //      System.out.println("USERAUTH fail ("+buf.buffer[5]+")");
                    throw new SshClientException("USERAUTH fail (" + buf.buffer[5] + ")");
                }
            }
            //throw new JSchException("USERAUTH fail");
            return(false);
        }