1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at>                 |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/
#include "gate/net/sshclients.hpp"
#include "gate/exceptions.hpp"

namespace gate
{
    namespace net
    {

        SshClient::~SshClient() noexcept
        {
            gate_sshclient_disconnect(&this->impl);
            gate_sshclient_release(&this->impl);
        }
        gate_sshclient_t* SshClient::c_impl()
        {
            return &this->impl;
        }
        SshClient::SshClient(String const& server, uint16_t port, String const& user, String const& password)
        {
            result_t result = gate_sshclient_create(&this->impl, server.c_impl(), port);
            GATEXX_CHECK_EXCEPTION(result);
            result = gate_sshclient_connect(&this->impl, user.c_impl(), password.c_impl(), 0);
            if (GATE_FAILED(result))
            {
                gate_sshclient_release(&this->impl);
                GATEXX_RAISE_EXCEPTION(result, NULL, 0);
            }
        }

        SshClient::SshClient(String const& server, uint16_t port, String const& user,
            String const& publicKey, String const& privateKey, String const& passPhrase)
        {
            result_t result = gate_sshclient_create(&this->impl, server.c_impl(), port);
            GATEXX_CHECK_EXCEPTION(result);
            result = gate_sshclient_connect_keys(&this->impl, user.c_impl(),
                publicKey.c_impl(), privateKey.c_impl(), passPhrase.c_impl(), 0);
            if (GATE_FAILED(result))
            {
                gate_sshclient_release(&this->impl);
                GATEXX_RAISE_EXCEPTION(result, NULL, 0);
            }
        }


        Ref<SshClient> SshClient::create(String const& server, uint16_t port, String const& user, String const& password)
        {
            Ptr<SshClient> ptr(new SshClient(server, port, user, password));
            Ref<SshClient> ref(gate::moveRef(ptr));
            return ref;
        }

        Ref<SshClient> SshClient::create(String const& server, uint16_t port, String const& user,
            String const& publicKey, String const& privateKey, String const& passPhrase)
        {
            Ptr<SshClient> ptr(new SshClient(server, port, user, publicKey, privateKey, passPhrase));
            Ref<SshClient> ref(gate::moveRef(ptr));
            return ref;
        }


        SshFtp::SshFtp(SshClientRef connectedClient)
            : client(connectedClient)
        {
            if (!client)
            {
                GATEXX_RAISE_ERROR(results::NullPointer);
            }
            result_t result = gate_sshclient_open_sftp(client->c_impl(), &this->impl);
            GATEXX_CHECK_EXCEPTION(result);
        }
        SshFtp::~SshFtp() noexcept
        {
            gate_sshclient_close_sftp(&this->impl);
        }

        void SshFtp::createDirectory(String const& path)
        {
            result_t result = gate_sshclient_sftp_mkdir(&this->impl, path.c_impl());
            GATEXX_CHECK_EXCEPTION(result);
        }
        void SshFtp::removeDirectory(String const& path)
        {
            result_t result = gate_sshclient_sftp_rmdir(&this->impl, path.c_impl());
            GATEXX_CHECK_EXCEPTION(result);
        }
        void SshFtp::deleteFile(String const& path)
        {
            result_t result = gate_sshclient_sftp_unlink(&this->impl, path.c_impl());
            GATEXX_CHECK_EXCEPTION(result);
        }
        void SshFtp::move(String const& path, String const& newPath)
        {
            result_t result = gate_sshclient_sftp_rename(&this->impl, path.c_impl(), newPath.c_impl());
            GATEXX_CHECK_EXCEPTION(result);
        }

        File::Properties SshFtp::getProperties(String const& path)
        {
            gate_file_properties_t props;
            result_t result = gate_sshclient_sftp_stat(&this->impl, path.c_impl(), &props);
            GATEXX_CHECK_EXCEPTION(result);
            return File::Properties(props);
        }

        struct GATE_API_LOCAL SshFtp_listEntries_data
        {
            File::callback_t const* callback;
            void* userparam;
        };

        static gate_bool_t SshFtp_listEntries_cb(gate_file_entry_t const* entry, void* userparam)
        {
            SshFtp_listEntries_data* data = static_cast<SshFtp_listEntries_data*>(userparam);
            File::Entry fileEntry((*entry));
            bool_t continueListing = true;
            File::callback_t cb = *(data->callback);
            try
            {
                cb(&fileEntry, data->userparam, &continueListing);
            }
            catch (...) {}
            return continueListing;
        }

        void SshFtp::listEntries(String const& dirpath, File::callback_t const& callback, void* userparam)
        {
            SshFtp_listEntries_data data;
            data.callback = &callback;
            data.userparam = userparam;

            result_t result = gate_sshclient_sftp_listdir(&this->impl, dirpath.c_impl(), &SshFtp_listEntries_cb, &data);
            GATEXX_CHECK_EXCEPTION(result);
        }

        File::pathmap_t SshFtp::listEntryPaths(String const& dirpath)
        {
            File::pathmap_t ret;
            gate_sshclient_sftp_handle_t sftpHandle;
            bool_t continueRead = true;<--- Assignment 'continueRead=true', assigned value is 1

            result_t result = gate_sshclient_sftp_opendir(&this->impl, dirpath.c_impl(), &sftpHandle);
            GATEXX_CHECK_EXCEPTION(result);
            while (continueRead)<--- Condition 'continueRead' is always true
            {
                File::Entry fileEntry;
                result = gate_sshclient_sftp_readdir(&sftpHandle, &fileEntry);
                GATE_BREAK_IF_FAILED(result);
                ret.add(fileEntry.getPath(), fileEntry.getName());
            }
            gate_sshclient_sftp_close(&sftpHandle);

            if (result != GATE_RESULT_NODATA)
            {
                GATEXX_CHECK_EXCEPTION(result);
            }
            return ret;
        }

        gate_sshclient_sftp_t* SshFtp::c_impl()
        {
            return &this->impl;
        }

        SshClientRef SshFtp::getClient() const
        {
            return this->client;
        }



        SshFtpFile::SshFtpFile(SshFtp& sftp, String const& path, enumint_t streamOpenFlags)
            : client(sftp.getClient())
        {
            result_t result = gate_sshclient_sftp_open(sftp.c_impl(), path.c_impl(), streamOpenFlags, &this->impl);
            GATEXX_CHECK_EXCEPTION(result);
        }
        SshFtpFile::~SshFtpFile() noexcept
        {
            gate_sshclient_sftp_close(&this->impl);
        }

        size_t SshFtpFile::read(char* buffer, gate_size_t capacity)
        {
            gate_size_t received = 0;
            result_t result = gate_sshclient_sftp_read(&this->impl, buffer, capacity, &received);
            GATEXX_CHECK_EXCEPTION(result);
            return received;
        }
        size_t SshFtpFile::write(char const* buffer, gate_size_t length)
        {
            gate_size_t written = 0;
            result_t result = gate_sshclient_sftp_write(&this->impl, buffer, length, &written);
            GATEXX_CHECK_EXCEPTION(result);
            return written;
        }
        void SshFtpFile::seek(gate_uint64_t position)
        {
            result_t result = gate_sshclient_sftp_seek(&this->impl, position);
            GATEXX_CHECK_EXCEPTION(result);
        }
        uint64_t SshFtpFile::tell()
        {
            gate_uint64_t pos = 0;
            result_t result = gate_sshclient_sftp_tell(&this->impl, &pos);
            GATEXX_CHECK_EXCEPTION(result);
            return pos;
        }

        gate_sshclient_sftp_handle_t* SshFtpFile::c_impl()
        {
            return &this->impl;
        }
        SshClientRef SshFtpFile::getClient() const
        {
            return this->client;
        }


