Guest User

Untitled

a guest
Jul 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.39 KB | None | 0 0
  1. --- /home/para/dev/APE/crash/APE_Server/modules/libape-spidermonkey.c 2010-11-29 00:17:35.000000000 +0100
  2. +++ libape-spidermonkey.c 2011-02-22 10:14:59.000000000 +0100
  3. @@ -27,6 +27,7 @@
  4. #include <mysac.h>
  5. #endif
  6. #include <jsapi.h>
  7. +#include <libssh2.h>
  8. #include <stdio.h>
  9. #include <glob.h>
  10. #include "plugins.h"
  11. @@ -162,6 +163,34 @@
  12. static struct _ape_mysql_queue *apemysql_push_queue(struct _ape_mysql_data *myhandle, char *query, unsigned int query_len, jsval callback);
  13. static void apemysql_shift_queue(struct _ape_mysql_data *myhandle);
  14. #endif
  15. +
  16. +typedef enum {
  17. + SSH_STARTUP,
  18. + SSH_AUTH,
  19. + SSH_REQ_CHANNEL,
  20. + SSH_REQ_PTY,
  21. + SSH_REQ_SHELL,
  22. + SSH_CONNECTED,
  23. + SSH_ERR
  24. +} ape_ssh_state;
  25. +
  26. +struct _ape_ssh_ctx
  27. +{
  28. + JSContext *cx;
  29. + JSObject *jsssh;
  30. +
  31. + struct {
  32. + char *login;
  33. + char *password;
  34. + } cred;
  35. +
  36. + LIBSSH2_SESSION *session;
  37. + LIBSSH2_CHANNEL *channel;
  38. +
  39. + ape_ssh_state state;
  40. + void *data;
  41. +
  42. +};
  43. //static JSBool sockserver_addproperty(JSContext *cx, JSObject *obj, jsval idval, jsval *vp);
  44.  
  45. static ace_plugin_infos infos_module = {
  46. @@ -267,6 +296,13 @@
  47. };
  48. #endif
  49.  
  50. +static JSClass ssh_class = {
  51. + "SSH", JSCLASS_HAS_PRIVATE,
  52. + JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
  53. + JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, apemysql_finalize,
  54. + JSCLASS_NO_OPTIONAL_MEMBERS
  55. +};
  56. +
  57. static JSClass cmdresponse_class = {
  58. "cmdresponse", JSCLASS_HAS_PRIVATE,
  59. JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
  60. @@ -274,6 +310,40 @@
  61. JSCLASS_NO_OPTIONAL_MEMBERS
  62. };
  63.  
  64. +APE_JS_NATIVE(apessh_sm_send)
  65. +//{
  66. + struct _ape_ssh_ctx *sshctx = JS_GetPrivate(cx, obj);
  67. + JSString *string;
  68. +
  69. + if (sshctx == NULL) {
  70. + return JS_TRUE;
  71. + }
  72. +
  73. + if (!JS_ConvertArguments(cx, argc, argv, "S", &string)) {
  74. + return JS_TRUE;
  75. + }
  76. +
  77. + libssh2_channel_write(sshctx->channel, JS_GetStringBytes(string), JS_GetStringLength(string));
  78. +
  79. + return JS_TRUE;
  80. +}
  81. +
  82. +APE_JS_NATIVE(apessh_sm_close)
  83. +//{
  84. + struct _ape_ssh_ctx *sshctx = JS_GetPrivate(cx, obj);
  85. + ape_socket *client;
  86. +
  87. + if (sshctx == NULL || sshctx->data == NULL) {
  88. + return JS_TRUE;
  89. + }
  90. +
  91. + client = (ape_socket *)sshctx->data;
  92. +
  93. + shutdown(client->fd, 2);
  94. +
  95. + return JS_TRUE;
  96. +}
  97. +
  98.  
  99. APE_JS_NATIVE(apesocket_write)
  100. //{
  101. @@ -1127,6 +1197,13 @@
  102. JS_FS_END
  103. };
  104.  
  105. +static JSFunctionSpec apessh_funcs[] = {
  106. + JS_FS("send", apessh_sm_send, 1, 0, 0),
  107. + JS_FS("close", apessh_sm_close, 0, 0, 0),
  108. + JS_FS_END
  109. +};
  110. +
  111. +
  112. static JSObject *sm_ape_socket_to_jsobj(JSContext *cx, ape_socket *client)
  113. {
  114. /*if (client->data != NULL) {
  115. @@ -1258,6 +1335,168 @@
  116. }
  117. }
  118.  
  119. +/* Async State machine pour libssh2 */
  120. +static void ape_ssh_io(ape_socket *client, int way, acetables *g_ape)
  121. +{
  122. + int ret = 0, err = 0;
  123. + struct _ape_ssh_ctx *sshctx = client->attach;
  124. + const char *login, *pass;
  125. +
  126. + for (;;) {
  127. + switch(sshctx->state) {
  128. + case SSH_STARTUP:
  129. + if ((ret = libssh2_session_startup(sshctx->session, client->fd)) == 0) {
  130. + sshctx->state = SSH_AUTH;
  131. + } else if (ret != LIBSSH2_ERROR_EAGAIN) {
  132. + err = 1;
  133. + sshctx->state = SSH_ERR;
  134. + }
  135. + break;
  136. + case SSH_AUTH:
  137. + /* hmm, bug in the libssh2 macro?, useless ptr : */
  138. + login = sshctx->cred.login;
  139. + pass = sshctx->cred.password;
  140. +
  141. + if ((ret = libssh2_userauth_password(sshctx->session, login, pass)) == 0) {
  142. + sshctx->state = SSH_REQ_CHANNEL;
  143. + } else if (ret != LIBSSH2_ERROR_EAGAIN) {
  144. + err = 2;
  145. + sshctx->state = SSH_ERR;
  146. + }
  147. + break;
  148. + case SSH_REQ_CHANNEL:
  149. + if ((sshctx->channel = libssh2_channel_open_session(sshctx->session)) == NULL) {
  150. + ret = libssh2_session_last_errno(sshctx->session);
  151. + } else {
  152. + sshctx->state = SSH_REQ_PTY;
  153. + break;
  154. + }
  155. + if (sshctx->channel == NULL && ret != LIBSSH2_ERROR_EAGAIN) {
  156. + err = 3;
  157. + sshctx->state = SSH_ERR;
  158. + }
  159. + break;
  160. + case SSH_REQ_PTY:
  161. + if ((ret = libssh2_channel_request_pty(sshctx->channel, "vt100")) == 0) {
  162. + sshctx->state = SSH_REQ_SHELL;
  163. +
  164. + } else if (ret != LIBSSH2_ERROR_EAGAIN) {
  165. + err = 4;
  166. + sshctx->state = SSH_ERR;
  167. + }
  168. + break;
  169. + case SSH_REQ_SHELL:
  170. + if ((ret = libssh2_channel_shell(sshctx->channel)) == 0) {
  171. + jsval rval;
  172. + sshctx->state = SSH_CONNECTED;
  173. +
  174. + JS_CallFunctionName(sshctx->cx, sshctx->jsssh, "onShell", 0, NULL, &rval);
  175. + } else if (ret != LIBSSH2_ERROR_EAGAIN) {
  176. + err = 5;
  177. + sshctx->state = SSH_ERR;
  178. + }
  179. + break;
  180. + case SSH_CONNECTED:
  181. + {
  182. + char *buf = xmalloc(sizeof(char) * 2048);
  183. + int len = 0, r = 0;
  184. +
  185. + while ((r = libssh2_channel_read(sshctx->channel, buf + len, 2048)) > 0) {
  186. + len += r;
  187. + buf = realloc(buf, len + 2048);
  188. +
  189. + }
  190. + if (len > 0) {
  191. + jsval params[1], rval;
  192. + params[0] = STRING_TO_JSVAL(JS_NewStringCopyN(sshctx->cx, buf, len));
  193. +
  194. + JS_CallFunctionName(sshctx->cx, sshctx->jsssh, "onRead", 1, params, &rval);
  195. +
  196. + }
  197. + free(buf);
  198. + if (r == -1) {
  199. + err = 6;
  200. + sshctx->state = SSH_ERR;
  201. + break;
  202. + }
  203. +
  204. + }
  205. + //printf("[APE SSH] Connected !\n");
  206. + return;
  207. + break;
  208. + case SSH_ERR:
  209. + {
  210. + jsval params[1], rval;
  211. + params[0] = INT_TO_JSVAL(err);
  212. +
  213. + JS_CallFunctionName(sshctx->cx, sshctx->jsssh, "onError", 1, params, &rval);
  214. +
  215. + if (err > 1) {
  216. + libssh2_session_disconnect(sshctx->session, "Shutdown");
  217. + }
  218. + if (err > 3) {
  219. + libssh2_channel_free(sshctx->channel);
  220. + }
  221. +
  222. + shutdown(client->fd, 2);
  223. + close_socket(client->fd, g_ape);
  224. +
  225. + libssh2_session_free(sshctx->session);
  226. +
  227. + free(sshctx->cred.login);
  228. + free(sshctx->cred.password);
  229. +
  230. + JS_SetPrivate(sshctx->cx, sshctx->jsssh, NULL);
  231. + JS_RemoveRoot(sshctx->cx, &sshctx->jsssh);
  232. + client->attach = NULL;
  233. +
  234. + free(sshctx);
  235. +
  236. + }
  237. + return;
  238. +
  239. + }
  240. + if (ret == LIBSSH2_ERROR_EAGAIN) {
  241. + break;
  242. + }
  243. + }
  244. +}
  245. +
  246. +static void ape_ssh_io_read(ape_socket *client, ape_buffer *buf, size_t offset, acetables *g_ape)
  247. +{
  248. + struct _ape_ssh_ctx *sshctx = client->attach;
  249. +
  250. + if (sshctx != NULL && libssh2_session_block_directions(sshctx->session) & LIBSSH2_SESSION_BLOCK_INBOUND) {
  251. + ape_ssh_io(client, LIBSSH2_SESSION_BLOCK_INBOUND, g_ape);
  252. + }
  253. +
  254. +}
  255. +
  256. +static void ape_ssh_io_write(ape_socket *client, acetables *g_ape)
  257. +{
  258. + struct _ape_ssh_ctx *sshctx = client->attach;
  259. +
  260. + if (sshctx != NULL && libssh2_session_block_directions(sshctx->session) & LIBSSH2_SESSION_BLOCK_OUTBOUND) {
  261. + ape_ssh_io(client, LIBSSH2_SESSION_BLOCK_OUTBOUND, g_ape);
  262. + }
  263. +}
  264. +
  265. +
  266. +static void sm_ssh_onconnect(ape_socket *client, acetables *g_ape)
  267. +{
  268. + struct _ape_ssh_ctx *sshctx = client->attach;
  269. +
  270. + sshctx->data = client;
  271. +
  272. + client->stream_type = STREAM_DELEGATE;
  273. + client->callbacks.on_read = ape_ssh_io_read;
  274. + client->callbacks.on_write = ape_ssh_io_write;
  275. +
  276. + ape_ssh_io(client, 0, g_ape);
  277. +
  278. + /* turn to delegate */
  279. +}
  280. +
  281. static void sm_sock_onconnect(ape_socket *client, acetables *g_ape)
  282. {
  283. jsval rval;
  284. @@ -2503,6 +2742,73 @@
  285. return nqueue;
  286. }
  287.  
  288. +/*
  289. +struct _ape_ssh_ctx
  290. +{
  291. + JSContext *ctx;
  292. + JSObject *jsssh;
  293. +
  294. + struct {
  295. + const char *login;
  296. + const char *password;
  297. + } cred;
  298. +
  299. + LIBSSH2_SESSION *session;
  300. +
  301. + int state;
  302. + void *data;
  303. +
  304. +};
  305. +*/
  306. +
  307. +
  308. +APE_JS_NATIVE(ape_sm_ssh_constructor)
  309. +//{
  310. + LIBSSH2_SESSION *session;
  311. + char *ip, *login, *pass;
  312. +
  313. + ape_socket *pattern;
  314. + struct _ape_ssh_ctx *sshctx;
  315. +
  316. + /* var ssh = new Ape.SSH('ape-project.org', 'root', 'lefu-'); */
  317. + if (!JS_ConvertArguments(cx, argc, argv, "sss", &ip, &login, &pass)) {
  318. + return JS_TRUE;
  319. + }
  320. +
  321. + session = libssh2_session_init();
  322. +
  323. + if (session == 0) {
  324. + return JS_TRUE;
  325. + }
  326. +
  327. + libssh2_session_set_blocking(session, 0);
  328. +
  329. + sshctx = xmalloc(sizeof(*sshctx));
  330. +
  331. + sshctx->cred.login = xstrdup(login);
  332. + sshctx->cred.password = xstrdup(pass);
  333. + sshctx->session = session;
  334. + sshctx->cx = cx;
  335. + sshctx->jsssh = obj;
  336. + sshctx->state = SSH_STARTUP;
  337. + sshctx->data = NULL;
  338. +
  339. + JS_AddRoot(cx, &sshctx->jsssh);
  340. +
  341. + pattern = xmalloc(sizeof(*pattern));
  342. +
  343. + pattern->callbacks.on_connect = sm_ssh_onconnect;
  344. + pattern->callbacks.on_disconnect = NULL;
  345. +
  346. + pattern->attach = sshctx;
  347. +
  348. + JS_SetPrivate(cx, obj, sshctx);
  349. +
  350. + ape_connect_name(ip, 22, pattern, g_ape);
  351. +
  352. + return JS_TRUE;
  353. +}
  354. +
  355. APE_JS_NATIVE(ape_sm_mysql_constructor)
  356. //{
  357. char *host, *login, *pass, *db;
  358. @@ -2678,6 +2984,7 @@
  359. #ifdef _USE_MYSQL
  360. JSObject *jsmysql;
  361. #endif
  362. + JSObject *jsssh;
  363.  
  364. obj = JS_DefineObject(asc->cx, asc->global, "Ape", &ape_class, NULL, 0);
  365. b64 = JS_DefineObject(asc->cx, obj, "base64", &b64_class, NULL, 0);
  366. @@ -2707,10 +3014,13 @@
  367. #ifdef _USE_MYSQL
  368. jsmysql = JS_InitClass(asc->cx, obj, NULL, &mysql_class, ape_sm_mysql_constructor, 2, NULL, NULL, NULL, apemysql_funcs_static);
  369. #endif
  370. + jsssh = JS_InitClass(asc->cx, obj, NULL, &ssh_class, ape_sm_ssh_constructor, 3, NULL, NULL, NULL, NULL);
  371. JS_InitClass(asc->cx, obj, NULL, &raw_class, ape_sm_raw_constructor, 1, NULL, NULL, NULL, NULL); /* Not used */
  372.  
  373. JS_DefineFunctions(asc->cx, sockclient, apesocket_client_funcs);
  374. JS_DefineFunctions(asc->cx, sockclient, apesocket_funcs);
  375. +
  376. + JS_DefineFunctions(asc->cx, jsssh, apessh_funcs);
  377.  
  378. JS_DefineFunctions(asc->cx, sockserver, apesocket_client_funcs);
  379. JS_DefineFunctions(asc->cx, sockserver, apesocketserver_funcs);
  380. @@ -2886,7 +3196,7 @@
  381.  
  382. glob_t globbuf;
  383.  
  384. - rt = JS_NewRuntime(8L * 1024L * 1024L);
  385. + rt = JS_NewRuntime(128L * 1024L * 1024L);
  386.  
  387. if (rt == NULL) {
  388. printf("[ERR] Not enougth memory\n");
  389. @@ -2912,6 +3222,10 @@
  390.  
  391. glob(rpath, 0, NULL, &globbuf);
  392.  
  393. + if (libssh2_init(0) != 0) {
  394. + printf("[SSH] Error: Failed to initialize libssh2\n");
  395. + }
  396. +
  397. for (i = 0; i < globbuf.gl_pathc; i++) {
  398. ape_sm_compiled *asc = xmalloc(sizeof(*asc));
Add Comment
Please, Sign In to add comment