1 /*
2 *
3 * Sample showing how to makes SSH2 with X11 Forwarding works.
4 *
5 * Usage :
6 * "ssh2 host user password [DEBUG]"
7 */
8
9 #include <string.h>
10 #include <sys/ioctl.h>
11 #include <netinet/in.h>
12 #include <sys/socket.h>
13 #include <sys/select.h>
14 #include <arpa/inet.h>
15 #include <unistd.h>
16 #include <sys/types.h>
17 #include <sys/un.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <termios.h>
24
25 #include <libssh2.h>
26
27 #define _PATH_UNIX_X "/tmp/.X11-unix/X%d"
28
29 /*
30 * Chained list that contains channels and associated X11 socket for each X11
31 * connections
32 */
33 struct chan_X11_list {
34 LIBSSH2_CHANNEL *chan;
35 int sock;
36 struct chan_X11_list *next;
37 };
38
39 struct chan_X11_list * gp_x11_chan = NULL;
40 struct termios _saved_tio;
41
42 /*
43 * Utility function to remove a Node of the chained list
44 */
remove_node(struct chan_X11_list * elem)45 static void remove_node(struct chan_X11_list *elem)
46 {
47 struct chan_X11_list *current_node = NULL;
48
49 current_node = gp_x11_chan;
50
51 if(gp_x11_chan == elem) {
52 gp_x11_chan = gp_x11_chan->next;
53 free(current_node);
54 return;
55 }
56
57 while(current_node->next != NULL) {
58 if(current_node->next == elem) {
59 current_node->next = current_node->next->next;
60 current_node = current_node->next;
61 free(current_node);
62 break;
63 }
64 }
65 }
66
67
session_shutdown(LIBSSH2_SESSION * session)68 static void session_shutdown(LIBSSH2_SESSION *session)
69 {
70 libssh2_session_disconnect(session,
71 "Session Shutdown, Thank you for playing");
72 libssh2_session_free(session);
73 }
74
_raw_mode(void)75 static int _raw_mode(void)
76 {
77 int rc;
78 struct termios tio;
79
80 rc = tcgetattr(fileno(stdin), &tio);
81 if(rc != -1) {
82 _saved_tio = tio;
83 /* do the equivalent of cfmakeraw() manually, to build on Solaris */
84 tio.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
85 tio.c_oflag &= ~OPOST;
86 tio.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
87 tio.c_cflag &= ~(CSIZE|PARENB);
88 tio.c_cflag |= CS8;
89 rc = tcsetattr(fileno(stdin), TCSADRAIN, &tio);
90 }
91 return rc;
92 }
93
_normal_mode(void)94 static int _normal_mode(void)
95 {
96 int rc;
97 rc = tcsetattr(fileno(stdin), TCSADRAIN, &_saved_tio);
98 return rc;
99 }
100
101 /*
102 * CallBack to initialize the forwarding.
103 * Save the channel to loop on it, save the X11 forwarded socket to send
104 * and receive info from our X server.
105 */
x11_callback(LIBSSH2_SESSION * session,LIBSSH2_CHANNEL * channel,char * shost,int sport,void ** abstract)106 static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel,
107 char *shost, int sport, void **abstract)
108 {
109 const char *display = NULL;
110 char *ptr = NULL;
111 char *temp_buff = NULL;
112 int display_port = 0;
113 int sock = 0;
114 int rc = 0;
115 struct sockaddr_un addr;
116 struct chan_X11_list *new;
117 struct chan_X11_list *chan_iter;
118 (void)session;
119 (void)shost;
120 (void)sport;
121 (void)abstract;
122 /*
123 * Connect to the display
124 * Inspired by x11_connect_display in openssh
125 */
126 display = getenv("DISPLAY");
127 if(display != NULL) {
128 if(strncmp(display, "unix:", 5) == 0 ||
129 display[0] == ':') {
130 /* Connect to the local unix domain */
131 ptr = strrchr(display, ':');
132 temp_buff = (char *) calloc(strlen(ptr + 1), sizeof(char));
133 if(!temp_buff) {
134 perror("calloc");
135 return;
136 }
137 memcpy(temp_buff, ptr + 1, strlen(ptr + 1));
138 display_port = atoi(temp_buff);
139 free(temp_buff);
140
141 sock = socket(AF_UNIX, SOCK_STREAM, 0);
142 if(sock < 0)
143 return;
144 memset(&addr, 0, sizeof(addr));
145 addr.sun_family = AF_UNIX;
146 snprintf(addr.sun_path, sizeof(addr.sun_path),
147 _PATH_UNIX_X, display_port);
148 rc = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
149
150 if(rc != -1) {
151 /* Connection Successfull */
152 if(gp_x11_chan == NULL) {
153 /* Calloc ensure that gp_X11_chan is full of 0 */
154 gp_x11_chan = (struct chan_X11_list *)
155 calloc(1, sizeof(struct chan_X11_list));
156 gp_x11_chan->sock = sock;
157 gp_x11_chan->chan = channel;
158 gp_x11_chan->next = NULL;
159 }
160 else {
161 chan_iter = gp_x11_chan;
162 while(chan_iter->next != NULL)
163 chan_iter = chan_iter->next;
164 /* Create the new Node */
165 new = (struct chan_X11_list *)
166 malloc(sizeof(struct chan_X11_list));
167 new->sock = sock;
168 new->chan = channel;
169 new->next = NULL;
170 chan_iter->next = new;
171 }
172 }
173 else
174 close(sock);
175 }
176 }
177 return;
178 }
179
180 /*
181 * Send and receive Data for the X11 channel.
182 * If the connection is closed, returns -1, 0 either.
183 */
x11_send_receive(LIBSSH2_CHANNEL * channel,int sock)184 static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock)
185 {
186 char *buf = NULL;
187 int bufsize = 8192;
188 int rc = 0;
189 int nfds = 1;
190 LIBSSH2_POLLFD *fds = NULL;
191 fd_set set;
192 struct timeval timeval_out;
193 timeval_out.tv_sec = 0;
194 timeval_out.tv_usec = 0;
195
196
197 FD_ZERO(&set);
198 FD_SET(sock, &set);
199
200 buf = calloc(bufsize, sizeof(char));
201 if(!buf)
202 return 0;
203
204 fds = malloc(sizeof (LIBSSH2_POLLFD));
205 if(!fds) {
206 free(buf);
207 return 0;
208 }
209
210 fds[0].type = LIBSSH2_POLLFD_CHANNEL;
211 fds[0].fd.channel = channel;
212 fds[0].events = LIBSSH2_POLLFD_POLLIN;
213 fds[0].revents = LIBSSH2_POLLFD_POLLIN;
214
215 rc = libssh2_poll(fds, nfds, 0);
216 if(rc >0) {
217 rc = libssh2_channel_read(channel, buf, bufsize);
218 write(sock, buf, rc);
219 }
220
221 rc = select(sock + 1, &set, NULL, NULL, &timeval_out);
222 if(rc > 0) {
223 memset((void *)buf, 0, bufsize);
224
225 /* Data in sock*/
226 rc = read(sock, buf, bufsize);
227 if(rc > 0) {
228 libssh2_channel_write(channel, buf, rc);
229 }
230 else {
231 free(buf);
232 return -1;
233 }
234 }
235
236 free(fds);
237 free(buf);
238 if(libssh2_channel_eof(channel) == 1) {
239 return -1;
240 }
241 return 0;
242 }
243
244 /*
245 * Main, more than inspired by ssh2.c by Bagder
246 */
247 int
main(int argc,char * argv[])248 main (int argc, char *argv[])
249 {
250 unsigned long hostaddr = 0;
251 int sock = 0;
252 int rc = 0;
253 struct sockaddr_in sin;
254 LIBSSH2_SESSION *session;
255 LIBSSH2_CHANNEL *channel;
256 char *username = NULL;
257 char *password = NULL;
258 size_t bufsiz = 8193;
259 char *buf = NULL;
260 int set_debug_on = 0;
261 int nfds = 1;
262 LIBSSH2_POLLFD *fds = NULL;
263
264 /* Chan List struct */
265 struct chan_X11_list *current_node = NULL;
266
267 /* Struct winsize for term size */
268 struct winsize w_size;
269 struct winsize w_size_bck;
270
271 /* For select on stdin */
272 fd_set set;
273 struct timeval timeval_out;
274 timeval_out.tv_sec = 0;
275 timeval_out.tv_usec = 10;
276
277
278 if(argc > 3) {
279 hostaddr = inet_addr(argv[1]);
280 username = argv[2];
281 password = argv[3];
282 }
283 else {
284 fprintf(stderr, "Usage: %s destination username password",
285 argv[0]);
286 return -1;
287 }
288
289 if(argc > 4) {
290 set_debug_on = 1;
291 fprintf(stderr, "DEBUG is ON: %d\n", set_debug_on);
292 }
293
294 rc = libssh2_init(0);
295 if(rc != 0) {
296 fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
297 return 1;
298 }
299
300 sock = socket(AF_INET, SOCK_STREAM, 0);
301 if(sock == -1) {
302 perror("socket");
303 return -1;
304 }
305
306 sin.sin_family = AF_INET;
307 sin.sin_port = htons(22);
308 sin.sin_addr.s_addr = hostaddr;
309
310 rc = connect(sock, (struct sockaddr *) &sin,
311 sizeof(struct sockaddr_in));
312 if(rc != 0) {
313 fprintf(stderr, "Failed to established connection!\n");
314 return -1;
315 }
316 /* Open a session */
317 session = libssh2_session_init();
318 rc = libssh2_session_handshake(session, sock);
319 if(rc != 0) {
320 fprintf(stderr, "Failed Start the SSH session\n");
321 return -1;
322 }
323
324 if(set_debug_on == 1)
325 libssh2_trace(session, LIBSSH2_TRACE_CONN);
326
327 /* ignore pedantic warnings by gcc on the callback argument */
328 #pragma GCC diagnostic push
329 #pragma GCC diagnostic ignored "-Wpedantic"
330 /* Set X11 Callback */
331 libssh2_session_callback_set(session, LIBSSH2_CALLBACK_X11,
332 (void *)x11_callback);
333 #pragma GCC diagnostic pop
334
335 /* Authenticate via password */
336 rc = libssh2_userauth_password(session, username, password);
337 if(rc != 0) {
338 fprintf(stderr, "Failed to authenticate\n");
339 session_shutdown(session);
340 close(sock);
341 return -1;
342 }
343
344 /* Open a channel */
345 channel = libssh2_channel_open_session(session);
346 if(channel == NULL) {
347 fprintf(stderr, "Failed to open a new channel\n");
348 session_shutdown(session);
349 close(sock);
350 return -1;
351 }
352
353
354 /* Request a PTY */
355 rc = libssh2_channel_request_pty(channel, "xterm");
356 if(rc != 0) {
357 fprintf(stderr, "Failed to request a pty\n");
358 session_shutdown(session);
359 close(sock);
360 return -1;
361 }
362
363 /* Request X11 */
364 rc = libssh2_channel_x11_req(channel, 0);
365 if(rc != 0) {
366 fprintf(stderr, "Failed to request X11 forwarding\n");
367 session_shutdown(session);
368 close(sock);
369 return -1;
370 }
371
372 /* Request a shell */
373 rc = libssh2_channel_shell(channel);
374 if(rc != 0) {
375 fprintf(stderr, "Failed to open a shell\n");
376 session_shutdown(session);
377 close(sock);
378 return -1;
379 }
380
381 rc = _raw_mode();
382 if(rc != 0) {
383 fprintf(stderr, "Failed to entered in raw mode\n");
384 session_shutdown(session);
385 close(sock);
386 return -1;
387 }
388
389 memset(&w_size, 0, sizeof(struct winsize));
390 memset(&w_size_bck, 0, sizeof(struct winsize));
391
392 while(1) {
393
394 FD_ZERO(&set);
395 FD_SET(fileno(stdin), &set);
396
397 /* Search if a resize pty has to be send */
398 ioctl(fileno(stdin), TIOCGWINSZ, &w_size);
399 if((w_size.ws_row != w_size_bck.ws_row) ||
400 (w_size.ws_col != w_size_bck.ws_col)) {
401 w_size_bck = w_size;
402
403 libssh2_channel_request_pty_size(channel,
404 w_size.ws_col,
405 w_size.ws_row);
406 }
407
408 buf = calloc(bufsiz, sizeof(char));
409 if(buf == NULL)
410 break;
411
412 fds = malloc(sizeof (LIBSSH2_POLLFD));
413 if(fds == NULL) {
414 free(buf);
415 break;
416 }
417
418 fds[0].type = LIBSSH2_POLLFD_CHANNEL;
419 fds[0].fd.channel = channel;
420 fds[0].events = LIBSSH2_POLLFD_POLLIN;
421 fds[0].revents = LIBSSH2_POLLFD_POLLIN;
422
423 rc = libssh2_poll(fds, nfds, 0);
424 if(rc >0) {
425 libssh2_channel_read(channel, buf, sizeof(buf));
426 fprintf(stdout, "%s", buf);
427 fflush(stdout);
428 }
429
430 /* Looping on X clients */
431 if(gp_x11_chan != NULL) {
432 current_node = gp_x11_chan;
433 }
434 else
435 current_node = NULL;
436
437 while(current_node != NULL) {
438 struct chan_X11_list *next_node;
439 rc = x11_send_receive(current_node->chan, current_node->sock);
440 next_node = current_node->next;
441 if(rc == -1) {
442 shutdown(current_node->sock, SHUT_RDWR);
443 close(current_node->sock);
444 remove_node(current_node);
445 }
446
447 current_node = next_node;
448 }
449
450
451 rc = select(fileno(stdin) + 1, &set, NULL, NULL, &timeval_out);
452 if(rc > 0) {
453 /* Data in stdin*/
454 rc = read(fileno(stdin), buf, 1);
455 if(rc > 0)
456 libssh2_channel_write(channel, buf, sizeof(buf));
457 }
458
459 free(fds);
460 free(buf);
461
462 if(libssh2_channel_eof (channel) == 1) {
463 break;
464 }
465 }
466
467 if(channel) {
468 libssh2_channel_free(channel);
469 channel = NULL;
470 }
471 _normal_mode();
472
473 libssh2_exit();
474
475 return 0;
476 }
477