        enumint_t const SshCommandChannel::StreamType_Default = GATE_SSHCLIENT_STREAMTYPE_DEFAULT;
        enumint_t const SshCommandChannel::StreamType_Error = GATE_SSHCLIENT_STREAMTYPE_ERROR;

        SshCommandChannel::SshCommandChannel(SshClientRef const& connectedClient)
            : client(connectedClient),
            impl(NULL)
        {
            result_t result = gate_sshclient_channel_open(this->client->c_impl(), &this->impl);
            GATEXX_CHECK_EXCEPTION(result);
        }
        SshCommandChannel::~SshCommandChannel() noexcept
        {
            gate_sshclient_channel_free(&this->impl);
        }

        void SshCommandChannel::execute(String const& command)
        {
            result_t result = gate_sshclient_channel_exec(&this->impl, command.c_impl());
            GATEXX_CHECK_EXCEPTION(result);
        }
        bool_t SshCommandChannel::canRead(enumint_t streamType)
        {
            gate_bool_t value = false;
            result_t result = gate_sshclient_channel_canread(&this->impl, streamType, &value);
            GATEXX_CHECK_EXCEPTION(result);
            return value;
        }
        size_t SshCommandChannel::read(char* buffer, gate_size_t capacity, enumint_t streamType)
        {
            gate_size_t retrieved = 0;
            result_t result = gate_sshclient_channel_read(&this->impl, streamType, buffer, capacity, &retrieved);
            GATEXX_CHECK_EXCEPTION(result);
            return retrieved;
        }
        size_t SshCommandChannel::write(char const* buffer, gate_size_t length, enumint_t streamType)
        {
            gate_size_t processed = 0;
            result_t result = gate_sshclient_channel_write(&this->impl, streamType, buffer, length, &processed);
            GATEXX_CHECK_EXCEPTION(result);
            return processed;
        }
        bool SshCommandChannel::eof()
        {
            return gate_sshclient_channel_eof(&this->impl);
        }
        void SshCommandChannel::sendEof()
        {
            result_t result = gate_sshclient_channel_send_eof(&this->impl);
            GATEXX_CHECK_EXCEPTION(result);
        }
        void SshCommandChannel::waitEof()
        {
            result_t result = gate_sshclient_channel_wait_eof(&this->impl);
            GATEXX_CHECK_EXCEPTION(result);
        }
        void SshCommandChannel::setEnv(String const& name, String const& value)
        {
            result_t result = gate_sshclient_channel_setenv(&this->impl, name.c_impl(), value.c_impl());
            GATEXX_CHECK_EXCEPTION(result);
        }
        void SshCommandChannel::getExitSignal(String* signal, String* errmsg, String* langtag)
        {
            gate_string_t strsig = GATE_STRING_INIT_EMPTY;
            gate_string_t strerr = GATE_STRING_INIT_EMPTY;
            gate_string_t strlang = GATE_STRING_INIT_EMPTY;
            result_t result = gate_sshclient_channel_get_exit_signal(&this->impl, &strsig, &strerr, &strlang);
            GATEXX_CHECK_EXCEPTION(result);
            try
            {
                if (signal) *signal = String::createFrom(strsig);
                if (errmsg) *errmsg = String::createFrom(strerr);
                if (langtag) *langtag = String::createFrom(strlang);
            }
            catch (...)
            {
                gate_string_release(&strlang);
                gate_string_release(&strerr);
                gate_string_release(&strsig);
                throw;
            }
            gate_string_release(&strlang);
            gate_string_release(&strerr);
            gate_string_release(&strsig);
        }
        int SshCommandChannel::getExitStatus()
        {
            int status = 0;
            result_t result = gate_sshclient_channel_get_exit_status(&this->impl, &status);
            GATEXX_CHECK_EXCEPTION(result);
            return status;
        }

    }	//	end of namespace net
}	//	end of namespace gate