Beispiel #1
0
 void dump(FileStream outs)
 {
     //StreamWriter outs = new StreamWriter(fs);
     try {
         HostKey hk;
         for (int i = 0; i < pool.Count; i++)
         {
             hk = (HostKey)(pool[i]);
             //hk.dump(out);
             String host = hk.getHost();
             String type = hk.getType();
             if (type.Equals("UNKNOWN"))
             {
                 Write(outs, StringAux.getBytes(host));
                 Write(outs, cr);
                 continue;
             }
             Write(outs, StringAux.getBytes(host));
             Write(outs, space);
             Write(outs, StringAux.getBytes(type));
             Write(outs, space);
             Write(outs, StringAux.getBytes(hk.getKey()));
             Write(outs, cr);
         }
         outs.Flush();
     }
     catch (Exception e) {
         Console.WriteLine(e);
     }
 }
Beispiel #2
0
 public void writeSECSHPublicKey(Stream outs, String comment)
 {
     byte[] pubblob = getPublicKeyBlob();
     byte[] pub     = StringAux.toBase64(pubblob, 0, pubblob.Length);
     try {
         Write(outs, StringAux.getBytes("---- BEGIN SSH2 PUBLIC KEY ----"));
         Write(outs, cr);
         Write(outs, StringAux.getBytes("Comment: \"" + comment + "\""));
         Write(outs, cr);
         int index = 0;
         while (index < pub.Length)
         {
             int len = 70;
             if ((pub.Length - index) < len)
             {
                 len = pub.Length - index;
             }
             outs.Write(pub, index, len);
             Write(outs, cr);
             index += len;
         }
         Write(outs, StringAux.getBytes("---- END SSH2 PUBLIC KEY ----"));
         Write(outs, cr);
     }
     catch (Exception e) {
         Console.WriteLine(e);
     }
 }
Beispiel #3
0
 internal static void setCookie(String foo)
 {
     cookie_hex = StringAux.getBytes(foo);
     cookie     = new byte[16];
     for (int i = 0; i < 16; i++)
     {
         cookie[i] = (byte)(((revtable(cookie_hex[i * 2]) << 4) & 0xf0) |
                            ((revtable(cookie_hex[i * 2 + 1])) & 0xf));
     }
 }
Beispiel #4
0
 public bool decrypt(String _passphrase)
 {
     byte[] passphrase = StringAux.getBytes(_passphrase);
     byte[] foo        = decrypt(data, passphrase, iv);
     if (parse(foo))
     {
         encrypted = false;
     }
     return(!encrypted);
 }
Beispiel #5
0
 public void setPassphrase(String passphrase)
 {
     if (passphrase == null || passphrase.Length == 0)
     {
         setPassphrase((byte[])null);
     }
     else
     {
         setPassphrase(StringAux.getBytes(passphrase));
     }
 }
        public void request(Session session, AChannel channel)
        {
            Buffer buf    = new Buffer();
            Packet packet = new Packet(buf);

            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.getRecipient());
            buf.putString(StringAux.getBytes("signal"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putString(StringAux.getBytes(signal));
            session.write(packet);
        }
Beispiel #7
0
 public void writePublicKey(Stream outs, String comment)
 {
     byte[] pubblob = getPublicKeyBlob();
     byte[] pub     = StringAux.toBase64(pubblob, 0, pubblob.Length);
     try {
         Write(outs, getKeyTypeName());
         Write(outs, space);
         outs.Write(pub, 0, pub.Length);
         Write(outs, space);
         Write(outs, StringAux.getBytes(comment));
         Write(outs, cr);
     }
     catch (Exception e) {
         Console.WriteLine(e);
     }
 }
Beispiel #8
0
        public void request(Session session, AChannel channel)
        {
            Buffer buf    = new Buffer();
            Packet packet = new Packet(buf);

            // send
            // byte     SSH_MSG_CHANNEL_REQUEST(98)
            // uint32 recipient channel
            // string request type       // "shell"
            // boolean want reply        // 0
            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.getRecipient());
            buf.putString(StringAux.getBytes("shell"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            session.write(packet);
        }
Beispiel #9
0
        public virtual bool start(Session session)
        {
            Packet packet = session.packet;
            Buffer buf    = session.buf;

            // send
            // byte      SSH_MSG_SERVICE_REQUEST(5)
            // string    service name "ssh-userauth"
            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_SERVICE_REQUEST);
            buf.putString(StringAux.getBytes("ssh-userauth"));
            session.write(packet);

            // receive
            // byte      SSH_MSG_SERVICE_ACCEPT(6)
            // string    service name
            buf = session.read(buf);
            //System.out.println("read: 6 ? "+buf.buffer[5]);
            return(buf.buffer[5] == 6);
        }
Beispiel #10
0
        internal ChannelX11()
            : base()
        {
            setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
            setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
            setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);

            type = StringAux.getBytes("x11");
            try {
                IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry(host).AddressList[0], port);
                socket = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
                socket.Connect(ep);
                io = new IO();
                NetworkStream ns = new NetworkStream(socket);
                io.setInputStream(ns);
                io.setOutputStream(ns);
            }
            catch (Exception e) {
                Console.WriteLine(e);
            }
        }
