# ne7ssh_sftp.h

    00001 /***************************************************************************
    00002  *   Copyright (C) 2005-2007 by NetSieben Technologies INC                 *
    00003  *   Author: Andrew Useckas                                                *
    00004  *   Email: andrew@netsieben.com                                           *
    00005  *                                                                         *
    00006  *   Windows Port and bugfixes: Keef Aragon <keef@netsieben.com>           *
    00007  *                                                                         *
    00008  *   This program may be distributed under the terms of the Q Public       *
    00009  *   License as defined by Trolltech AS of Norway and appearing in the     *
    00010  *   file LICENSE.QPL included in the packaging of this file.              *
    00011  *                                                                         *
    00012  *   This program is distributed in the hope that it will be useful,       *
    00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
    00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *
    00015  ***************************************************************************/
    00016
    00017
    00018 #ifndef NE7SSHSFTP_H
    00019 #define NE7SSHSFTP_H
    00020
    00021 #include "ne7ssh_channel.h"
    00022 #include "ne7ssh.h"
    00023
    00024 #ifdef WIN32
    00025 // 777 = 0x1ff
    00026 #ifndef __MINGW32__
    00027 #define S_IRUSR 0x100
    00028 #endif
    00029 #define S_IRGRP 0x020
    00030 #define S_IROTH 0x004
    00031 #ifndef __MINGW32__
    00032 #define S_IWUSR 0x080
    00033 #endif
    00034 #define S_IWGRP 0x010
    00035 #define S_IWOTH 0x002
    00036 #ifndef __MINGW32__
    00037 #define S_IXUSR 0x040
    00038 #endif
    00039 #define S_IXGRP 0x008
    00040 #define S_IXOTH 0x001
    00041 #define S_ISUID 0x800
    00042 #define S_ISGID 0x400
    00043 #define S_ISVTX 0x200
    00044 #ifndef __MINGW32__
    00045 #define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
    00046 #endif
    00047 #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
    00048 #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
    00049 #endif
    00050
    00051 #define SSH2_FXP_INIT                1
    00052 #define SSH2_FXP_VERSION             2
    00053 #define SSH2_FXP_OPEN                3
    00054 #define SSH2_FXP_CLOSE               4
    00055 #define SSH2_FXP_READ                5
    00056 #define SSH2_FXP_WRITE               6
    00057 #define SSH2_FXP_LSTAT               7
    00058 #define SSH2_FXP_FSTAT               8
    00059 #define SSH2_FXP_SETSTAT             9
    00060 #define SSH2_FXP_FSETSTAT           10
    00061 #define SSH2_FXP_OPENDIR            11
    00062 #define SSH2_FXP_READDIR            12
    00063 #define SSH2_FXP_REMOVE             13
    00064 #define SSH2_FXP_MKDIR              14
    00065 #define SSH2_FXP_RMDIR              15
    00066 #define SSH2_FXP_REALPATH           16
    00067 #define SSH2_FXP_STAT               17
    00068 #define SSH2_FXP_RENAME             18
    00069 #define SSH2_FXP_READLINK           19
    00070 #define SSH2_FXP_LINK               21
    00071 #define SSH2_FXP_BLOCK              22
    00072 #define SSH2_FXP_UNBLOCK            23
    00073
    00074 #define SSH2_FXP_STATUS            101
    00075 #define SSH2_FXP_HANDLE            102
    00076 #define SSH2_FXP_DATA              103
    00077 #define SSH2_FXP_NAME              104
    00078 #define SSH2_FXP_ATTRS             105
    00079
    00080 #define SSH2_FXP_EXTENDED          200
    00081 #define SSH2_FXP_EXTENDED_REPLY    201
    00082
    00083 #define SFTP_VERSION 3
    00084 #define SFTP_MAX_SEQUENCE 4294967295U
    00085 #define SFTP_MAX_PACKET_SIZE (32 * 1024)
    00086 #define SFTP_MAX_MSG_SIZE (256 * 1024)
    00087
    00088 #define SSH2_FXF_READ           0x00000001
    00089 #define SSH2_FXF_WRITE          0x00000002
    00090 #define SSH2_FXF_APPEND         0x00000004
    00091 #define SSH2_FXF_CREAT          0x00000008
    00092 #define SSH2_FXF_TRUNC          0x00000010
    00093 #define SSH2_FXF_EXCL           0x00000020
    00094
    00095 #define SSH2_FILEXFER_ATTR_SIZE         0x00000001
    00096 #define SSH2_FILEXFER_ATTR_UIDGID       0x00000002
    00097 #define SSH2_FILEXFER_ATTR_ACMODTIME    0x00000008
    00098 #define SSH2_FILEXFER_ATTR_PERMISSIONS  0x00000004
    00099
    00100 class ne7ssh_session;
    00101 class ne7ssh_transport;
    00102
    00106 class Ne7sshSftp : public ne7ssh_channel
    00107 {
    00108   private:
    00109     ne7ssh_session* session;
    00110     uint32 timeout;
    00111     uint32 seq;
    00112     uint8 sftpCmd;
    00113     ne7ssh_string commBuffer;
    00114     Botan::SecureVector<Botan::byte> fileBuffer;
    00115     enum writeMode { READ, OVERWRITE, APPEND };
    00116     uint8 lastError;
    00117     char* currentPath;
    00118
    00122     typedef struct
    00123     {
    00124       uint32 flags;
    00125       uint64 size;
    00126       uint32 owner;
    00127       uint32 group;
    00128       uint32 permissions;
    00129       uint32 atime;
    00130       uint32 mtime;
    00131     } sftpFileAttrs;
    00132
    00133     sftpFileAttrs attrs;
    00134
    00138     typedef struct
    00139     {
    00140       uint32 fileID;
    00141       uint16 handleLen;
    00142       char handle[256];
    00143     } sftpFile;
    00144     sftpFile **sftpFiles;
    00145     uint16 sftpFilesCount;
    00146
    00152     bool handleData (Botan::SecureVector<Botan::byte>& packet);
    00153
    00159     bool handleVersion (Botan::SecureVector<Botan::byte>& packet);
    00160
    00166     bool handleStatus (Botan::SecureVector<Botan::byte>& packet);
    00167
    00173     bool addOpenHandle (Botan::SecureVector<Botan::byte>& packet);
    00174
    00180     bool handleSftpData (Botan::SecureVector<Botan::byte>& packet);
    00181
    00187     bool handleNames (Botan::SecureVector<Botan::byte>& packet);
    00188
    00194     sftpFile* getFileHandle (uint32 fileID);
    00195
    00202     bool receiveUntil (short _cmd, uint32 timeSec = 0);
    00203
    00210     bool receiveWhile (short _cmd, uint32 timeSec = 0);
    00211
    00217     bool processAttrs (Botan::SecureVector<Botan::byte>& packet);
    00218
    00225     bool getFileStats (const char* remoteFile, bool followSymLinks = true);
    00226
    00234     bool getFileAttrs (sftpFileAttrs& attributes, Botan::SecureVector<Botan::byte>& remoteFile,  bool followSymLinks = true);
    00235
    00241     bool getFStat (uint32 fileID);
    00242
    00248     uint64 getFileSize (uint32 fileID);
    00249
    00254     bool receiveWindowAdjust ();
    00255
    00261     ne7ssh_string getFullPath (const char* filename);
    00262
    00269     bool isType (const char* remoteFile, uint32 type);
    00270
    00271   public:
    00277     Ne7sshSftp(ne7ssh_session* _session, ne7ssh_channel* _channel);
    00278
    00282     ~Ne7sshSftp();
    00283
    00288     bool init();
    00289
    00294     void setTimeout (uint32 _timeout) { timeout = _timeout; }
    00295
    00302     uint32 openFile (const char* filename, uint8 shortMode);
    00303
    00309     uint32 openDir (const char* dirname);
    00310
    00317     bool readFile (uint32 fileID, uint64 offset = 0);
    00318
    00327     bool writeFile (uint32 fileID, const uint8* data, uint32 len, uint64 offset = 0);
    00328
    00334     bool closeFile (uint32 fileID);
    00335
    00343     bool getFileAttrs (Ne7SftpSubsystem::fileAttrs& attributes, const char* remoteFile,  bool followSymLinks = true);
    00344
    00350     bool isFile (const char* remoteFile);
    00351
    00357     bool isDir (const char* remoteFile);
    00358
    00359
    00366     bool get (const char* remoteFile, FILE* localFile);
    00367
    00374     bool put (FILE* localFile, const char* remoteFile);
    00375
    00381     bool rm (const char* remoteFile);
    00382
    00389     bool mv (const char* oldFile, const char* newFile);
    00390
    00396     bool mkdir (const char* remoteDir);
    00397
    00403     bool rmdir (const char* remoteDir);
    00404
    00411     const char* ls (const char* remoteDir, bool longNames=false);
    00412
    00418     bool cd (const char* remoteDir);
    00419
    00426     bool chmod (const char* remoteFile, const char* mode);
    00427
    00435     bool chown (const char* remoteFile, uint32 uid, uint32 gid = 0);
    00436 };
    00437
    00438 #endif