| 
									
										
										
										
											2016-07-07 02:59:55 +08:00
										 |  |  | package sftp | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-09 03:38:35 +08:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"encoding" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/pkg/errors" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // all incoming packets
 | 
					
						
							| 
									
										
										
										
											2017-03-14 09:24:32 +08:00
										 |  |  | type requestPacket interface { | 
					
						
							| 
									
										
										
										
											2016-07-09 03:38:35 +08:00
										 |  |  | 	encoding.BinaryUnmarshaler | 
					
						
							|  |  |  | 	id() uint32 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-24 04:47:05 +08:00
										 |  |  | type requestChan chan requestPacket | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-26 02:52:07 +08:00
										 |  |  | type responsePacket interface { | 
					
						
							| 
									
										
										
										
											2016-07-19 02:50:45 +08:00
										 |  |  | 	encoding.BinaryMarshaler | 
					
						
							| 
									
										
										
										
											2017-03-15 08:43:51 +08:00
										 |  |  | 	id() uint32 | 
					
						
							| 
									
										
										
										
											2016-07-19 02:50:45 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-07-12 04:56:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-09 03:38:35 +08:00
										 |  |  | // interfaces to group types
 | 
					
						
							| 
									
										
										
										
											2016-07-07 02:59:55 +08:00
										 |  |  | type hasPath interface { | 
					
						
							| 
									
										
										
										
											2017-03-14 09:24:32 +08:00
										 |  |  | 	requestPacket | 
					
						
							| 
									
										
										
										
											2016-07-07 02:59:55 +08:00
										 |  |  | 	getPath() string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type hasHandle interface { | 
					
						
							| 
									
										
										
										
											2017-03-14 09:24:32 +08:00
										 |  |  | 	requestPacket | 
					
						
							| 
									
										
										
										
											2016-07-07 02:59:55 +08:00
										 |  |  | 	getHandle() string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 10:31:46 +08:00
										 |  |  | type isOpener interface { | 
					
						
							|  |  |  | 	hasPath | 
					
						
							|  |  |  | 	isOpener() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-14 06:54:55 +08:00
										 |  |  | type notReadOnly interface { | 
					
						
							|  |  |  | 	notReadOnly() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 02:59:55 +08:00
										 |  |  | //// define types by adding methods
 | 
					
						
							|  |  |  | // hasPath
 | 
					
						
							|  |  |  | func (p sshFxpLstatPacket) getPath() string    { return p.Path } | 
					
						
							|  |  |  | func (p sshFxpStatPacket) getPath() string     { return p.Path } | 
					
						
							|  |  |  | func (p sshFxpRmdirPacket) getPath() string    { return p.Path } | 
					
						
							|  |  |  | func (p sshFxpReadlinkPacket) getPath() string { return p.Path } | 
					
						
							|  |  |  | func (p sshFxpRealpathPacket) getPath() string { return p.Path } | 
					
						
							|  |  |  | func (p sshFxpMkdirPacket) getPath() string    { return p.Path } | 
					
						
							|  |  |  | func (p sshFxpSetstatPacket) getPath() string  { return p.Path } | 
					
						
							|  |  |  | func (p sshFxpStatvfsPacket) getPath() string  { return p.Path } | 
					
						
							|  |  |  | func (p sshFxpRemovePacket) getPath() string   { return p.Filename } | 
					
						
							|  |  |  | func (p sshFxpRenamePacket) getPath() string   { return p.Oldpath } | 
					
						
							|  |  |  | func (p sshFxpSymlinkPacket) getPath() string  { return p.Targetpath } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 10:31:46 +08:00
										 |  |  | // Openers implement hasPath and isOpener
 | 
					
						
							| 
									
										
										
										
											2016-07-12 05:54:46 +08:00
										 |  |  | func (p sshFxpOpendirPacket) getPath() string { return p.Path } | 
					
						
							| 
									
										
										
										
											2016-07-13 10:31:46 +08:00
										 |  |  | func (p sshFxpOpendirPacket) isOpener()       {} | 
					
						
							| 
									
										
										
										
											2016-07-12 05:54:46 +08:00
										 |  |  | func (p sshFxpOpenPacket) getPath() string    { return p.Path } | 
					
						
							| 
									
										
										
										
											2016-07-13 10:31:46 +08:00
										 |  |  | func (p sshFxpOpenPacket) isOpener()          {} | 
					
						
							| 
									
										
										
										
											2016-07-12 05:54:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 02:59:55 +08:00
										 |  |  | // hasHandle
 | 
					
						
							|  |  |  | func (p sshFxpFstatPacket) getHandle() string    { return p.Handle } | 
					
						
							| 
									
										
										
										
											2016-07-09 03:38:35 +08:00
										 |  |  | func (p sshFxpFsetstatPacket) getHandle() string { return p.Handle } | 
					
						
							| 
									
										
										
										
											2016-07-07 02:59:55 +08:00
										 |  |  | func (p sshFxpReadPacket) getHandle() string     { return p.Handle } | 
					
						
							|  |  |  | func (p sshFxpWritePacket) getHandle() string    { return p.Handle } | 
					
						
							| 
									
										
										
										
											2016-07-09 03:38:35 +08:00
										 |  |  | func (p sshFxpReaddirPacket) getHandle() string  { return p.Handle } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-14 06:54:55 +08:00
										 |  |  | // notReadOnly
 | 
					
						
							|  |  |  | func (p sshFxpWritePacket) notReadOnly()    {} | 
					
						
							|  |  |  | func (p sshFxpSetstatPacket) notReadOnly()  {} | 
					
						
							|  |  |  | func (p sshFxpFsetstatPacket) notReadOnly() {} | 
					
						
							|  |  |  | func (p sshFxpRemovePacket) notReadOnly()   {} | 
					
						
							|  |  |  | func (p sshFxpMkdirPacket) notReadOnly()    {} | 
					
						
							|  |  |  | func (p sshFxpRmdirPacket) notReadOnly()    {} | 
					
						
							|  |  |  | func (p sshFxpRenamePacket) notReadOnly()   {} | 
					
						
							|  |  |  | func (p sshFxpSymlinkPacket) notReadOnly()  {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-09 03:38:35 +08:00
										 |  |  | // this has a handle, but is only used for close
 | 
					
						
							|  |  |  | func (p sshFxpClosePacket) getHandle() string { return p.Handle } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-12 04:56:47 +08:00
										 |  |  | // some packets with ID are missing id()
 | 
					
						
							|  |  |  | func (p sshFxpDataPacket) id() uint32   { return p.ID } | 
					
						
							|  |  |  | func (p sshFxpStatusPacket) id() uint32 { return p.ID } | 
					
						
							|  |  |  | func (p sshFxpStatResponse) id() uint32 { return p.ID } | 
					
						
							|  |  |  | func (p sshFxpNamePacket) id() uint32   { return p.ID } | 
					
						
							| 
									
										
										
										
											2017-03-15 08:43:51 +08:00
										 |  |  | func (p sshFxpHandlePacket) id() uint32 { return p.ID } | 
					
						
							|  |  |  | func (p sshFxVersionPacket) id() uint32 { return 0 } | 
					
						
							| 
									
										
										
										
											2016-07-12 03:57:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-09 03:38:35 +08:00
										 |  |  | // take raw incoming packet data and build packet objects
 | 
					
						
							| 
									
										
										
										
											2017-03-14 09:24:32 +08:00
										 |  |  | func makePacket(p rxPacket) (requestPacket, error) { | 
					
						
							|  |  |  | 	var pkt requestPacket | 
					
						
							| 
									
										
										
										
											2016-07-09 03:38:35 +08:00
										 |  |  | 	switch p.pktType { | 
					
						
							|  |  |  | 	case ssh_FXP_INIT: | 
					
						
							|  |  |  | 		pkt = &sshFxInitPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_LSTAT: | 
					
						
							|  |  |  | 		pkt = &sshFxpLstatPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_OPEN: | 
					
						
							|  |  |  | 		pkt = &sshFxpOpenPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_CLOSE: | 
					
						
							|  |  |  | 		pkt = &sshFxpClosePacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_READ: | 
					
						
							|  |  |  | 		pkt = &sshFxpReadPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_WRITE: | 
					
						
							|  |  |  | 		pkt = &sshFxpWritePacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_FSTAT: | 
					
						
							|  |  |  | 		pkt = &sshFxpFstatPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_SETSTAT: | 
					
						
							|  |  |  | 		pkt = &sshFxpSetstatPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_FSETSTAT: | 
					
						
							|  |  |  | 		pkt = &sshFxpFsetstatPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_OPENDIR: | 
					
						
							|  |  |  | 		pkt = &sshFxpOpendirPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_READDIR: | 
					
						
							|  |  |  | 		pkt = &sshFxpReaddirPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_REMOVE: | 
					
						
							|  |  |  | 		pkt = &sshFxpRemovePacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_MKDIR: | 
					
						
							|  |  |  | 		pkt = &sshFxpMkdirPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_RMDIR: | 
					
						
							|  |  |  | 		pkt = &sshFxpRmdirPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_REALPATH: | 
					
						
							|  |  |  | 		pkt = &sshFxpRealpathPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_STAT: | 
					
						
							|  |  |  | 		pkt = &sshFxpStatPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_RENAME: | 
					
						
							|  |  |  | 		pkt = &sshFxpRenamePacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_READLINK: | 
					
						
							|  |  |  | 		pkt = &sshFxpReadlinkPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_SYMLINK: | 
					
						
							|  |  |  | 		pkt = &sshFxpSymlinkPacket{} | 
					
						
							|  |  |  | 	case ssh_FXP_EXTENDED: | 
					
						
							|  |  |  | 		pkt = &sshFxpExtendedPacket{} | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		return nil, errors.Errorf("unhandled packet type: %s", p.pktType) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err := pkt.UnmarshalBinary(p.pktBytes); err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return pkt, nil | 
					
						
							|  |  |  | } |