Beispiel #11
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.getBytes("subsystem"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putString(StringAux.getBytes("sftp"));
            session.write(packet);

            if (reply)
            {
                while (channel.reply == -1)
                {
                    try {
                        System.Threading.Thread.Sleep(10);
                    }
                    catch                    //(Exception ee)
                    {
                    }
                }
                if (channel.reply == 0)
                {
                    throw new SshClientException("failed to send sftp request");
                }
            }
        }
Beispiel #12
0
 internal void dump(Buffer buf)
 {
     buf.putInt(flags);
     if ((flags & SSH_FILEXFER_ATTR_SIZE) != 0)
     {
         buf.putLong(size);
     }
     if ((flags & SSH_FILEXFER_ATTR_UIDGID) != 0)
     {
         buf.putInt(uid);
         buf.putInt(gid);
     }
     if ((flags & SSH_FILEXFER_ATTR_PERMISSIONS) != 0)
     {
         buf.putInt(permissions);
     }
     if ((flags & SSH_FILEXFER_ATTR_ACMODTIME) != 0)
     {
         buf.putInt(atime);
     }
     if ((flags & SSH_FILEXFER_ATTR_ACMODTIME) != 0)
     {
         buf.putInt(mtime);
     }
     if ((flags & SSH_FILEXFER_ATTR_EXTENDED) != 0)
     {
         int count = extended.Length / 2;
         if (count > 0)
         {
             for (int i = 0; i < count; i++)
             {
                 buf.putString(StringAux.getBytes(extended[i * 2]));
                 buf.putString(StringAux.getBytes(extended[i * 2 + 1]));
             }
         }
     }
 }
        public void request(Session session, AChannel channel)
        {
            Buffer buf    = new Buffer();
            Packet packet = new Packet(buf);

            //byte      SSH_MSG_CHANNEL_REQUEST
            //uint32    recipient_channel
            //string    "window-change"
            //boolean   FALSE
            //uint32    terminal width, columns
            //uint32    terminal height, rows
            //uint32    terminal width, pixels
            //uint32    terminal height, pixels
            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.getRecipient());
            buf.putString(StringAux.getBytes("window-change"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putInt(width_columns);
            buf.putInt(height_rows);
            buf.putInt(width_pixels);
            buf.putInt(height_pixels);
            session.write(packet);
        }
Beispiel #14
0
        public void request(Session session, AChannel channel)
        {
            Buffer buf    = new Buffer();
            Packet packet = new Packet(buf);

            // byte      SSH_MSG_CHANNEL_REQUEST(98)
            // uint32 recipient channel
            // string request type        // "x11-req"
            // boolean want reply         // 0
            // boolean   single connection
            // string    x11 authentication protocol // "MIT-MAGIC-COOKIE-1".
            // string    x11 authentication cookie
            // uint32    x11 screen number
            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.getRecipient());
            buf.putString(StringAux.getBytes("x11-req"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putByte((byte)0);
            buf.putString(StringAux.getBytes("MIT-MAGIC-COOKIE-1"));
            buf.putString(ChannelX11.getFakedCookie(session));
            buf.putInt(0);
            session.write(packet);
        }
        public override void connect()
        {
            try {
                if (!session.isConnected())
                {
                    throw new SshClientException("session is down");
                }
                Buffer buf    = new Buffer(150);
                Packet packet = new Packet(buf);
                // send
                // byte   SSH_MSG_CHANNEL_OPEN(90)
                // string channel type         //
                // uint32 sender channel       // 0
                // uint32 initial window size  // 0x100000(65536)
                // uint32 maxmum packet size   // 0x4000(16384)

                packet.reset();
                buf.putByte((byte)90);
                buf.putString(StringAux.getBytes("direct-tcpip"));
                buf.putInt(id);
                buf.putInt(lwsize);
                buf.putInt(lmpsize);
                buf.putString(StringAux.getBytes(host));
                buf.putInt(port);
                buf.putString(StringAux.getBytes(originator_IP_address));
                buf.putInt(originator_port);
                session.write(packet);

                int retry = 1000;
                try {
                    while (this.getRecipient() == -1 &&
                           session.isConnected() &&
                           retry > 0 &&
                           !_eof_remote)
                    {
                        //Thread.sleep(500);
                        ThreadAux.Sleep(50);
                        retry--;
                    }
                }
                catch {
                }

                if (!session.isConnected())
                {
                    throw new SshClientException("session is down");
                }
                if (retry == 0 || this._eof_remote)
                {
                    throw new SshClientException("channel is not opened.");
                }

                /*
                 * if(this.eof_remote){      // failed to open
                 * disconnect();
                 * return;
                 * }
                 */

                connected = true;

                thread = new ThreadAux(this);
                thread.start();
            }
            catch (Exception e) {
                io.close();
                io = null;
                AChannel.del(this);
                if (e is SshClientException)
                {
                    throw (SshClientException)e;
                }
            }
        }
Beispiel #16
0
        internal void setKnownHosts(StreamReader foo)
        {
            pool.Clear();
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            byte i;
            int  j;
            bool error = false;

            try {
                StreamReader fis = foo;
                String       host;
                String       key = null;
                int          type;
                byte[]       buf  = new byte[1024];
                int          bufl = 0;
loop:
                while (true)
                {
                    bufl = 0;
                    while (true)
                    {
                        j = fis.Read();
                        if (j == -1)
                        {
                            goto break_loop;
                        }
                        if (j == 0x0d)
                        {
                            continue;
                        }
                        if (j == 0x0a)
                        {
                            break;
                        }
                        buf[bufl++] = (byte)j;
                    }

                    j = 0;
                    while (j < bufl)
                    {
                        i = buf[j];
                        if (i == ' ' || i == '\t')
                        {
                            j++;
                            continue;
                        }
                        if (i == '#')
                        {
                            addInvalidLine(System.Text.Encoding.Default.GetString(buf, 0, bufl));
                            goto loop;
                        }
                        break;
                    }
                    if (j >= bufl)
                    {
                        addInvalidLine(System.Text.Encoding.Default.GetString(buf, 0, bufl));
                        goto loop;
                    }

                    sb.Length = 0;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == 0x20 || i == '\t')
                        {
                            break;
                        }
                        sb.Append((char)i);
                    }
                    host = sb.ToString();
                    if (j >= bufl || host.Length == 0)
                    {
                        addInvalidLine(System.Text.Encoding.Default.GetString(buf, 0, bufl));
                        goto loop;
                    }

                    sb.Length = 0;
                    type      = -1;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == 0x20 || i == '\t')
                        {
                            break;
                        }
                        sb.Append((char)i);
                    }
                    if (sb.ToString().Equals("ssh-dss"))
                    {
                        type = HostKey.SSHDSS;
                    }
                    else if (sb.ToString().Equals("ssh-rsa"))
                    {
                        type = HostKey.SSHRSA;
                    }
                    else
                    {
                        j = bufl;
                    }
                    if (j >= bufl)
                    {
                        addInvalidLine(StringAux.getString(buf, 0, bufl));
                        goto loop;
                    }

                    sb.Length = 0;
                    while (j < bufl)
                    {
                        i = buf[j++];
                        if (i == 0x0d)
                        {
                            continue;
                        }
                        if (i == 0x0a)
                        {
                            break;
                        }
                        sb.Append((char)i);
                    }
                    key = sb.ToString();
                    if (key.Length == 0)
                    {
                        addInvalidLine(StringAux.getString(buf, 0, bufl));
                        goto loop;
                    }

                    //System.out.println(host);
                    //System.out.println("|"+key+"|");

                    HostKey hk = new HostKey(host, type,
                                             StringAux.fromBase64(StringAux.getBytes(key), 0,
                                                                  key.Length));
                    pool.Add(hk);
                }

