PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/3rdparty/libssh2/libssh2/example/scp_write.c

https://gitlab.com/jeanim/libqxt
C | 222 lines | 168 code | 29 blank | 25 comment | 28 complexity | f2de6c639189e585f71bae70c7932ffe MD5 | raw file
  1. /*
  2. * $Id: scp_write.c,v 1.7 2009/04/28 10:35:30 bagder Exp $
  3. *
  4. * Sample showing how to do a simple SCP transfer.
  5. */
  6. #include "libssh2_config.h"
  7. #include <libssh2.h>
  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. #ifdef HAVE_SYS_TIME_H
  24. # include <sys/time.h>
  25. #endif
  26. #include <sys/types.h>
  27. #include <fcntl.h>
  28. #include <errno.h>
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. int main(int argc, char *argv[])
  32. {
  33. unsigned long hostaddr;
  34. int sock, i, auth_pw = 1;
  35. struct sockaddr_in sin;
  36. const char *fingerprint;
  37. LIBSSH2_SESSION *session = NULL;
  38. LIBSSH2_CHANNEL *channel;
  39. const char *username="username";
  40. const char *password="password";
  41. const char *loclfile="scp_write.c";
  42. const char *scppath="/tmp/TEST";
  43. FILE *local;
  44. int rc;
  45. char mem[1024];
  46. size_t nread;
  47. char *ptr;
  48. struct stat fileinfo;
  49. #ifdef WIN32
  50. WSADATA wsadata;
  51. WSAStartup(MAKEWORD(2,0), &wsadata);
  52. #endif
  53. if (argc > 1) {
  54. hostaddr = inet_addr(argv[1]);
  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. loclfile = argv[4];
  66. }
  67. if (argc > 5) {
  68. scppath = argv[5];
  69. }
  70. rc = libssh2_init (0);
  71. if (rc != 0) {
  72. fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
  73. return 1;
  74. }
  75. local = fopen(loclfile, "rb");
  76. if (!local) {
  77. fprintf(stderr, "Can't open local file %s\n", loclfile);
  78. return -1;
  79. }
  80. stat(loclfile, &fileinfo);
  81. /* Ultra basic "connect to port 22 on localhost"
  82. * Your code is responsible for creating the socket establishing the
  83. * connection
  84. */
  85. sock = socket(AF_INET, SOCK_STREAM, 0);
  86. if(-1 == sock) {
  87. fprintf(stderr, "failed to create socket!\n");
  88. return -1;
  89. }
  90. sin.sin_family = AF_INET;
  91. sin.sin_port = htons(22);
  92. sin.sin_addr.s_addr = hostaddr;
  93. if (connect(sock, (struct sockaddr*)(&sin),
  94. sizeof(struct sockaddr_in)) != 0) {
  95. fprintf(stderr, "failed to connect!\n");
  96. return -1;
  97. }
  98. /* Create a session instance
  99. */
  100. session = libssh2_session_init();
  101. if(!session)
  102. return -1;
  103. /* ... start it up. This will trade welcome banners, exchange keys,
  104. * and setup crypto, compression, and MAC layers
  105. */
  106. rc = libssh2_session_startup(session, sock);
  107. if(rc) {
  108. fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
  109. return -1;
  110. }
  111. /* At this point we havn't yet authenticated. The first thing to do
  112. * is check the hostkey's fingerprint against our known hosts Your app
  113. * may have it hard coded, may go to a file, may present it to the
  114. * user, that's your call
  115. */
  116. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  117. fprintf(stderr, "Fingerprint: ");
  118. for(i = 0; i < 20; i++) {
  119. fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
  120. }
  121. fprintf(stderr, "\n");
  122. if (auth_pw) {
  123. /* We could authenticate via password */
  124. if (libssh2_userauth_password(session, username, password)) {
  125. fprintf(stderr, "Authentication by password failed.\n");
  126. goto shutdown;
  127. }
  128. } else {
  129. /* Or by public key */
  130. if (libssh2_userauth_publickey_fromfile(session, username,
  131. "/home/username/.ssh/id_rsa.pub",
  132. "/home/username/.ssh/id_rsa",
  133. password)) {
  134. fprintf(stderr, "\tAuthentication by public key failed\n");
  135. goto shutdown;
  136. }
  137. }
  138. /* Send a file via scp. The mode parameter must only have permissions! */
  139. channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777,
  140. (unsigned long)fileinfo.st_size);
  141. if (!channel) {
  142. char *errmsg;
  143. int errlen;
  144. int err = libssh2_session_last_error(session, &errmsg, &errlen, 0);
  145. fprintf(stderr, "Unable to open a session: (%d) %s\n", err, errmsg);
  146. goto shutdown;
  147. }
  148. fprintf(stderr, "SCP session waiting to send file\n");
  149. do {
  150. nread = fread(mem, 1, sizeof(mem), local);
  151. if (nread <= 0) {
  152. /* end of file */
  153. break;
  154. }
  155. ptr = mem;
  156. do {
  157. /* write the same data over and over, until error or completion */
  158. rc = libssh2_channel_write(channel, ptr, nread);
  159. if (rc < 0) {
  160. fprintf(stderr, "ERROR %d\n", rc);
  161. break;
  162. }
  163. else {
  164. /* rc indicates how many bytes were written this time */
  165. ptr += rc;
  166. nread -= rc;
  167. }
  168. } while (nread);
  169. } while (1);
  170. fprintf(stderr, "Sending EOF\n");
  171. libssh2_channel_send_eof(channel);
  172. fprintf(stderr, "Waiting for EOF\n");
  173. libssh2_channel_wait_eof(channel);
  174. fprintf(stderr, "Waiting for channel to close\n");
  175. libssh2_channel_wait_closed(channel);
  176. libssh2_channel_free(channel);
  177. channel = NULL;
  178. shutdown:
  179. if(session) {
  180. libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
  181. libssh2_session_free(session);
  182. }
  183. #ifdef WIN32
  184. closesocket(sock);
  185. #else
  186. close(sock);
  187. #endif
  188. if (local)
  189. fclose(local);
  190. fprintf(stderr, "all done\n");
  191. libssh2_exit();
  192. return 0;
  193. }