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.
 
 
 
 

191 lines
4.6 KiB

  1. /*
  2. * Sample showing how to do a simple SCP transfer.
  3. */
  4. #include "libssh2_config.h"
  5. #include <libssh2.h>
  6. #ifdef HAVE_WINSOCK2_H
  7. # include <winsock2.h>
  8. #endif
  9. #ifdef HAVE_SYS_SOCKET_H
  10. # include <sys/socket.h>
  11. #endif
  12. #ifdef HAVE_NETINET_IN_H
  13. # include <netinet/in.h>
  14. #endif
  15. # ifdef HAVE_UNISTD_H
  16. #include <unistd.h>
  17. #endif
  18. #ifdef HAVE_ARPA_INET_H
  19. # include <arpa/inet.h>
  20. #endif
  21. #ifdef HAVE_SYS_TIME_H
  22. # include <sys/time.h>
  23. #endif
  24. #include <sys/types.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. int main(int argc, char *argv[])
  30. {
  31. unsigned long hostaddr;
  32. int sock, i, auth_pw = 1;
  33. struct sockaddr_in sin;
  34. const char *fingerprint;
  35. LIBSSH2_SESSION *session;
  36. LIBSSH2_CHANNEL *channel;
  37. const char *username = "username";
  38. const char *password = "password";
  39. const char *scppath = "/tmp/TEST";
  40. libssh2_struct_stat fileinfo;
  41. int rc;
  42. libssh2_struct_stat_size got = 0;
  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. if(argc > 1) {
  53. hostaddr = inet_addr(argv[1]);
  54. }
  55. else {
  56. hostaddr = htonl(0x7F000001);
  57. }
  58. if(argc > 2) {
  59. username = argv[2];
  60. }
  61. if(argc > 3) {
  62. password = argv[3];
  63. }
  64. if(argc > 4) {
  65. scppath = argv[4];
  66. }
  67. rc = libssh2_init(0);
  68. if(rc) {
  69. fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  70. return 1;
  71. }
  72. /* Ultra basic "connect to port 22 on localhost"
  73. * Your code is responsible for creating the socket establishing the
  74. * connection
  75. */
  76. sock = socket(AF_INET, SOCK_STREAM, 0);
  77. sin.sin_family = AF_INET;
  78. sin.sin_port = htons(22);
  79. sin.sin_addr.s_addr = hostaddr;
  80. if(connect(sock, (struct sockaddr*)(&sin),
  81. sizeof(struct sockaddr_in)) != 0) {
  82. fprintf(stderr, "failed to connect!\n");
  83. return -1;
  84. }
  85. /* Create a session instance
  86. */
  87. session = libssh2_session_init();
  88. if(!session)
  89. return -1;
  90. /* ... start it up. This will trade welcome banners, exchange keys,
  91. * and setup crypto, compression, and MAC layers
  92. */
  93. rc = libssh2_session_handshake(session, sock);
  94. if(rc) {
  95. fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
  96. return -1;
  97. }
  98. /* At this point we havn't yet authenticated. The first thing to do
  99. * is check the hostkey's fingerprint against our known hosts Your app
  100. * may have it hard coded, may go to a file, may present it to the
  101. * user, that's your call
  102. */
  103. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  104. fprintf(stderr, "Fingerprint: ");
  105. for(i = 0; i < 20; i++) {
  106. fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
  107. }
  108. fprintf(stderr, "\n");
  109. if(auth_pw) {
  110. /* We could authenticate via password */
  111. if(libssh2_userauth_password(session, username, password)) {
  112. fprintf(stderr, "Authentication by password failed.\n");
  113. goto shutdown;
  114. }
  115. }
  116. else {
  117. /* Or by public key */
  118. #define HOME_DIR "/home/username/"
  119. if(libssh2_userauth_publickey_fromfile(session, username,
  120. HOME_DIR ".ssh/id_rsa.pub",
  121. HOME_DIR ".ssh/id_rsa",
  122. password)) {
  123. fprintf(stderr, "\tAuthentication by public key failed\n");
  124. goto shutdown;
  125. }
  126. }
  127. /* Request a file via SCP */
  128. channel = libssh2_scp_recv2(session, scppath, &fileinfo);
  129. if(!channel) {
  130. fprintf(stderr, "Unable to open a session: %d\n",
  131. libssh2_session_last_errno(session));
  132. goto shutdown;
  133. }
  134. while(got < fileinfo.st_size) {
  135. char mem[1024];
  136. int amount = sizeof(mem);
  137. if((fileinfo.st_size -got) < amount) {
  138. amount = (int)(fileinfo.st_size -got);
  139. }
  140. rc = libssh2_channel_read(channel, mem, amount);
  141. if(rc > 0) {
  142. write(1, mem, rc);
  143. }
  144. else if(rc < 0) {
  145. fprintf(stderr, "libssh2_channel_read() failed: %d\n", rc);
  146. break;
  147. }
  148. got += rc;
  149. }
  150. libssh2_channel_free(channel);
  151. channel = NULL;
  152. shutdown:
  153. libssh2_session_disconnect(session,
  154. "Normal Shutdown, Thank you for playing");
  155. libssh2_session_free(session);
  156. #ifdef WIN32
  157. closesocket(sock);
  158. #else
  159. close(sock);
  160. #endif
  161. fprintf(stderr, "all done\n");
  162. libssh2_exit();
  163. return 0;
  164. }