Advertisement
Guest User

Untitled

a guest
Jun 4th, 2011
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.10 KB | None | 0 0
  1. #include <libssh/libssh.h>
  2. #include <libssh/server.h>
  3. #include <libssh/callbacks.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <glib.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <poll.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <netdb.h>
  14. #include "key.h" //jeetu - temporary hardcoded key
  15.  
  16. #ifndef KEYS_FOLDER
  17. #ifdef _WIN32
  18. #define KEYS_FOLDER
  19. #else
  20. #define KEYS_FOLDER "/home/jeetu/tmp/" //jeetu - temporary
  21. #endif
  22. #endif
  23.  
  24. #define MAX_X11_AUTH_PROTO_STR_SZ 18
  25. #define MAX_X11_AUTH_COOKIE_STR_SZ 50
  26.  
  27. //jeetu - all hardcoded defines; should probably figure out how these values came to be in the orig openssh code
  28. #define MAX_DISPLAYS 1000
  29. #define NI_MAXSERV 32
  30. #define NUM_SOCKS 10
  31. #define SSH_LISTEN_BACKLOG 128
  32.  
  33.  
  34. static int copy_chan_to_fd(ssh_session session,
  35. ssh_channel channel,
  36. void *data,
  37. uint32_t len,
  38. int is_stderr,
  39. void *userdata);
  40.  
  41. static void chan_close(ssh_session session, ssh_channel channel, void *userdata);
  42. static int copy_fd_to_chan(socket_t fd, int revents, void *userdata);
  43.  
  44. /*
  45. struct ssh_channel_callbacks_struct cb = {
  46. .channel_data_function = copy_chan_to_fd,
  47. .channel_eof_function = chan_close,
  48. .channel_close_function = chan_close,
  49. .userdata = NULL
  50. };
  51. */
  52.  
  53.  
  54. typedef struct x11_session_struct
  55. {
  56. char *x11_auth_cookie;
  57. char *x11_auth_protocol;
  58. int screen_number;
  59. int single_connection;
  60. unsigned int display_number;
  61. } x11_session;
  62.  
  63. typedef struct {
  64. ssh_session session;
  65. int *sockets;
  66. } WaitThreadData;
  67.  
  68. static WaitThreadData * thread_data_new (void);
  69. int authenticate_user(ssh_session session);
  70. int pubkey_auth(char *pk64);
  71. int server_loop(ssh_session session);
  72. int session_x11_req(ssh_session session,ssh_message message,x11_session* x11session);
  73. int session_setup_x11fwd(ssh_session session,x11_session* x11session);
  74. int x11_create_display_inet(ssh_session session,unsigned int *display_numberp, int *sockets);
  75. static gpointer wait_for_something(gpointer user_data);
  76. static gpointer server_thread(gpointer session_data);
  77. int exec_command(const char *command,x11_session* x11session);
  78.  
  79. //struct x11_session_struct x11session; //jeetu - may not need to be global
  80. ssh_channel chan=0;
  81. ssh_session *session;
  82. /*static GMutex *mutex_for_copy_fd_to_chan = NULL;
  83. static GMutex *mutex_for_dopoll = NULL;
  84. */
  85.  
  86. /* Return Values:
  87. * 0 - Success
  88. * 1 - ssh_bind_listen failed - error listening to socket
  89. * 2 - ssh_bind_accept failed - error accepting a connection
  90. * 3 - ssh_handle_key_change failed
  91. * 4 - authenticate_user failed
  92. */
  93. int main(int argc, char **argv)
  94. {
  95. ssh_bind sshbind;
  96. int auth=0;
  97. int sftp=0;
  98. int i;
  99. int r;
  100. int port = 2000;
  101. ssh_string pubkey = NULL;
  102. char *pk64 = NULL;
  103. int signature_state = SSH_PUBLICKEY_STATE_NONE;
  104. int rc = 0;
  105. int exec_req=0;
  106. int verbosity = SSH_LOG_FUNCTIONS;
  107. int session_count = 0;
  108.  
  109. g_thread_init(NULL);
  110. /* g_assert(mutex_for_copy_fd_to_chan == NULL);
  111. mutex_for_copy_fd_to_chan = g_mutex_new(); //jeetu - free mutex
  112. g_assert(mutex_for_dopoll == NULL);
  113. mutex_for_dopoll = g_mutex_new(); //jeetu - free mutex*/
  114.  
  115. ssh_threads_set_callbacks(ssh_threads_get_pthread());
  116. if(ssh_init() == -1)
  117. {
  118. printf("\nError initializing ssh: ssh_init() failed");
  119. exit(1);
  120. }
  121.  
  122. sshbind=ssh_bind_new();
  123. session = (ssh_session *) ssh_new();
  124.  
  125. ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT,&port);
  126. ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_DSAKEY, KEYS_FOLDER "ssh_host_dsa_key");
  127. ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, KEYS_FOLDER "ssh_host_rsa_key");
  128.  
  129. if(ssh_bind_listen(sshbind)<0)
  130. {
  131. printf("Error listening to socket: %s\n",ssh_get_error(sshbind));
  132. return 1;
  133. }
  134.  
  135. while(1)
  136. {
  137. session[session_count]=ssh_new();
  138. ssh_options_getopt(session[session_count],&argc,argv);
  139. r=ssh_bind_accept(sshbind,session[session_count]);
  140. if(r==SSH_ERROR)
  141. {
  142. printf("error accepting a connection : %s\n",ssh_get_error(sshbind));
  143. return 2;
  144. }
  145. if(ssh_handle_key_exchange(session[session_count]))
  146. {
  147. printf("ssh_handle_key_exchange: %s\n",ssh_get_error(session[session_count]));
  148. return 3;
  149. }
  150.  
  151. /* public key authentication */
  152. auth = authenticate_user(session[session_count]);
  153. if(!auth)
  154. {
  155. printf("auth error: %s\n",ssh_get_error(session[session_count]));
  156. ssh_disconnect(session[session_count]);
  157. return 4;
  158. }
  159. // server_loop(session[session_count]);
  160. g_thread_create(server_thread,session[session_count],FALSE,NULL);
  161. session_count++;
  162. }
  163.  
  164. /* if(x11session.x11_auth_protocol != NULL)
  165. free(x11session.x11_auth_protocol);
  166. if(x11session.x11_auth_cookie != NULL)
  167. free(x11session.x11_auth_cookie);
  168. */
  169.  
  170. // ssh_disconnect(session[session_count]);
  171. ssh_bind_free(sshbind);
  172. ssh_finalize();
  173.  
  174. return 0;
  175. }
  176.  
  177. /* returns 1 for OK, 0 for KO */
  178. int authenticate_user(ssh_session session)
  179. {
  180. ssh_message message;
  181. ssh_string pubkey = NULL;
  182. char *pk64 = NULL;
  183. int signature_state = SSH_PUBLICKEY_STATE_NONE;
  184.  
  185. do
  186. {
  187. message = ssh_message_get(session);
  188. if(!message)
  189. return 0;
  190.  
  191. switch(ssh_message_type(message))
  192. {
  193. case SSH_REQUEST_AUTH:
  194. switch(ssh_message_subtype(message))
  195. {
  196. case SSH_AUTH_METHOD_PUBLICKEY:
  197. pubkey = publickey_to_string(ssh_message_auth_publickey(message));
  198. pk64 = g_base64_encode((const guchar *)ssh_string_to_char(pubkey), ssh_string_len(pubkey));
  199. signature_state = ssh_message_auth_publickey_state(message);
  200. if(signature_state == SSH_PUBLICKEY_STATE_NONE)
  201. {
  202. /* no signature */
  203. ssh_message_auth_reply_pk_ok_simple(message);
  204. break;
  205. }
  206. else if(signature_state != SSH_PUBLICKEY_STATE_VALID)
  207. {
  208. /* will be rejected later */
  209. }
  210. else
  211. {
  212. /* signature is good at that point */
  213. if(pubkey_auth(pk64))
  214. {
  215. /* user is allowed */
  216. ssh_message_auth_reply_success(message, 0);
  217. ssh_message_free(message);
  218. return 1;
  219. }
  220. }
  221. /* the following is not necessary if we want only pubkey auth */
  222. ssh_message_auth_set_methods(message,SSH_AUTH_METHOD_PUBLICKEY);
  223. /* reject authentication */
  224. ssh_message_reply_default(message);
  225. break;
  226. case SSH_AUTH_METHOD_PASSWORD:
  227. /* handle password auth if needed */
  228. default:
  229. ssh_message_auth_set_methods(message,SSH_AUTH_METHOD_PUBLICKEY);
  230. ssh_message_reply_default(message);
  231. }
  232. break;
  233. default:
  234. ssh_message_reply_default(message);
  235. }
  236. ssh_message_free(message);
  237. }while(1);
  238.  
  239. return 0;
  240. }
  241.  
  242. int pubkey_auth(char* pk64)
  243. {
  244. if(strcmp(pk64,MY_PUB_KEY) == 0)
  245. return 1;
  246.  
  247. return 0;
  248. }
  249.  
  250.  
  251. //int server_loop(ssh_session session)
  252. static gpointer server_thread(gpointer session_data)
  253. {
  254. ssh_message message;
  255. int message_subtype = 0;
  256. int message_type = 0;
  257. int ret = 0;
  258. ssh_buffer buf;
  259. ssh_session session = (ssh_session) session_data;
  260. struct ssh_message_struct *msg;
  261. x11_session x11session;
  262.  
  263. do
  264. {
  265. message=ssh_message_get(session);
  266. msg = message;
  267. if(message)
  268. {
  269. message_type = ssh_message_type(message);
  270. message_subtype = ssh_message_subtype(message);
  271. // printf("\nmessage_type = %d subtype = %d",message_type,message_subtype);
  272. switch(message_type)
  273. {
  274. case SSH_REQUEST_CHANNEL_OPEN:
  275. if(message_subtype == SSH_CHANNEL_SESSION)
  276. {
  277. printf("\nSSH_CHANNEL_SESSION");
  278. chan=ssh_message_channel_request_open_reply_accept(message);
  279. }
  280. break;
  281. case SSH_REQUEST_CHANNEL:
  282. printf("\nSSH_REQUEST_CHANNEL subtype = %d",message_subtype);
  283. if(message_subtype == SSH_CHANNEL_REQUEST_X11)
  284. {
  285. printf("\nSSH_CHANNEL_REQUEST_X11");
  286. if(session_x11_req(session,message,&x11session) != 1)
  287. {
  288. printf("\nsession_x11_req error");
  289. ssh_message_reply_default(message);
  290. ssh_disconnect(session);
  291. return NULL;
  292. }
  293. else
  294. ssh_message_channel_request_reply_success(message);
  295. }
  296. if(message_subtype == SSH_CHANNEL_REQUEST_ENV)
  297. {
  298. printf("\nSSH_CHANNEL_REQUEST_ENV");
  299. ssh_message_channel_request_reply_success(message);
  300. }
  301. if(message_subtype == SSH_CHANNEL_REQUEST_EXEC)
  302. {
  303. printf("\nSSH_CHANNEL_REQUEST_EXEC command = %s\n",ssh_message_channel_request_command(message));
  304. ret = exec_command(ssh_message_channel_request_command(message),&x11session);
  305. if(ret == 0)
  306. {
  307. printf("\nserver_loop: unable to exec command\n");
  308. }
  309. ssh_message_channel_request_reply_success(message);
  310. }
  311. break;
  312. default:
  313. ssh_message_reply_default(message);
  314. }
  315. ssh_message_free(message);
  316. }
  317. } while(1);
  318.  
  319. // return 1;
  320. return NULL;
  321. }
  322.  
  323.  
  324. int session_x11_req(ssh_session session,ssh_message message,x11_session* x11session)
  325. {
  326. int ret = 1;
  327. FILE* fpxauth;
  328. char xauth_path[] = "/usr/bin/xauth";
  329. char strxauth_exec[200]; //jeetu - buffer size sufficient?; xauth path name may be larger; ideally not fixed
  330.  
  331. x11session->x11_auth_protocol = NULL;
  332. x11session->x11_auth_cookie = NULL;
  333.  
  334. x11session->x11_auth_protocol = malloc(MAX_X11_AUTH_PROTO_STR_SZ+2);
  335. strncpy(x11session->x11_auth_protocol,ssh_message_channel_request_x11_auth_protocol(message),MAX_X11_AUTH_PROTO_STR_SZ+1);
  336. x11session->x11_auth_protocol[MAX_X11_AUTH_PROTO_STR_SZ] = '\0';
  337. if(strncmp(x11session->x11_auth_protocol,"MIT-MAGIC-COOKIE-1",MAX_X11_AUTH_PROTO_STR_SZ+1) == 0)
  338. {
  339. x11session->x11_auth_cookie = malloc(MAX_X11_AUTH_COOKIE_STR_SZ+2);
  340. strncpy(x11session->x11_auth_cookie,ssh_message_channel_request_x11_auth_cookie(message),MAX_X11_AUTH_COOKIE_STR_SZ+1);
  341. x11session->x11_auth_cookie[MAX_X11_AUTH_COOKIE_STR_SZ] = '\0';
  342. x11session->screen_number = ssh_message_channel_request_x11_screen_number(message);
  343. x11session->single_connection = ssh_message_channel_request_x11_single_connection(message);
  344. }
  345. else
  346. return 0;
  347. // ret = execl("/usr/bin/xauth","/usr/bin/xauth","add","unix:10.0",x11session.x11_auth_protocol,x11session.x11_auth_cookie,(char *) NULL);
  348.  
  349. ret = session_setup_x11fwd(session,x11session);
  350. if(ret == 0)
  351. {
  352. printf("\nsession_setup_x11fwd failed");
  353. return 0;
  354. }
  355.  
  356. printf("\nx11_auth_protocol=%s\nx11_auth_cookie=%s\nscreen_number = %d\nsingle_connection = %d\ndisplay_number = %d\n",x11session->x11_auth_protocol,x11session->x11_auth_cookie,x11session->screen_number,x11session->single_connection,x11session->display_number);
  357.  
  358. snprintf(strxauth_exec,199,"%s remove :%d",xauth_path,x11session->display_number);
  359. printf("\nstrxauth_exec = %s",strxauth_exec);
  360.  
  361. fpxauth = popen(strxauth_exec,"r");
  362. if(fpxauth == NULL)
  363. return 0;
  364. pclose(fpxauth);
  365.  
  366. strxauth_exec[0] = '\0';
  367. snprintf(strxauth_exec,199,"%s add unix:%d %s %s",xauth_path,x11session->display_number,x11session->x11_auth_protocol,x11session->x11_auth_cookie);
  368. // snprintf(strxauth_exec,199,"%s add unix:10 %s %s",xauth_path,x11session.x11_auth_protocol,x11session.x11_auth_cookie);
  369. printf("\nstrxauth_exec = %s",strxauth_exec);
  370.  
  371. fpxauth = popen(strxauth_exec,"r");
  372. if(fpxauth == NULL)
  373. return 0;
  374. pclose(fpxauth);
  375.  
  376. return ret;
  377. }
  378.  
  379.  
  380.  
  381. int session_setup_x11fwd(ssh_session session,x11_session* x11session)
  382. {
  383. int ret = 1;
  384. WaitThreadData *waitdata;
  385. int *sockets; //jeetu - sufficient array size?
  386.  
  387. sockets = malloc( (sizeof(int) * 10) );
  388.  
  389. ret = x11_create_display_inet(session,&x11session->display_number,sockets);
  390. if(ret == 0)
  391. {
  392. printf("\nx11_create_display_inet failed");
  393. return 0;
  394. }
  395.  
  396. printf("\nsockets[0] = %d session = %d",sockets[0],session);
  397. waitdata = thread_data_new(); //jeetu - memory needs to be freed
  398. waitdata->session = session;
  399. waitdata->sockets = sockets;
  400. g_thread_create(wait_for_something,waitdata,FALSE,NULL);
  401.  
  402. //free(sockets);
  403. return ret;
  404. }
  405.  
  406. int x11_create_display_inet(ssh_session session,unsigned int *display_numberp, int *sockets)
  407. {
  408. int ret = 1;
  409. int display_num = 0,sock = 0,num_socks = 0;
  410. unsigned int port = 0;
  411. struct addrinfo hints, *ai, *aitop;
  412. char strport[NI_MAXSERV];
  413. int gaierr,n,socks[NUM_SOCKS];
  414. static int x11_display_offset = 10; //jeetu - temporarily hardcoded here
  415. ssh_channel nc;
  416.  
  417. printf("\nx11_create_display_inet: x11_display_offset = %d\n",x11_display_offset);
  418. for(display_num = x11_display_offset; display_num < MAX_DISPLAYS; display_num++)
  419. {
  420. port = 6000 + display_num;
  421. hints.ai_family = AF_INET;
  422. hints.ai_flags = AI_NUMERICSERV | AI_PASSIVE;
  423. hints.ai_socktype = SOCK_STREAM;
  424. hints.ai_protocol = 0;
  425. snprintf(strport, sizeof strport, "%d", port);
  426. if((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0)
  427. {
  428. printf("\ngetaddrinfo: %s",gai_strerror(gaierr));
  429. return 0;
  430. }
  431.  
  432. for(ai = aitop; ai; ai = ai->ai_next)
  433. {
  434. if(ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
  435. continue;
  436. sock = socket(ai->ai_family, ai->ai_socktype,ai->ai_protocol);
  437. if(sock < 0)
  438. {
  439. if((errno != EINVAL) && (errno != EAFNOSUPPORT))
  440. {
  441. printf("\nsocket error: %s", strerror(errno));
  442. freeaddrinfo(aitop);
  443. return 0;
  444. }
  445. else
  446. {
  447. printf("\nx11_create_display_inet: Socket family %d not supported",ai->ai_family);
  448. continue;
  449. }
  450. }
  451. // if(ai->ai_family == AF_INET6)
  452. // sock_set_v6only(sock);
  453. // if(x11_use_localhost)
  454. // channel_set_reuseaddr(sock);
  455. if(bind(sock, ai->ai_addr, ai->ai_addrlen) < 0)
  456. {
  457. printf("bind port %d: %s", port, strerror(errno));
  458. close(sock);
  459. for(n = 0; n < num_socks; n++)
  460. {
  461. close(socks[n]);
  462. }
  463. num_socks = 0;
  464. break;
  465. }
  466. socks[num_socks++] = sock;
  467. if(num_socks == NUM_SOCKS)
  468. break;
  469. }
  470. freeaddrinfo(aitop);
  471. if(num_socks > 0)
  472. break;
  473. }
  474.  
  475. if(display_num >= MAX_DISPLAYS)
  476. {
  477. printf("\nFailed to allocate internet-domain X11 display socket.");
  478. return 0;
  479. }
  480.  
  481. /* Start listening for connections on the socket. */
  482. for(n = 0; n < num_socks; n++)
  483. {
  484. sock = socks[n];
  485. *(sockets+n) = sock;
  486. if(listen(sock, SSH_LISTEN_BACKLOG) < 0)
  487. {
  488. printf("\nlisten: %s", strerror(errno));
  489. close(sock);
  490. return 0;
  491. }
  492. }
  493.  
  494. *display_numberp = display_num;
  495. x11_display_offset++;
  496. return ret;
  497. }
  498.  
  499. static gpointer wait_for_something(gpointer user_data)
  500. {
  501. int ret = 1;
  502. fd_set infds, testfds;
  503. struct timeval tv = { 15, 0 };
  504. int maxfds = 0;
  505. int nready;
  506. int client_sock, cli_len;
  507. struct sockaddr_in cli_addr, serv_addr;
  508. ssh_event event;
  509. short events;
  510. ssh_channel chan_x11=0;
  511. WaitThreadData *data;
  512. struct ssh_channel_callbacks_struct cb =
  513. {
  514. .channel_data_function = copy_chan_to_fd,
  515. .channel_eof_function = chan_close,
  516. .channel_close_function = chan_close,
  517. .userdata = NULL
  518. };
  519.  
  520. data = user_data;
  521.  
  522. FD_ZERO(&infds);
  523. FD_SET(data->sockets[0], &infds);
  524. printf("\ndata->sockets[0] = %d",data->sockets[0]);
  525. maxfds = data->sockets[0];
  526. while(1)
  527. {
  528. testfds = infds;
  529. printf("\nwait_for_something: before select");
  530. tv.tv_sec = 15;
  531. nready = select(maxfds + 1, &testfds, NULL, NULL, &tv);
  532. if(nready == -1)
  533. {
  534. printf("\nselect error: %s\n",strerror(errno));
  535. }
  536. if(nready > 0)
  537. {
  538. printf("\nwait_for_something: nready > 0");
  539. if(FD_ISSET(data->sockets[0], &testfds))
  540. {
  541. printf("\nFD_ISSET\n");
  542. cli_len = sizeof (cli_addr);
  543. bzero((char *) &cli_addr, sizeof (cli_addr));
  544. client_sock = accept(data->sockets[0], (struct sockaddr *) &cli_addr, (socklen_t *) &cli_len);
  545. printf("\nclient_sock = %d",client_sock);
  546. chan_x11 = ssh_channel_new(data->session);
  547. printf("\nchan_x11 session = %d\n",data->session);
  548. if(ssh_channel_open_x11(chan_x11,"127.0.0.1",6010) == SSH_ERROR)
  549. {
  550. printf("ssh_channel_open_x11 error : %s\n",ssh_get_error(chan_x11));
  551. return NULL;
  552. }
  553. else
  554. printf("\nssh_channel_open_x11\n");
  555.  
  556. cb.userdata = &client_sock;
  557. ssh_callbacks_init(&cb);
  558. ssh_set_channel_callbacks(chan_x11, &cb);
  559. events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL;
  560. event = ssh_event_new();
  561. if(event == NULL)
  562. {
  563. printf("Couldn't get a event\n");
  564. return NULL;
  565. }
  566. else
  567. printf("\nevent != NULL");
  568.  
  569. if(ssh_event_add_fd(event, client_sock, events, copy_fd_to_chan, chan_x11) != SSH_OK)
  570. {
  571. printf("Couldn't add an fd to the event\n");
  572. return NULL;
  573. }
  574. else
  575. printf("\nAdded fd to event");
  576.  
  577. if(ssh_event_add_session(event, data->session) != SSH_OK)
  578. {
  579. printf("Couldn't add the session to the event\n");
  580. return NULL;
  581. }
  582. else
  583. printf("\nadded the session to the event\n");
  584.  
  585. do {
  586. // g_mutex_lock(mutex_for_dopoll);
  587. ssh_event_dopoll(event, 1000);
  588. // g_mutex_unlock(mutex_for_dopoll);
  589. } while(!ssh_channel_is_closed(chan_x11));
  590. printf("\nssh_channel_open_x11: channel closed\n");
  591. /* ssh_event_remove_fd(event, client_sock);
  592. ssh_event_remove_session(event, data->session);
  593. ssh_event_free(event);*/
  594. break;
  595. }
  596. }
  597. }
  598. printf("\nexiting wait_for_something");
  599. return NULL;
  600. }
  601.  
  602. static int copy_fd_to_chan(socket_t fd, int revents, void *userdata)
  603. {
  604. ssh_channel chan = (ssh_channel)userdata;
  605. char buf[4000];
  606. int sz = 0;
  607.  
  608. if(!chan) {
  609. close(fd);
  610. return -1;
  611. }
  612. if(revents & POLLIN) {
  613. // g_mutex_lock(mutex_for_copy_fd_to_chan);
  614. sz = read(fd, buf, 4000);
  615. // g_mutex_unlock(mutex_for_copy_fd_to_chan);
  616. if(sz == 0)
  617. {
  618. printf("\ncopy_fd_to_chan: sz == 0\n");
  619. ssh_channel_close(chan);
  620. sz = -1;
  621. }
  622. if(sz > 0) {
  623. // g_mutex_lock(mutex_for_copy_fd_to_chan);
  624. ssh_channel_write(chan, buf, sz);
  625. // g_mutex_unlock(mutex_for_copy_fd_to_chan);
  626. }
  627. }
  628. if(revents & POLLHUP) {
  629. ssh_channel_close(chan);
  630. sz = -1;
  631. }
  632. return sz;
  633. }
  634.  
  635.  
  636. static int copy_chan_to_fd(ssh_session session,
  637. ssh_channel channel,
  638. void *data,
  639. uint32_t len,
  640. int is_stderr,
  641. void *userdata)
  642. {
  643. int fd = *(int*)userdata;
  644. int sz;
  645. (void)session;
  646. (void)channel;
  647. (void)is_stderr;
  648.  
  649. sz = write(fd, data, len);
  650. return sz;
  651. }
  652.  
  653. static void chan_close(ssh_session session, ssh_channel channel, void *userdata)
  654. {
  655. int fd = *(int*)userdata;
  656. (void)session;
  657. (void)channel;
  658.  
  659. close(fd);
  660. }
  661.  
  662.  
  663. static WaitThreadData * thread_data_new (void)
  664. {
  665. WaitThreadData *data;
  666.  
  667. data = g_new0 (WaitThreadData, 1);
  668.  
  669. return data;
  670. }
  671.  
  672.  
  673. int exec_command(const char *command,x11_session* x11session)
  674. {
  675. FILE *fpcmd;
  676. char str_exec[200]; //jeetu - buffer size sufficient?; command name may be larger; ideally not fixed
  677.  
  678. printf("\nexec_command: x11session->display_number = %d\n",x11session->display_number);
  679. str_exec[0] = '\0';
  680. snprintf(str_exec,199,"%s -display :%d",command,x11session->display_number);
  681. fpcmd = popen(str_exec,"r");
  682. if(fpcmd == NULL)
  683. return 0;
  684.  
  685. return 1;
  686. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement