# ne7ssh.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 #ifndef NE7SSH_H
    00018 #define NE7SSH_H
    00019
    00020 #include <botan/build.h>
    00021
    00022 //#include <botan/zlib.h>
    00023 //#include "error.h"
    00024
    00025 #if BOTAN_VERSION_MAJOR > 1
    00026 #   error Unsupported Botan Version
    00027 #endif
    00028
    00029 #define BOTAN_PRE_15 (BOTAN_VERSION_MINOR < 5)
    00030 #define BOTAN_PRE_18 (BOTAN_VERSION_MINOR < 8)
    00031
    00032 #if !BOTAN_PRE_18 && !BOTAN_PRE_15
    00033 # include <botan/auto_rng.h>
    00034 #endif
    00035
    00036 #include <stdlib.h>
    00037 #include <string>
    00038 #include <fcntl.h>
    00039 #if !defined(WIN32) && !defined(__MINGW32__)
    00040 #   include <pthread.h>
    00041 #   include <sys/select.h>
    00042 #   include <unistd.h>
    00043 typedef pthread_t ne7ssh_thread_t;
    00044 #else
    00045 #include <windows.h>
    00046 typedef HANDLE ne7ssh_thread_t;
    00047 #endif
    00048
    00049 #include "ne7ssh_types.h"
    00050 #include "ne7ssh_error.h"
    00051 #include "ne7ssh_mutex.h"
    00052
    00053 #define SSH2_MSG_DISCONNECT 1
    00054 #define SSH2_MSG_IGNORE 2
    00055
    00056 #define SSH2_MSG_KEXINIT  20
    00057 #define SSH2_MSG_NEWKEYS  21
    00058
    00059 #define SSH2_MSG_KEXDH_INIT 30
    00060 #define SSH2_MSG_KEXDH_REPLY  31
    00061
    00062 #define SSH2_MSG_SERVICE_REQUEST 5
    00063 #define SSH2_MSG_SERVICE_ACCEPT 6
    00064
    00065 #define SSH2_MSG_USERAUTH_REQUEST 50
    00066 #define SSH2_MSG_USERAUTH_FAILURE 51
    00067 #define SSH2_MSG_USERAUTH_SUCCESS 52
    00068 #define SSH2_MSG_USERAUTH_BANNER 53
    00069 #define SSH2_MSG_USERAUTH_PK_OK 60
    00070
    00071 #define SSH2_MSG_CHANNEL_OPEN                           90
    00072 #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION              91
    00073 #define SSH2_MSG_CHANNEL_OPEN_FAILURE                   92
    00074 #define SSH2_MSG_CHANNEL_WINDOW_ADJUST                  93
    00075 #define SSH2_MSG_CHANNEL_DATA                           94
    00076 #define SSH2_MSG_CHANNEL_EXTENDED_DATA                  95
    00077 #define SSH2_MSG_CHANNEL_EOF                            96
    00078 #define SSH2_MSG_CHANNEL_CLOSE                          97
    00079 #define SSH2_MSG_CHANNEL_REQUEST                        98
    00080 #define SSH2_MSG_CHANNEL_SUCCESS                        99
    00081 #define SSH2_MSG_CHANNEL_FAILURE                        100
    00082
    00083 class ne7ssh_connection;
    00084
    00086 typedef struct {
    00088   ne7ssh_connection **conns;
    00090   uint32 count;
    00091 } connStruct;
    00092
    00094 namespace Botan
    00095 {
    00096     class LibraryInitializer;
    00097 }
    00098
    00099 class Ne7SftpSubsystem;
    00100
    00104 class SSH_EXPORT ne7ssh
    00105 {
    00106   private:
    00107
    00108     static Ne7ssh_Mutex mut;
    00109     Botan::LibraryInitializer *init;
    00110     ne7ssh_connection **connections;
    00111     uint32 conCount;
    00112     static bool running;
    00113     static bool selectActive;
    00114     connStruct allConns;
    00115
    00116
    00122     static void *selectThread (void*);
    00123
    00128     uint32 getChannelNo ();
    00129     ne7ssh_thread_t select_thread;
    00130     bool connected;
    00131
    00136     static bool lock ();
    00137
    00142     static bool unlock ();
    00143     static Ne7sshError* errs;
    00144
    00145   public:
    00146 #if !BOTAN_PRE_18 && !BOTAN_PRE_15
    00147     static Botan::AutoSeeded_RNG *rng;
    00148 #endif
    00149     static const char* SSH_VERSION;
    00150     static const char* KEX_ALGORITHMS;
    00151     static const char* HOSTKEY_ALGORITHMS;
    00152     static const char* MAC_ALGORITHMS;
    00153     static const char* CIPHER_ALGORITHMS;
    00154     static const char* COMPRESSION_ALGORITHMS;
    00155     static char* PREFERED_CIPHER;
    00156     static char* PREFERED_MAC;
    00157
    00161     ne7ssh();
    00165     ~ne7ssh();
    00166
    00177     int connectWithPassword (const char* host, const int port, const char* username, const char* password, bool shell = true, const int timeout = 0);
    00178
    00191     int connectWithKey (const char* host, const int port, const char* username, const char* privKeyFileName, bool shell = true, const int timeout = 0);
    00192
    00198 //    ne7ssh_connection** getConnections () { return connections; }
    00199
    00200     connStruct* getConnetions () { return &allConns; }
    00201
    00207 //    uint32 getConCount () { return conCount; }
    00208
    00215     bool send (const char* data, int channel);
    00216
    00224     bool sendCmd (const char* cmd, int channel, int timeout);
    00225
    00231     bool close (int channel);
    00232
    00238     void setCount (uint32 count) { conCount = count; }
    00239
    00245     const char* read (int channel, bool do_lock=true);
    00246
    00252     void* readBinary (int channel);
    00253
    00259     int getReceivedSize (int channel, bool do_lock=true);
    00260
    00268     bool waitFor (int channel, const char* str, uint32 timeout=0);
    00269
    00276     void setOptions (const char* prefCipher, const char* prefHmac);
    00277
    00278
    00288     bool generateKeyPair (const char* type, const char* fqdn, const char* privKeyFileName, const char* pubKeyFileName, uint16 keySize = 0);
    00289
    00296     bool initSftp (Ne7SftpSubsystem& _sftp, int channel);
    00297
    00302     static Ne7sshError* errors();
    00303
    00304     static bool isSelectActive() { return selectActive; }
    00305     static void selectDead() { selectActive = false; }
    00306 };
    00307
    00308 class Ne7sshSftp;
    00309
    00313 class SSH_EXPORT Ne7SftpSubsystem
    00314 {
    00315   private:
    00316     bool inited;
    00317     Ne7sshSftp* sftp;
    00318
    00323 bool errorNotInited ();
    00324
    00325   public:
    00327     typedef struct
    00328     {
    00329       uint64_t  size;
    00330       uint32_t  owner;
    00331       uint32_t  group;
    00332       uint32_t  permissions;
    00333       uint32_t  atime;
    00334       uint32_t  mtime;
    00335     } fileAttrs;
    00336
    00338     enum writeMode { READ, OVERWRITE, APPEND };
    00339
    00343     Ne7SftpSubsystem ();
    00344
    00349     Ne7SftpSubsystem (class Ne7sshSftp* _sftp);
    00350
    00354     ~Ne7SftpSubsystem();
    00355
    00361     bool setTimeout (uint32 _timeout);
    00362
    00369     uint32 openFile (const char* filename, uint8 mode);
    00370
    00376     uint32 openDir (const char* dirname);
    00377
    00384     bool readFile (uint32 fileID, uint64 offset = 0);
    00385
    00394     bool writeFile (uint32 fileID, const uint8* data, uint32 len, uint64 offset = 0);
    00395
    00401     bool closeFile (uint32 fileID);
    00402
    00410     bool getFileAttrs (fileAttrs& attrs, const char* filename, bool followSymLinks = true);
    00411
    00412
    00419     bool get (const char* remoteFile, FILE* localFile);
    00420
    00427     bool put (FILE* localFile, const char* remoteFile);
    00428
    00434     bool rm (const char* remoteFile);
    00435
    00442     bool mv (const char* oldFile, const char* newFile);
    00443
    00449     bool mkdir (const char* remoteDir);
    00450
    00456     bool rmdir (const char* remoteDir);
    00457
    00464     const char* ls (const char* remoteDir, bool longNames=false);
    00465
    00471     bool cd (const char* remoteDir);
    00472
    00479     bool chmod (const char* remoteFile, const char* mode);
    00480
    00488     bool chown (const char* remoteFile, uint32_t uid, uint32_t gid = 0);
    00489
    00495     bool isFile (const char* remoteFile);
    00496
    00502     bool isDir (const char* remoteFile);
    00503 };
    00504
    00505 #endif