geom_gate userland utility improvements
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

254 lines
6.7 KiB

  1. /*
  2. * Sample showing how to do SSH2 connect using ssh-agent.
  3. *
  4. * The sample code has default values for host name, user name:
  5. *
  6. * "ssh2_agent host user"
  7. */
  8. #include "libssh2_config.h"
  9. #include <libssh2.h>
  10. #include <libssh2_sftp.h>
  11. #ifdef HAVE_WINDOWS_H
  12. # include <windows.h>
  13. #endif
  14. #ifdef HAVE_WINSOCK2_H
  15. # include <winsock2.h>
  16. #endif
  17. #ifdef HAVE_SYS_SOCKET_H
  18. # include <sys/socket.h>
  19. #endif
  20. #ifdef HAVE_NETINET_IN_H
  21. # include <netinet/in.h>
  22. #endif
  23. # ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. # ifdef HAVE_ARPA_INET_H
  27. #include <arpa/inet.h>
  28. #endif
  29. #include <sys/types.h>
  30. #include <fcntl.h>
  31. #include <errno.h>
  32. #include <stdio.h>
  33. #include <ctype.h>
  34. #include <stdlib.h>
  35. const char *username = "username";
  36. int main(int argc, char *argv[])
  37. {
  38. unsigned long hostaddr;
  39. int sock = -1, i, rc;
  40. struct sockaddr_in sin;
  41. const char *fingerprint;
  42. char *userauthlist;
  43. LIBSSH2_SESSION *session = NULL;
  44. LIBSSH2_CHANNEL *channel;
  45. LIBSSH2_AGENT *agent = NULL;
  46. struct libssh2_agent_publickey *identity, *prev_identity = NULL;
  47. #ifdef WIN32
  48. WSADATA wsadata;
  49. int err;
  50. err = WSAStartup(MAKEWORD(2, 0), &wsadata);
  51. if(err != 0) {
  52. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  53. return 1;
  54. }
  55. #endif
  56. if(argc > 1) {
  57. hostaddr = inet_addr(argv[1]);
  58. }
  59. else {
  60. hostaddr = htonl(0x7F000001);
  61. }
  62. if(argc > 2) {
  63. username = argv[2];
  64. }
  65. rc = libssh2_init(0);
  66. if(rc != 0) {
  67. fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  68. return 1;
  69. }
  70. /* Ultra basic "connect to port 22 on localhost". Your code is
  71. * responsible for creating the socket establishing the connection
  72. */
  73. sock = socket(AF_INET, SOCK_STREAM, 0);
  74. if(sock == -1) {
  75. fprintf(stderr, "failed to create socket!\n");
  76. rc = 1;
  77. goto shutdown;
  78. }
  79. sin.sin_family = AF_INET;
  80. sin.sin_port = htons(22);
  81. sin.sin_addr.s_addr = hostaddr;
  82. if(connect(sock, (struct sockaddr*)(&sin),
  83. sizeof(struct sockaddr_in)) != 0) {
  84. fprintf(stderr, "failed to connect!\n");
  85. goto shutdown;
  86. }
  87. /* Create a session instance and start it up. This will trade welcome
  88. * banners, exchange keys, and setup crypto, compression, and MAC layers
  89. */
  90. session = libssh2_session_init();
  91. if(libssh2_session_handshake(session, sock)) {
  92. fprintf(stderr, "Failure establishing SSH session\n");
  93. return 1;
  94. }
  95. /* At this point we havn't authenticated. The first thing to do is check
  96. * the hostkey's fingerprint against our known hosts Your app may have it
  97. * hard coded, may go to a file, may present it to the user, that's your
  98. * call
  99. */
  100. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  101. fprintf(stderr, "Fingerprint: ");
  102. for(i = 0; i < 20; i++) {
  103. fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
  104. }
  105. fprintf(stderr, "\n");
  106. /* check what authentication methods are available */
  107. userauthlist = libssh2_userauth_list(session, username, strlen(username));
  108. fprintf(stderr, "Authentication methods: %s\n", userauthlist);
  109. if(strstr(userauthlist, "publickey") == NULL) {
  110. fprintf(stderr, "\"publickey\" authentication is not supported\n");
  111. goto shutdown;
  112. }
  113. /* Connect to the ssh-agent */
  114. agent = libssh2_agent_init(session);
  115. if(!agent) {
  116. fprintf(stderr, "Failure initializing ssh-agent support\n");
  117. rc = 1;
  118. goto shutdown;
  119. }
  120. if(libssh2_agent_connect(agent)) {
  121. fprintf(stderr, "Failure connecting to ssh-agent\n");
  122. rc = 1;
  123. goto shutdown;
  124. }
  125. if(libssh2_agent_list_identities(agent)) {
  126. fprintf(stderr, "Failure requesting identities to ssh-agent\n");
  127. rc = 1;
  128. goto shutdown;
  129. }
  130. while(1) {
  131. rc = libssh2_agent_get_identity(agent, &identity, prev_identity);
  132. if(rc == 1)
  133. break;
  134. if(rc < 0) {
  135. fprintf(stderr,
  136. "Failure obtaining identity from ssh-agent support\n");
  137. rc = 1;
  138. goto shutdown;
  139. }
  140. if(libssh2_agent_userauth(agent, username, identity)) {
  141. fprintf(stderr, "\tAuthentication with username %s and "
  142. "public key %s failed!\n",
  143. username, identity->comment);
  144. }
  145. else {
  146. fprintf(stderr, "\tAuthentication with username %s and "
  147. "public key %s succeeded!\n",
  148. username, identity->comment);
  149. break;
  150. }
  151. prev_identity = identity;
  152. }
  153. if(rc) {
  154. fprintf(stderr, "Couldn't continue authentication\n");
  155. goto shutdown;
  156. }
  157. /* We're authenticated now. */
  158. /* Request a shell */
  159. channel = libssh2_channel_open_session(session);
  160. if(!channel) {
  161. fprintf(stderr, "Unable to open a session\n");
  162. goto shutdown;
  163. }
  164. /* Some environment variables may be set,
  165. * It's up to the server which ones it'll allow though
  166. */
  167. libssh2_channel_setenv(channel, "FOO", "bar");
  168. /* Request a terminal with 'vanilla' terminal emulation
  169. * See /etc/termcap for more options
  170. */
  171. if(libssh2_channel_request_pty(channel, "vanilla")) {
  172. fprintf(stderr, "Failed requesting pty\n");
  173. goto skip_shell;
  174. }
  175. /* Open a SHELL on that pty */
  176. if(libssh2_channel_shell(channel)) {
  177. fprintf(stderr, "Unable to request shell on allocated pty\n");
  178. goto shutdown;
  179. }
  180. /* At this point the shell can be interacted with using
  181. * libssh2_channel_read()
  182. * libssh2_channel_read_stderr()
  183. * libssh2_channel_write()
  184. * libssh2_channel_write_stderr()
  185. *
  186. * Blocking mode may be (en|dis)abled with: libssh2_channel_set_blocking()
  187. * If the server send EOF, libssh2_channel_eof() will return non-0
  188. * To send EOF to the server use: libssh2_channel_send_eof()
  189. * A channel can be closed with: libssh2_channel_close()
  190. * A channel can be freed with: libssh2_channel_free()
  191. */
  192. skip_shell:
  193. if(channel) {
  194. libssh2_channel_free(channel);
  195. channel = NULL;
  196. }
  197. /* Other channel types are supported via:
  198. * libssh2_scp_send()
  199. * libssh2_scp_recv2()
  200. * libssh2_channel_direct_tcpip()
  201. */
  202. shutdown:
  203. if(agent) {
  204. libssh2_agent_disconnect(agent);
  205. libssh2_agent_free(agent);
  206. }
  207. if(session) {
  208. libssh2_session_disconnect(session,
  209. "Normal Shutdown, Thank you for playing");
  210. libssh2_session_free(session);
  211. }
  212. if(sock != -1) {
  213. #ifdef WIN32
  214. closesocket(sock);
  215. #else
  216. close(sock);
  217. #endif
  218. }
  219. fprintf(stderr, "all done!\n");
  220. libssh2_exit();
  221. return rc;
  222. }