public void request(Session session, Channel 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(Util.getBytes("subsystem")); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putString(Util.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 JSchException("failed to send sftp request"); } } }
public void request(Session session, Channel 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( Util.getBytes("signal")); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putString(Util.getBytes(signal)); session.write(packet); }
public void request(Session session, Channel 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(Util.getBytes("x11-req")); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putByte((byte)0); buf.putString(Util.getBytes("MIT-MAGIC-COOKIE-1")); buf.putString(ChannelX11.getFakedCookie(session)); buf.putInt(0); session.write(packet); }
public void request(Session session, Channel 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(Util.getBytes("shell")); buf.putByte((byte)(waitForReply() ? 1 : 0)); session.write(packet); }
public void request(Session session, Channel 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(Util.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); }
public override void connect() { try { if(!session.isConnected()) { throw new JSchException("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(Util.getBytes("direct-tcpip")); buf.putInt(id); buf.putInt(lwsize); buf.putInt(lmpsize); buf.putString(Util.getBytes(host)); buf.putInt(port); buf.putString(Util.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); Thread.Sleep(50); retry--; } } catch { } if(!session.isConnected()) { throw new JSchException("session is down"); } if(retry==0 || this._eof_remote) { throw new JSchException("channel is not opened."); } /* if(this.eof_remote){ // failed to open disconnect(); return; } */ connected=true; thread=new Thread(this); thread.start(); } catch(Exception e) { io.close(); io=null; Channel.del(this); if (e is JSchException) { throw (JSchException) e; } } }
public virtual void connect() { if(!session.isConnected()) { throw new JSchException("session is down"); } try { Buffer buf=new Buffer(100); 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(this.type); buf.putInt(this.id); buf.putInt(this.lwsize); buf.putInt(this.lmpsize); session.write(packet); int retry=1000; while(this.getRecipient()==-1 && session.isConnected() && retry>0) { try{Thread.sleep(50);} catch(Exception ee){} retry--; } if(!session.isConnected()) { throw new JSchException("session is down"); } if(retry==0) { throw new JSchException("channel is not opened."); } connected=true; start(); } catch(Exception e) { connected=false; if(e is JSchException) throw (JSchException)e; } }
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(Util.getBytes(extended[i*2])); buf.putString(Util.getBytes(extended[i*2+1])); } } } }
public override byte[] getPublicKeyBlob() { byte[] foo=base.getPublicKeyBlob(); if(foo!=null) return foo; if(pub_array==null) return null; Buffer buf=new Buffer(sshrsa.Length+4+ pub_array.Length+4+ n_array.Length+4); buf.putString(sshrsa); buf.putString(pub_array); buf.putString(n_array); return buf.buffer; }
byte[] getSignature_rsa(Session session, byte[] data) { try { Type t=Type.GetType(jsch.getConfig("signature.rsa")); SignatureRSA rsa=(SignatureRSA)Activator.CreateInstance(t); rsa.init(); rsa.setPrvKey(e_array, n_array, d_array, p_array, q_array, dmp1_array, dmq1_array, iqmp_array); /* byte[] goo=new byte[4]; goo[0]=(byte)(session.getSessionId().Length>>>24); goo[1]=(byte)(session.getSessionId().Length>>>16); goo[2]=(byte)(session.getSessionId().Length>>>8); goo[3]=(byte)(session.getSessionId().Length); rsa.update(goo); rsa.update(session.getSessionId()); */ rsa.update(data); byte[] sig = rsa.sign(); Buffer buf=new Buffer("ssh-rsa".Length+4+ sig.Length+4); buf.putString( System.Text.Encoding.Default.GetBytes( "ssh-rsa" )); buf.putString(sig); return buf.buffer; } catch(Exception e) { Console.WriteLine(e); } return null; }
byte[] getSignature_dss(Session session, byte[] data) { /* byte[] foo; int i; System.out.print("P "); foo=P_array; for(i=0; i<foo.Length; i++){ System.out.print(Integer.toHexString(foo[i]&0xff)+":"); } System.out.println(""); System.out.print("Q "); foo=Q_array; for(i=0; i<foo.Length; i++){ System.out.print(Integer.toHexString(foo[i]&0xff)+":"); } System.out.println(""); System.out.print("G "); foo=G_array; for(i=0; i<foo.Length; i++){ System.out.print(Integer.toHexString(foo[i]&0xff)+":"); } System.out.println(""); */ try { Type t=Type.GetType(jsch.getConfig("signature.dss")); SignatureDSA dsa=(SignatureDSA)(Activator.CreateInstance(t)); dsa.init(); dsa.setPrvKey(prv_array, P_array, Q_array, G_array); /* byte[] goo=new byte[4]; goo[0]=(byte)(session.getSessionId().Length>>>24); goo[1]=(byte)(session.getSessionId().Length>>>16); goo[2]=(byte)(session.getSessionId().Length>>>8); goo[3]=(byte)(session.getSessionId().Length); dsa.update(goo); dsa.update(session.getSessionId()); */ dsa.update(data); byte[] sig = dsa.sign(); Buffer buf=new Buffer("ssh-dss".Length+4+ sig.Length+4); buf.putString( System.Text.Encoding.Default.GetBytes( "ssh-dss" ) ); buf.putString(sig); return buf.buffer; } catch(Exception e) { Console.WriteLine("e "+e); } return null; }
byte[] getPublicKeyBlob_rsa() { if(e_array==null) return null; Buffer buf=new Buffer("ssh-rsa".Length+4+ e_array.Length+4+ n_array.Length+4); buf.putString( System.Text.Encoding.Default.GetBytes( "ssh-rsa" ) ); buf.putString(e_array); buf.putString(n_array); return buf.buffer; }
byte[] getPublicKeyBlob_dss() { if(P_array==null) return null; Buffer buf=new Buffer("ssh-dss".Length+4+ P_array.Length+4+ Q_array.Length+4+ G_array.Length+4+ pub_array.Length+4); buf.putString(System.Text.Encoding.Default.GetBytes("ssh-dss")); buf.putString(P_array); buf.putString(Q_array); buf.putString(G_array); buf.putString(pub_array); return buf.buffer; }
private void setPortForwarding(int rport) { lock(grr) { Buffer buf=new Buffer(100); // ?? Packet packet=new Packet(buf); try { // byte SSH_MSG_GLOBAL_REQUEST 80 // String "tcpip-forward" // bool want_reply // String address_to_bind // uint32 port number to bind packet.reset(); buf.putByte((byte) SSH_MSG_GLOBAL_REQUEST); buf.putString( new String( "tcpip-forward" ).getBytes()); // buf.putByte((byte)0); buf.putByte((byte)1); buf.putString(new String("0.0.0.0").getBytes()); buf.putInt(rport); write(packet); } catch(Exception e) { throw new JSchException(e.ToString()); } grr.setThread(Thread.currentThread()); try{ Thread.Sleep(10000);} catch(Exception e) { } int reply=grr.getReply(); grr.setThread(null); if(reply==0) { throw new JSchException("remote port forwarding failed for listen port "+rport); } } }
public void connect(int connectTimeout) { if(_isConnected) { throw new JSchException("session is already connected"); } io=new IO(); if(random==null) { try { Class c=Class.forName(getConfig("random")); random=(Random)(c.newInstance()); } catch(Exception e) { System.Console.Error.WriteLine("connect: random "+e); } } Packet.setRandom(random); try { int i, j; int pad=0; if(proxy==null) { proxy=jsch.getProxy(host); if(proxy!=null) { lock(proxy) { proxy.close(); } } } if(proxy==null) { Stream In; Stream Out; if(socket_factory==null) { socket=Util.createSocket(host, port, connectTimeout); In=socket.getInputStream(); Out=socket.getOutputStream(); } else { socket=socket_factory.createSocket(host, port); In=socket_factory.getInputStream(socket); Out=socket_factory.getOutputStream(socket); } //if(timeout>0){ socket.setSoTimeout(timeout); } socket.setTcpNoDelay(true); io.setInputStream(In); io.setOutputStream(Out); } else { lock(proxy) { proxy.connect(socket_factory, host, port, connectTimeout); io.setInputStream(proxy.getInputStream()); io.setOutputStream(proxy.getOutputStream()); socket=proxy.getSocket(); } } if(connectTimeout>0 && socket!=null) { socket.setSoTimeout(connectTimeout); } _isConnected=true; while(true) { i=0; j=0; while(i<buf.buffer.Length) { j=io.getByte(); if(j<0)break; buf.buffer[i]=(byte)j; i++; if(j==10)break; } if(j<0) { throw new JSchException("connection is closed by foreign host"); } if(buf.buffer[i-1]==10) { // 0x0a i--; if(buf.buffer[i-1]==13) { // 0x0d i--; } } if(i>4 && (i!=buf.buffer.Length) && (buf.buffer[0]!='S'||buf.buffer[1]!='S'|| buf.buffer[2]!='H'||buf.buffer[3]!='-')) { //System.err.println(new String(buf.buffer, 0, i); continue; } if(i==buf.buffer.Length || i<7 || // SSH-1.99 or SSH-2.0 (buf.buffer[4]=='1' && buf.buffer[6]!='9') // SSH-1.5 ) { throw new JSchException("invalid server's version String"); } break; } V_S=new byte[i]; SharpSSH.SharpSsh.java.System.arraycopy(buf.buffer, 0, V_S, 0, i); //System.Console.WriteLine("V_S: ("+i+") ["+new String(V_S)+"]"); //io.put(V_C, 0, V_C.Length); io.put("\n".getBytes(), 0, 1); { // Some Cisco devices will miss to read '\n' if it is sent separately. byte[] foo=new byte[V_C.Length+1]; SharpSSH.SharpSsh.java.System.arraycopy(V_C, 0, foo, 0, V_C.Length); foo[foo.Length-1]=(byte)'\n'; io.put(foo, 0, foo.Length); } buf=read(buf); //System.Console.WriteLine("read: 20 ? "+buf.buffer[5]); if(buf.buffer[5]!=SSH_MSG_KEXINIT) { throw new JSchException("invalid protocol: "+buf.buffer[5]); } KeyExchange kex=receive_kexinit(buf); while(true) { buf=read(buf); if(kex.getState()==buf.buffer[5]) { bool result=kex.next(buf); if(!result) { //System.Console.WriteLine("verify: "+result); in_kex=false; throw new JSchException("verify: "+result); } } else { in_kex=false; throw new JSchException("invalid protocol(kex): "+buf.buffer[5]); } if(kex.getState()==KeyExchange.STATE_END) { break; } } try{ checkHost(host, kex); } catch(JSchException ee) { in_kex=false; throw ee; } send_newkeys(); // receive SSH_MSG_NEWKEYS(21) buf=read(buf); //System.Console.WriteLine("read: 21 ? "+buf.buffer[5]); if(buf.buffer[5]==SSH_MSG_NEWKEYS) { receive_newkeys(buf, kex); } else { in_kex=false; throw new JSchException("invalid protocol(newkyes): "+buf.buffer[5]); } bool auth=false; bool auth_cancel=false; UserAuthNone usn=new UserAuthNone(userinfo); auth=usn.start(this); String methods=null; if(!auth) { methods=usn.getMethods(); if(methods!=null) { methods=methods.toLowerCase(); } else { // methods: publickey,password,keyboard-interactive methods="publickey,password,keyboard-interactive"; } } loop: while(true) { //System.Console.WriteLine("methods: "+methods); while(!auth && methods!=null && methods.Length()>0) { //System.Console.WriteLine(" methods: "+methods); UserAuth us=null; if(methods.startsWith("publickey")) { //System.Console.WriteLine(" jsch.identities.size()="+jsch.identities.size()); lock(jsch.identities) { if(jsch.identities.size()>0) { us=new UserAuthPublicKey(userinfo); } } } else if(methods.startsWith("keyboard-interactive")) { if(userinfo is UIKeyboardInteractive) { us=new UserAuthKeyboardInteractive(userinfo); } } else if(methods.startsWith("password")) { us=new UserAuthPassword(userinfo); } if(us!=null) { try { auth=us.start(this); auth_cancel=false; } catch(JSchAuthCancelException ee) { //System.Console.WriteLine(ee); auth_cancel=true; } catch(JSchPartialAuthException ee) { methods=ee.getMethods(); //System.Console.WriteLine("PartialAuth: "+methods); auth_cancel=false; continue;//loop; } catch(RuntimeException ee) { throw ee; } catch(Exception ee) { System.Console.WriteLine("ee: "+ee); // SSH_MSG_DISCONNECT: 2 Too many authentication failures } } if(!auth) { int comma=methods.indexOf(","); if(comma==-1) break; methods=methods.subString(comma+1); } } break; } if(connectTimeout>0 || timeout>0) { socket.setSoTimeout(timeout); } if(auth) { isAuthed=true; connectThread=new Thread(this); connectThread.setName("Connect thread "+host+" session"); connectThread.start(); return; } if(auth_cancel) throw new JSchException("Auth cancel"); throw new JSchException("Auth fail"); } catch(Exception e) { in_kex=false; if(_isConnected) { try { packet.reset(); buf.putByte((byte)SSH_MSG_DISCONNECT); buf.putInt(3); buf.putString(new String(e.ToString()).getBytes()); buf.putString(new String("en").getBytes()); write(packet); disconnect(); } catch(Exception ee) { } } _isConnected=false; //e.printStackTrace(); if(e is RuntimeException) throw (RuntimeException)e; if(e is JSchException) throw (JSchException)e; throw new JSchException("Session.connect: "+e); } }
public void sendKeepAliveMsg() { Buffer buf=new Buffer(); Packet packet=new Packet(buf); packet.reset(); buf.putByte((byte)SSH_MSG_GLOBAL_REQUEST); buf.putString(keepalivemsg); buf.putByte((byte)1); write(packet); }