ssh2.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Self test, based on examples/ssh2.c. */
  2. #include "libssh2_config.h"
  3. #include <libssh2.h>
  4. #include <libssh2_sftp.h>
  5. #ifdef HAVE_WINDOWS_H
  6. # include <windows.h>
  7. #endif
  8. #ifdef HAVE_WINSOCK2_H
  9. # include <winsock2.h>
  10. #endif
  11. #ifdef HAVE_SYS_SOCKET_H
  12. # include <sys/socket.h>
  13. #endif
  14. #ifdef HAVE_NETINET_IN_H
  15. # include <netinet/in.h>
  16. #endif
  17. # ifdef HAVE_UNISTD_H
  18. #include <unistd.h>
  19. #endif
  20. # ifdef HAVE_ARPA_INET_H
  21. #include <arpa/inet.h>
  22. #endif
  23. #include <sys/types.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <ctype.h>
  29. int main(int argc, char *argv[])
  30. {
  31. unsigned long hostaddr;
  32. int sock, i, auth_pw = 0;
  33. struct sockaddr_in sin;
  34. const char *fingerprint;
  35. char *userauthlist;
  36. LIBSSH2_SESSION *session;
  37. LIBSSH2_CHANNEL *channel;
  38. const char *pubkeyfile="etc/user.pub";
  39. const char *privkeyfile="etc/user";
  40. const char *username="username";
  41. const char *password="password";
  42. int ec = 1;
  43. #ifdef WIN32
  44. WSADATA wsadata;
  45. int err;
  46. err = WSAStartup(MAKEWORD(2,0), &wsadata);
  47. if (err != 0) {
  48. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  49. return -1;
  50. }
  51. #endif
  52. (void)argc;
  53. (void)argv;
  54. if (getenv ("USER"))
  55. username = getenv ("USER");
  56. if (getenv ("PRIVKEY"))
  57. privkeyfile = getenv ("PRIVKEY");
  58. if (getenv ("PUBKEY"))
  59. pubkeyfile = getenv ("PUBKEY");
  60. hostaddr = htonl(0x7F000001);
  61. sock = socket(AF_INET, SOCK_STREAM, 0);
  62. #ifndef WIN32
  63. fcntl(sock, F_SETFL, 0);
  64. #endif
  65. sin.sin_family = AF_INET;
  66. sin.sin_port = htons(4711);
  67. sin.sin_addr.s_addr = hostaddr;
  68. if (connect(sock, (struct sockaddr*)(&sin),
  69. sizeof(struct sockaddr_in)) != 0) {
  70. fprintf(stderr, "failed to connect!\n");
  71. return 1;
  72. }
  73. /* Create a session instance and start it up
  74. * This will trade welcome banners, exchange keys, and setup crypto, compression, and MAC layers
  75. */
  76. session = libssh2_session_init();
  77. if (libssh2_session_startup(session, sock)) {
  78. fprintf(stderr, "Failure establishing SSH session\n");
  79. return 1;
  80. }
  81. /* At this point we havn't authenticated,
  82. * The first thing to do is check the hostkey's fingerprint against our known hosts
  83. * Your app may have it hard coded, may go to a file, may present it to the user, that's your call
  84. */
  85. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  86. printf("Fingerprint: ");
  87. for(i = 0; i < 20; i++) {
  88. printf("%02X ", (unsigned char)fingerprint[i]);
  89. }
  90. printf("\n");
  91. /* check what authentication methods are available */
  92. userauthlist = libssh2_userauth_list(session, username, strlen(username));
  93. printf("Authentication methods: %s\n", userauthlist);
  94. if (strstr(userauthlist, "password") != NULL) {
  95. auth_pw |= 1;
  96. }
  97. if (strstr(userauthlist, "keyboard-interactive") != NULL) {
  98. auth_pw |= 2;
  99. }
  100. if (strstr(userauthlist, "publickey") != NULL) {
  101. auth_pw |= 4;
  102. }
  103. if (auth_pw & 4) {
  104. /* Authenticate by public key */
  105. if (libssh2_userauth_publickey_fromfile(session, username, pubkeyfile, privkeyfile, password)) {
  106. printf("\tAuthentication by public key failed!\n");
  107. goto shutdown;
  108. } else {
  109. printf("\tAuthentication by public key succeeded.\n");
  110. }
  111. } else {
  112. printf("No supported authentication methods found!\n");
  113. goto shutdown;
  114. }
  115. /* Request a shell */
  116. if (!(channel = libssh2_channel_open_session(session))) {
  117. fprintf(stderr, "Unable to open a session\n");
  118. goto shutdown;
  119. }
  120. /* Some environment variables may be set,
  121. * It's up to the server which ones it'll allow though
  122. */
  123. libssh2_channel_setenv(channel, "FOO", "bar");
  124. /* Request a terminal with 'vanilla' terminal emulation
  125. * See /etc/termcap for more options
  126. */
  127. if (libssh2_channel_request_pty(channel, "vanilla")) {
  128. fprintf(stderr, "Failed requesting pty\n");
  129. goto skip_shell;
  130. }
  131. /* Open a SHELL on that pty */
  132. if (libssh2_channel_shell(channel)) {
  133. fprintf(stderr, "Unable to request shell on allocated pty\n");
  134. goto shutdown;
  135. }
  136. ec = 0;
  137. skip_shell:
  138. if (channel) {
  139. libssh2_channel_free(channel);
  140. channel = NULL;
  141. }
  142. shutdown:
  143. libssh2_session_disconnect(session, "Normal Shutdown");
  144. libssh2_session_free(session);
  145. #ifdef WIN32
  146. Sleep(1000);
  147. closesocket(sock);
  148. #else
  149. sleep(1);
  150. close(sock);
  151. #endif
  152. return ec;
  153. }