glue.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. Copyright (c) 2009-2022 Bruce A Henderson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the author nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
  15. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <libssh2.h>
  26. #include <libssh2_sftp.h>
  27. class MaxSSHSession;
  28. extern "C" {
  29. #include "blitz.h"
  30. void net_libssh2_TSSHSession__kbdCallback(BBObject * session, BBString * name, BBString * instruction,
  31. BBArray * prompts, BBArray * responses);
  32. BBArray * net_libssh2_TSSHSession__newPrompts(int size);
  33. void net_libssh2_TSSHSession__setPrompt(BBArray * array, int index, const LIBSSH2_USERAUTH_KBDINT_PROMPT * prompt);
  34. BBArray * net_libssh2_TSSHSession__newResponses(int size);
  35. void net_libssh2_TSSHSession__setResponse(BBArray * array, int index, LIBSSH2_USERAUTH_KBDINT_RESPONSE * response);
  36. int bmx_libssh2_session_disconnect(MaxSSHSession * session, BBString * description);
  37. MaxSSHSession * bmx_libssh2_session_create(BBObject * handle);
  38. int bmx_libssh2_session_handshake(MaxSSHSession * session, libssh2_socket_t socket);
  39. int bmx_libssh2_session_free(MaxSSHSession * session);
  40. LIBSSH2_CHANNEL * bmx_libssh2_channel_open(MaxSSHSession * session);
  41. int bmx_libssh2_userauth_keyboard_interactive(MaxSSHSession * session, BBString * username);
  42. int bmx_libssh2_userauth_password(MaxSSHSession * session, BBString * username, BBString * password);
  43. int bmx_libssh2_userauth_authenticated(MaxSSHSession * session);
  44. BBString * bmx_libssh2_kbdint_prompt_gettext(LIBSSH2_USERAUTH_KBDINT_PROMPT * prompt);
  45. int bmx_libssh2_kbdint_prompt_echo(LIBSSH2_USERAUTH_KBDINT_PROMPT * prompt);
  46. void bmx_libssh2_kbdint_response_settext(LIBSSH2_USERAUTH_KBDINT_RESPONSE * response, BBString * text);
  47. int bmx_libssh2_channel_setenv(LIBSSH2_CHANNEL * channel, BBString * name, BBString * value);
  48. int bmx_libssh2_channel_requestpty(LIBSSH2_CHANNEL * channel, BBString * term);
  49. int bmx_libssh2_channel_shell(LIBSSH2_CHANNEL * channel);
  50. int bmx_libssh2_channel_exec(LIBSSH2_CHANNEL * channel, BBString * message);
  51. int bmx_libssh2_channel_subsystem(LIBSSH2_CHANNEL * channel, BBString * message);
  52. int bmx_libssh2_channel_processstartup(LIBSSH2_CHANNEL * channel, BBString * request, BBString * message);
  53. void bmx_libssh2_channel_free(LIBSSH2_CHANNEL * channel);
  54. int bmx_libssh2_channel_read(LIBSSH2_CHANNEL * channel, char * buffer, int size);
  55. int bmx_libssh2_channel_read_stderr(LIBSSH2_CHANNEL * channel, char * buffer, int size);
  56. int bmx_libssh2_channel_write(LIBSSH2_CHANNEL * channel, char * buffer, int size);
  57. int bmx_libssh2_channel_write_stderr(LIBSSH2_CHANNEL * channel, char * buffer, int size);
  58. int bmx_libssh2_channel_eof(LIBSSH2_CHANNEL * channel);
  59. void bmx_libssh2_channel_set_blocking(LIBSSH2_CHANNEL * channel, int blocking);
  60. int bmx_libssh2_channel_pollread(LIBSSH2_CHANNEL * channel, int extended);
  61. int bmx_libssh2_channel_sendeof(LIBSSH2_CHANNEL * channel);
  62. int bmx_libssh2_channel_waitclosed(LIBSSH2_CHANNEL * channel);
  63. int bmx_libssh2_channel_waiteof(LIBSSH2_CHANNEL * channel);
  64. int bmx_libssh2_channel_getexitstatus(LIBSSH2_CHANNEL * channel);
  65. LIBSSH2_CHANNEL * bmx_libssh2_channel_directtcpip(MaxSSHSession * session, BBString * host, int port, BBString * shost, int sport);
  66. LIBSSH2_SFTP * bmx_libssh2_sftp_init(MaxSSHSession * session);
  67. int bmx_libssh2_sftp_shutdown(LIBSSH2_SFTP * sftp);
  68. int bmx_libssh2_sftp_last_error(LIBSSH2_SFTP * sftp);
  69. LIBSSH2_CHANNEL * bmx_libssh2_sftp_get_channel(LIBSSH2_SFTP * sftp);
  70. int bmx_libssh2_sftp_rename(LIBSSH2_SFTP * sftp, BBString * sourcefile, BBString * destfile, int flags);
  71. int bmx_libssh2_sftp_mkdir(LIBSSH2_SFTP * sftp, BBString * path, int mode);
  72. int bmx_libssh2_sftp_rmdir(LIBSSH2_SFTP * sftp, BBString * path);
  73. LIBSSH2_SFTP_HANDLE * bmx_libssh2_sftp_open(LIBSSH2_SFTP * sftp, BBString * filename, int flags, int mode, int openType);
  74. size_t bmx_libssh2_sftp_read(LIBSSH2_SFTP_HANDLE * handle, char * buf, size_t length);
  75. size_t bmx_libssh2_sftp_write(LIBSSH2_SFTP_HANDLE * handle, char * buf, size_t count);
  76. size_t bmx_libssh2_sftp_tell(LIBSSH2_SFTP_HANDLE * handle);
  77. void bmx_libssh2_sftp_seek64(LIBSSH2_SFTP_HANDLE * handle, BBInt64 offset);
  78. int bmx_libssh2_sftp_fsync(LIBSSH2_SFTP_HANDLE * handle);
  79. int bmx_libssh2_sftp_close_handle(LIBSSH2_SFTP_HANDLE * handle);
  80. }
  81. class MaxSSHSession
  82. {
  83. public:
  84. MaxSSHSession(BBObject * handle)
  85. : maxHandle(handle)
  86. {
  87. session = libssh2_session_init_ex(NULL, NULL, NULL, this);
  88. }
  89. ~MaxSSHSession()
  90. {
  91. }
  92. LIBSSH2_SESSION * Session() {
  93. return session;
  94. }
  95. BBObject * Handle() {
  96. return maxHandle;
  97. }
  98. private:
  99. BBObject * maxHandle;
  100. LIBSSH2_SESSION * session;
  101. };
  102. // ***************************************************
  103. void _interactive_callback(const char *name, int name_len,
  104. const char *instruction, int instruction_len, int num_prompts,
  105. const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
  106. LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
  107. void **abstract) {
  108. BBArray * _prompts = net_libssh2_TSSHSession__newPrompts(num_prompts);
  109. BBArray * _responses = net_libssh2_TSSHSession__newResponses(num_prompts);
  110. for (int i = 0; i < num_prompts; i++) {
  111. net_libssh2_TSSHSession__setPrompt(_prompts, i, prompts + i);
  112. net_libssh2_TSSHSession__setResponse(_responses, i, responses + i);
  113. }
  114. net_libssh2_TSSHSession__kbdCallback(((MaxSSHSession*)*abstract)->Handle(), bbStringFromBytes((const unsigned char *)name, name_len),
  115. bbStringFromBytes((const unsigned char *)instruction, instruction_len), _prompts, _responses);
  116. }
  117. // ***************************************************
  118. MaxSSHSession * bmx_libssh2_session_create(BBObject * handle) {
  119. return new MaxSSHSession(handle);
  120. }
  121. int bmx_libssh2_session_handshake(MaxSSHSession * session, libssh2_socket_t socket) {
  122. return libssh2_session_handshake(session->Session(), socket);
  123. }
  124. int bmx_libssh2_session_free(MaxSSHSession * session) {
  125. return libssh2_session_free(session->Session());
  126. }
  127. int bmx_libssh2_userauth_keyboard_interactive(MaxSSHSession * session, BBString * username) {
  128. char *p = (char*)bbStringToUTF8String( username );
  129. int ret = libssh2_userauth_keyboard_interactive(session->Session(), p, &_interactive_callback);
  130. bbMemFree( p );
  131. return ret;
  132. }
  133. LIBSSH2_CHANNEL * bmx_libssh2_channel_open(MaxSSHSession * session) {
  134. return libssh2_channel_open_session(session->Session());
  135. }
  136. int bmx_libssh2_session_disconnect(MaxSSHSession * session, BBString * description) {
  137. char *d = (char*)bbStringToUTF8String( description );
  138. int ret = libssh2_session_disconnect(session->Session(), d);
  139. bbMemFree( d );
  140. return ret;
  141. }
  142. int bmx_libssh2_userauth_password(MaxSSHSession * session, BBString * username, BBString * password) {
  143. char *u = (char*)bbStringToUTF8String( username );
  144. char *p = (char*)bbStringToUTF8String( password );
  145. int ret = libssh2_userauth_password(session->Session(), u, p);
  146. bbMemFree( u );
  147. bbMemFree( p );
  148. return ret;
  149. }
  150. int bmx_libssh2_userauth_authenticated(MaxSSHSession * session) {
  151. return libssh2_userauth_authenticated(session->Session());
  152. }
  153. // ***************************************************
  154. BBString * bmx_libssh2_kbdint_prompt_gettext(LIBSSH2_USERAUTH_KBDINT_PROMPT * prompt) {
  155. return bbStringFromCString(prompt->text);
  156. }
  157. int bmx_libssh2_kbdint_prompt_echo(LIBSSH2_USERAUTH_KBDINT_PROMPT * prompt) {
  158. return static_cast<int>(prompt->echo);
  159. }
  160. // ***************************************************
  161. void bmx_libssh2_kbdint_response_settext(LIBSSH2_USERAUTH_KBDINT_RESPONSE * response, BBString * text) {
  162. char *p = (char*)bbStringToUTF8String( text );
  163. response->text = strdup(p);
  164. response->length = strlen(p);
  165. bbMemFree( p );
  166. }
  167. // ***************************************************
  168. int bmx_libssh2_channel_setenv(LIBSSH2_CHANNEL * channel, BBString * name, BBString * value) {
  169. char *n = (char*)bbStringToUTF8String( name );
  170. char *v = (char*)bbStringToUTF8String( value );
  171. int ret = libssh2_channel_setenv(channel, n, v);
  172. bbMemFree(n);
  173. bbMemFree(v);
  174. return ret;
  175. }
  176. int bmx_libssh2_channel_requestpty(LIBSSH2_CHANNEL * channel, BBString * term) {
  177. char *t = (char*)bbStringToCString( term );
  178. int ret = libssh2_channel_request_pty(channel, t);
  179. bbMemFree(t);
  180. return ret;
  181. }
  182. int bmx_libssh2_channel_shell(LIBSSH2_CHANNEL * channel) {
  183. return libssh2_channel_shell(channel);
  184. }
  185. int bmx_libssh2_channel_exec(LIBSSH2_CHANNEL * channel, BBString * message) {
  186. char *m = (char*)bbStringToCString( message );
  187. int ret = libssh2_channel_exec(channel, m);
  188. bbMemFree(m);
  189. return ret;
  190. }
  191. int bmx_libssh2_channel_subsystem(LIBSSH2_CHANNEL * channel, BBString * message) {
  192. char *m = (char*)bbStringToCString( message );
  193. int ret = libssh2_channel_subsystem(channel, m);
  194. bbMemFree(m);
  195. return ret;
  196. }
  197. int bmx_libssh2_channel_processstartup(LIBSSH2_CHANNEL * channel, BBString * request, BBString * message) {
  198. char *r = (char*)bbStringToUTF8String( request );
  199. char *m = (char*)bbStringToUTF8String( message );
  200. int ret = libssh2_channel_process_startup(channel, r, strlen(r), m, strlen(m));
  201. bbMemFree(r);
  202. bbMemFree(m);
  203. return ret;
  204. }
  205. void bmx_libssh2_channel_free(LIBSSH2_CHANNEL * channel) {
  206. libssh2_channel_free(channel);
  207. }
  208. int bmx_libssh2_channel_read(LIBSSH2_CHANNEL * channel, char * buffer, int size) {
  209. return static_cast<int>(libssh2_channel_read(channel, buffer, static_cast<size_t>(size)));
  210. }
  211. int bmx_libssh2_channel_read_stderr(LIBSSH2_CHANNEL * channel, char * buffer, int size) {
  212. return static_cast<int>(libssh2_channel_read_stderr(channel, buffer, static_cast<size_t>(size)));
  213. }
  214. int bmx_libssh2_channel_write(LIBSSH2_CHANNEL * channel, char * buffer, int size) {
  215. return static_cast<int>(libssh2_channel_write(channel, buffer, static_cast<size_t>(size)));
  216. }
  217. int bmx_libssh2_channel_write_stderr(LIBSSH2_CHANNEL * channel, char * buffer, int size) {
  218. return static_cast<int>(libssh2_channel_write_stderr(channel, buffer, static_cast<size_t>(size)));
  219. }
  220. int bmx_libssh2_channel_eof(LIBSSH2_CHANNEL * channel) {
  221. return libssh2_channel_eof(channel);
  222. }
  223. void bmx_libssh2_channel_set_blocking(LIBSSH2_CHANNEL * channel, int blocking) {
  224. libssh2_channel_set_blocking(channel, blocking);
  225. }
  226. int bmx_libssh2_channel_pollread(LIBSSH2_CHANNEL * channel, int extended) {
  227. return libssh2_poll_channel_read(channel, extended);
  228. }
  229. int bmx_libssh2_channel_sendeof(LIBSSH2_CHANNEL * channel) {
  230. return libssh2_channel_send_eof(channel);
  231. }
  232. int bmx_libssh2_channel_waitclosed(LIBSSH2_CHANNEL * channel) {
  233. return libssh2_channel_wait_closed(channel);
  234. }
  235. int bmx_libssh2_channel_waiteof(LIBSSH2_CHANNEL * channel) {
  236. return libssh2_channel_wait_eof(channel);
  237. }
  238. int bmx_libssh2_channel_getexitstatus(LIBSSH2_CHANNEL * channel) {
  239. return libssh2_channel_get_exit_status(channel);
  240. }
  241. LIBSSH2_CHANNEL * bmx_libssh2_channel_directtcpip(MaxSSHSession * session, BBString * host, int port, BBString * shost, int sport) {
  242. LIBSSH2_CHANNEL * channel;
  243. char *h = (char*)bbStringToUTF8String( host );
  244. char * sh = 0;
  245. if (shost != &bbEmptyString) {
  246. sh = (char*)bbStringToUTF8String( shost );
  247. }
  248. if (sh) {
  249. channel = libssh2_channel_direct_tcpip_ex(session->Session(), h, port, sh, sport);
  250. } else {
  251. channel = libssh2_channel_direct_tcpip(session->Session(), h, port);
  252. }
  253. bbMemFree(h);
  254. if (sh) bbMemFree(sh);
  255. return channel;
  256. }
  257. // ***************************************************
  258. LIBSSH2_SFTP * bmx_libssh2_sftp_init(MaxSSHSession * session) {
  259. return libssh2_sftp_init(session->Session());
  260. }
  261. int bmx_libssh2_sftp_shutdown(LIBSSH2_SFTP * sftp) {
  262. return libssh2_sftp_shutdown(sftp);
  263. }
  264. int bmx_libssh2_sftp_last_error(LIBSSH2_SFTP * sftp) {
  265. return libssh2_sftp_last_error(sftp);
  266. }
  267. LIBSSH2_CHANNEL * bmx_libssh2_sftp_get_channel(LIBSSH2_SFTP * sftp) {
  268. return libssh2_sftp_get_channel(sftp);
  269. }
  270. int bmx_libssh2_sftp_rename(LIBSSH2_SFTP * sftp, BBString * sourcefile, BBString * destfile, int flags) {
  271. char *s = (char*)bbStringToUTF8String( sourcefile );
  272. char *d = (char*)bbStringToUTF8String( destfile );
  273. int res = libssh2_sftp_rename_ex(sftp, s, strlen(s), d, strlen(d), flags);
  274. bbMemFree(d);
  275. bbMemFree(s);
  276. return res;
  277. }
  278. int bmx_libssh2_sftp_mkdir(LIBSSH2_SFTP * sftp, BBString * path, int mode) {
  279. char *p = (char*)bbStringToUTF8String( path );
  280. int res = libssh2_sftp_mkdir(sftp, p, mode);
  281. bbMemFree(p);
  282. return res;
  283. }
  284. int bmx_libssh2_sftp_rmdir(LIBSSH2_SFTP * sftp, BBString * path) {
  285. char *p = (char*)bbStringToUTF8String( path );
  286. int res = libssh2_sftp_rmdir(sftp, p);
  287. bbMemFree(p);
  288. return res;
  289. }
  290. LIBSSH2_SFTP_HANDLE * bmx_libssh2_sftp_open(LIBSSH2_SFTP * sftp, BBString * filename, int flags, int mode, int openType) {
  291. char *f = (char*)bbStringToUTF8String( filename );
  292. LIBSSH2_SFTP_HANDLE * handle = libssh2_sftp_open_ex(sftp, f, strlen(f), flags, mode, openType);
  293. bbMemFree(f);
  294. return handle;
  295. }
  296. // ***************************************************
  297. size_t bmx_libssh2_sftp_read(LIBSSH2_SFTP_HANDLE * handle, char * buf, size_t length) {
  298. return libssh2_sftp_read(handle, buf, length);
  299. }
  300. size_t bmx_libssh2_sftp_write(LIBSSH2_SFTP_HANDLE * handle, char * buf, size_t count) {
  301. return libssh2_sftp_write(handle, buf, count);
  302. }
  303. size_t bmx_libssh2_sftp_tell(LIBSSH2_SFTP_HANDLE * handle) {
  304. return libssh2_sftp_tell(handle);
  305. }
  306. void bmx_libssh2_sftp_seek64(LIBSSH2_SFTP_HANDLE * handle, BBInt64 offset) {
  307. libssh2_sftp_seek64(handle, offset);
  308. }
  309. int bmx_libssh2_sftp_fsync(LIBSSH2_SFTP_HANDLE * handle) {
  310. return libssh2_sftp_fsync(handle);
  311. }
  312. int bmx_libssh2_sftp_close_handle(LIBSSH2_SFTP_HANDLE * handle) {
  313. return libssh2_sftp_close_handle(handle);
  314. }