| 1 | |
| 2 | |
| 3 | package sftp |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | ) |
| 8 | |
| 9 | const ( |
| 10 | sshFxpInit = 1 |
| 11 | sshFxpVersion = 2 |
| 12 | sshFxpOpen = 3 |
| 13 | sshFxpClose = 4 |
| 14 | sshFxpRead = 5 |
| 15 | sshFxpWrite = 6 |
| 16 | sshFxpLstat = 7 |
| 17 | sshFxpFstat = 8 |
| 18 | sshFxpSetstat = 9 |
| 19 | sshFxpFsetstat = 10 |
| 20 | sshFxpOpendir = 11 |
| 21 | sshFxpReaddir = 12 |
| 22 | sshFxpRemove = 13 |
| 23 | sshFxpMkdir = 14 |
| 24 | sshFxpRmdir = 15 |
| 25 | sshFxpRealpath = 16 |
| 26 | sshFxpStat = 17 |
| 27 | sshFxpRename = 18 |
| 28 | sshFxpReadlink = 19 |
| 29 | sshFxpSymlink = 20 |
| 30 | sshFxpStatus = 101 |
| 31 | sshFxpHandle = 102 |
| 32 | sshFxpData = 103 |
| 33 | sshFxpName = 104 |
| 34 | sshFxpAttrs = 105 |
| 35 | sshFxpExtended = 200 |
| 36 | sshFxpExtendedReply = 201 |
| 37 | ) |
| 38 | |
| 39 | const ( |
| 40 | sshFxOk = 0 |
| 41 | sshFxEOF = 1 |
| 42 | sshFxNoSuchFile = 2 |
| 43 | sshFxPermissionDenied = 3 |
| 44 | sshFxFailure = 4 |
| 45 | sshFxBadMessage = 5 |
| 46 | sshFxNoConnection = 6 |
| 47 | sshFxConnectionLost = 7 |
| 48 | sshFxOPUnsupported = 8 |
| 49 | |
| 50 | |
| 51 | |
| 52 | sshFxInvalidHandle = 9 |
| 53 | sshFxNoSuchPath = 10 |
| 54 | sshFxFileAlreadyExists = 11 |
| 55 | sshFxWriteProtect = 12 |
| 56 | sshFxNoMedia = 13 |
| 57 | sshFxNoSpaceOnFilesystem = 14 |
| 58 | sshFxQuotaExceeded = 15 |
| 59 | sshFxUnknownPrincipal = 16 |
| 60 | sshFxLockConflict = 17 |
| 61 | sshFxDirNotEmpty = 18 |
| 62 | sshFxNotADirectory = 19 |
| 63 | sshFxInvalidFilename = 20 |
| 64 | sshFxLinkLoop = 21 |
| 65 | sshFxCannotDelete = 22 |
| 66 | sshFxInvalidParameter = 23 |
| 67 | sshFxFileIsADirectory = 24 |
| 68 | sshFxByteRangeLockConflict = 25 |
| 69 | sshFxByteRangeLockRefused = 26 |
| 70 | sshFxDeletePending = 27 |
| 71 | sshFxFileCorrupt = 28 |
| 72 | sshFxOwnerInvalid = 29 |
| 73 | sshFxGroupInvalid = 30 |
| 74 | sshFxNoMatchingByteRangeLock = 31 |
| 75 | ) |
| 76 | |
| 77 | const ( |
| 78 | sshFxfRead = 0x00000001 |
| 79 | sshFxfWrite = 0x00000002 |
| 80 | sshFxfAppend = 0x00000004 |
| 81 | sshFxfCreat = 0x00000008 |
| 82 | sshFxfTrunc = 0x00000010 |
| 83 | sshFxfExcl = 0x00000020 |
| 84 | ) |
| 85 | |
| 86 | var ( |
| 87 | |
| 88 | supportedSFTPExtensions = []sshExtensionPair{ |
| 89 | {"[email protected]", "1"}, |
| 90 | {"[email protected]", "1"}, |
| 91 | {"[email protected]", "2"}, |
| 92 | {"[email protected]", "1"}, |
| 93 | } |
| 94 | sftpExtensions = supportedSFTPExtensions |
| 95 | ) |
| 96 | |
| 97 | type fxp uint8 |
| 98 | |
| 99 | func (f fxp) String() string { |
| 100 | switch f { |
| 101 | case sshFxpInit: |
| 102 | return "SSH_FXP_INIT" |
| 103 | case sshFxpVersion: |
| 104 | return "SSH_FXP_VERSION" |
| 105 | case sshFxpOpen: |
| 106 | return "SSH_FXP_OPEN" |
| 107 | case sshFxpClose: |
| 108 | return "SSH_FXP_CLOSE" |
| 109 | case sshFxpRead: |
| 110 | return "SSH_FXP_READ" |
| 111 | case sshFxpWrite: |
| 112 | return "SSH_FXP_WRITE" |
| 113 | case sshFxpLstat: |
| 114 | return "SSH_FXP_LSTAT" |
| 115 | case sshFxpFstat: |
| 116 | return "SSH_FXP_FSTAT" |
| 117 | case sshFxpSetstat: |
| 118 | return "SSH_FXP_SETSTAT" |
| 119 | case sshFxpFsetstat: |
| 120 | return "SSH_FXP_FSETSTAT" |
| 121 | case sshFxpOpendir: |
| 122 | return "SSH_FXP_OPENDIR" |
| 123 | case sshFxpReaddir: |
| 124 | return "SSH_FXP_READDIR" |
| 125 | case sshFxpRemove: |
| 126 | return "SSH_FXP_REMOVE" |
| 127 | case sshFxpMkdir: |
| 128 | return "SSH_FXP_MKDIR" |
| 129 | case sshFxpRmdir: |
| 130 | return "SSH_FXP_RMDIR" |
| 131 | case sshFxpRealpath: |
| 132 | return "SSH_FXP_REALPATH" |
| 133 | case sshFxpStat: |
| 134 | return "SSH_FXP_STAT" |
| 135 | case sshFxpRename: |
| 136 | return "SSH_FXP_RENAME" |
| 137 | case sshFxpReadlink: |
| 138 | return "SSH_FXP_READLINK" |
| 139 | case sshFxpSymlink: |
| 140 | return "SSH_FXP_SYMLINK" |
| 141 | case sshFxpStatus: |
| 142 | return "SSH_FXP_STATUS" |
| 143 | case sshFxpHandle: |
| 144 | return "SSH_FXP_HANDLE" |
| 145 | case sshFxpData: |
| 146 | return "SSH_FXP_DATA" |
| 147 | case sshFxpName: |
| 148 | return "SSH_FXP_NAME" |
| 149 | case sshFxpAttrs: |
| 150 | return "SSH_FXP_ATTRS" |
| 151 | case sshFxpExtended: |
| 152 | return "SSH_FXP_EXTENDED" |
| 153 | case sshFxpExtendedReply: |
| 154 | return "SSH_FXP_EXTENDED_REPLY" |
| 155 | default: |
| 156 | return "unknown" |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | type fx uint8 |
| 161 | |
| 162 | func (f fx) String() string { |
| 163 | switch f { |
| 164 | case sshFxOk: |
| 165 | return "SSH_FX_OK" |
| 166 | case sshFxEOF: |
| 167 | return "SSH_FX_EOF" |
| 168 | case sshFxNoSuchFile: |
| 169 | return "SSH_FX_NO_SUCH_FILE" |
| 170 | case sshFxPermissionDenied: |
| 171 | return "SSH_FX_PERMISSION_DENIED" |
| 172 | case sshFxFailure: |
| 173 | return "SSH_FX_FAILURE" |
| 174 | case sshFxBadMessage: |
| 175 | return "SSH_FX_BAD_MESSAGE" |
| 176 | case sshFxNoConnection: |
| 177 | return "SSH_FX_NO_CONNECTION" |
| 178 | case sshFxConnectionLost: |
| 179 | return "SSH_FX_CONNECTION_LOST" |
| 180 | case sshFxOPUnsupported: |
| 181 | return "SSH_FX_OP_UNSUPPORTED" |
| 182 | default: |
| 183 | return "unknown" |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | type unexpectedPacketErr struct { |
| 188 | want, got fxp |
| 189 | } |
| 190 | |
| 191 | func (u *unexpectedPacketErr) Error() string { |
| 192 | return fmt.Sprintf("sftp: unexpected packet: want %v, got %v", u.want, u.got) |
| 193 | } |
| 194 | |
| 195 | func unimplementedPacketErr(u fxp) error { |
| 196 | return fmt.Errorf("sftp: unimplemented packet type: got %v", u) |
| 197 | } |
| 198 | |
| 199 | type unexpectedIDErr struct{ want, got uint32 } |
| 200 | |
| 201 | func (u *unexpectedIDErr) Error() string { |
| 202 | return fmt.Sprintf("sftp: unexpected id: want %d, got %d", u.want, u.got) |
| 203 | } |
| 204 | |
| 205 | func unimplementedSeekWhence(whence int) error { |
| 206 | return fmt.Errorf("sftp: unimplemented seek whence %d", whence) |
| 207 | } |
| 208 | |
| 209 | func unexpectedCount(want, got uint32) error { |
| 210 | return fmt.Errorf("sftp: unexpected count: want %d, got %d", want, got) |
| 211 | } |
| 212 | |
| 213 | type unexpectedVersionErr struct{ want, got uint32 } |
| 214 | |
| 215 | func (u *unexpectedVersionErr) Error() string { |
| 216 | return fmt.Sprintf("sftp: unexpected server version: want %v, got %v", u.want, u.got) |
| 217 | } |
| 218 | |
| 219 | |
| 220 | |
| 221 | type StatusError struct { |
| 222 | Code uint32 |
| 223 | msg, lang string |
| 224 | } |
| 225 | |
| 226 | func (s *StatusError) Error() string { |
| 227 | return fmt.Sprintf("sftp: %q (%v)", s.msg, fx(s.Code)) |
| 228 | } |
| 229 | |
| 230 | |
| 231 | func (s *StatusError) FxCode() fxerr { |
| 232 | return fxerr(s.Code) |
| 233 | } |
| 234 | |
| 235 | func getSupportedExtensionByName(extensionName string) (sshExtensionPair, error) { |
| 236 | for _, supportedExtension := range supportedSFTPExtensions { |
| 237 | if supportedExtension.Name == extensionName { |
| 238 | return supportedExtension, nil |
| 239 | } |
| 240 | } |
| 241 | return sshExtensionPair{}, fmt.Errorf("unsupported extension: %s", extensionName) |
| 242 | } |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | func SetSFTPExtensions(extensions ...string) error { |
| 249 | tempExtensions := []sshExtensionPair{} |
| 250 | for _, extension := range extensions { |
| 251 | sftpExtension, err := getSupportedExtensionByName(extension) |
| 252 | if err != nil { |
| 253 | return err |
| 254 | } |
| 255 | tempExtensions = append(tempExtensions, sftpExtension) |
| 256 | } |
| 257 | sftpExtensions = tempExtensions |
| 258 | return nil |
| 259 | } |