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.
 
 
 
 

187 lines
4.6 KiB

  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,
  75. * and setup crypto, compression, and MAC layers
  76. */
  77. session = libssh2_session_init();
  78. if(libssh2_session_startup(session, sock)) {
  79. fprintf(stderr, "Failure establishing SSH session\n");
  80. return 1;
  81. }
  82. /* At this point we haven't authenticated,
  83. * The first thing to do is check the hostkey's
  84. * fingerprint against our known hosts
  85. * Your app may have it hard coded, may go to a file,
  86. * may present it to the user, that's your call
  87. */
  88. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  89. printf("Fingerprint: ");
  90. for(i = 0; i < 20; i++) {
  91. printf("%02X ", (unsigned char)fingerprint[i]);
  92. }
  93. printf("\n");
  94. /* check what authentication methods are available */
  95. userauthlist = libssh2_userauth_list(session, username, strlen(username));
  96. printf("Authentication methods: %s\n", userauthlist);
  97. if(strstr(userauthlist, "password") != NULL) {
  98. auth_pw |= 1;
  99. }
  100. if(strstr(userauthlist, "keyboard-interactive") != NULL) {
  101. auth_pw |= 2;
  102. }
  103. if(strstr(userauthlist, "publickey") != NULL) {
  104. auth_pw |= 4;
  105. }
  106. if(auth_pw & 4) {
  107. /* Authenticate by public key */
  108. if(libssh2_userauth_publickey_fromfile(session, username, pubkeyfile,
  109. privkeyfile, password)) {
  110. printf("\tAuthentication by public key failed!\n");
  111. goto shutdown;
  112. }
  113. else {
  114. printf("\tAuthentication by public key succeeded.\n");
  115. }
  116. }
  117. else {
  118. printf("No supported authentication methods found!\n");
  119. goto shutdown;
  120. }
  121. /* Request a shell */
  122. channel = libssh2_channel_open_session(session);
  123. if(!channel) {
  124. fprintf(stderr, "Unable to open a session\n");
  125. goto shutdown;
  126. }
  127. /* Some environment variables may be set,
  128. * It's up to the server which ones it'll allow though
  129. */
  130. libssh2_channel_setenv(channel, "FOO", "bar");
  131. /* Request a terminal with 'vanilla' terminal emulation
  132. * See /etc/termcap for more options
  133. */
  134. if(libssh2_channel_request_pty(channel, "vanilla")) {
  135. fprintf(stderr, "Failed requesting pty\n");
  136. goto skip_shell;
  137. }
  138. /* Open a SHELL on that pty */
  139. if(libssh2_channel_shell(channel)) {
  140. fprintf(stderr, "Unable to request shell on allocated pty\n");
  141. goto shutdown;
  142. }
  143. ec = 0;
  144. skip_shell:
  145. if(channel) {
  146. libssh2_channel_free(channel);
  147. channel = NULL;
  148. }
  149. shutdown:
  150. libssh2_session_disconnect(session, "Normal Shutdown");
  151. libssh2_session_free(session);
  152. #ifdef WIN32
  153. Sleep(1000);
  154. closesocket(sock);
  155. #else
  156. sleep(1);
  157. close(sock);
  158. #endif
  159. return ec;
  160. }