break_loop:

                fis.Close();
                if (error)
                {
                    throw new SshClientException("KnownHosts: invalid format");
                }
            }
            catch (Exception e) {
                if (e is SshClientException)
                {
                    throw (SshClientException)e;
                }
                throw new SshClientException(e.ToString());
            }
        }
Beispiel #17
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)
        {
            //System.out.println("UserAuthKeyboardInteractive: start");
            Packet packet   = session.packet;
            Buffer buf      = session.buf;
            String username = session.username;
            String dest     = username + "@" + session.host;

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

            bool cancel = false;

            byte[] _username = null;
            try {
                _username = System.Text.Encoding.UTF8.GetBytes(username);
            }
            catch {
                _username = StringAux.getBytes(username);
            }

            while (true)
            {
                // send
                // byte      SSH_MSG_USERAUTH_REQUEST(50)
                // string    user name (ISO-10646 UTF-8, as defined in [RFC-2279])
                // string    service name (US-ASCII) "ssh-userauth" ? "ssh-connection"
                // string    "keyboard-interactive" (US-ASCII)
                // string    language tag (as defined in [RFC-3066])
                // string    submethods (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("ssh-userauth".getBytes());
                buf.putString(StringAux.getBytes("keyboard-interactive"));
                buf.putString(StringAux.getBytes(""));
                buf.putString(StringAux.getBytes(""));
                session.write(packet);

                bool firsttime = true;
loop:
                while (true)
                {
                    // receive
                    // byte      SSH_MSG_USERAUTH_SUCCESS(52)
                    // string    service name
                    try {
                        buf = session.read(buf);
                    }
                    catch (SshClientException e) {
                        e.GetType();
                        return(false);
                    }
                    catch (System.IO.IOException e) {
                        e.GetType();
                        return(false);
                    }
                    //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 {
                            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));
                        }

                        if (firsttime)
                        {
                            throw new SshClientException("USERAUTH KI is not supported");
                            //return false;
                            //cancel=true;  // ??
                        }
                        break;
                    }
                    if (buf.buffer[5] == Session.SSH_MSG_USERAUTH_INFO_REQUEST)
                    {
                        firsttime = false;
                        buf.getInt();
                        buf.getByte();
                        buf.getByte();
                        String name         = StringAux.getString(buf.getString());
                        String instruction  = StringAux.getString(buf.getString());
                        String languate_tag = StringAux.getString(buf.getString());
                        int    num          = buf.getInt();
                        //System.out.println("name: "+name);
                        //System.out.println("instruction: "+instruction);
                        //System.out.println("lang: "+languate_tag);
                        //System.out.println("num: "+num);
                        String[] prompt = new String[num];
                        bool[]   echo   = new bool[num];
                        for (int i = 0; i < num; i++)
                        {
                            prompt[i] = StringAux.getString(buf.getString());
                            echo[i]   = (buf.getByte() != 0);
                            //System.out.println("  "+prompt[i]+","+echo[i]);
                        }

                        String[] response = null;
                        if (num > 0 ||
                            (name.Length > 0 || instruction.Length > 0)
                            )
                        {
                            IUIKeyboardInteractive kbi = (IUIKeyboardInteractive)userinfo;
                            if (userinfo != null)
                            {
                                response = kbi.promptKeyboardInteractive(dest,
                                                                         name,
                                                                         instruction,
                                                                         prompt,
                                                                         echo);
                            }
                        }
                        // byte      SSH_MSG_USERAUTH_INFO_RESPONSE(61)
                        // int       num-responses
                        // string    response[1] (ISO-10646 UTF-8)
                        // ...
                        // string    response[num-responses] (ISO-10646 UTF-8)
                        //if(response!=null)
                        //System.out.println("response.length="+response.length);
                        //else
                        //System.out.println("response is null");
                        packet.reset();
                        buf.putByte((byte)Session.SSH_MSG_USERAUTH_INFO_RESPONSE);
                        if (num > 0 &&
                            (response == null ||                         // cancel
                             num != response.Length))
                        {
                            buf.putInt(0);
                            if (response == null)
                            {
                                cancel = true;
                            }
                        }
                        else
                        {
                            buf.putInt(num);
                            for (int i = 0; i < num; i++)
                            {
                                //System.out.println("response: |"+response[i]+"| <- replace here with **** if you need");
                                buf.putString(StringAux.getBytes(response[i]));
                            }
                        }
                        session.write(packet);
                        if (cancel)
                        {
                            break;
                        }
                        //System.out.println("continue loop");
                        goto loop;
                    }
                    //throw new JSchException("USERAUTH fail ("+buf.buffer[5]+")");
                    return(false);
                }
                if (cancel)
                {
                    throw new SshClientAuthCancelException("keyboard-interactive");
                    //break;
                }
            }
            //return false;
        }
Beispiel #19
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;
        }
Beispiel #20
0
 public void setCookie(String cookie)
 {
     ChannelX11.cookie = StringAux.getBytes(cookie);
 }
Beispiel #21
0
        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);
        }