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.
 
 
 
 

239 lines
6.2 KiB

  1. /*
  2. * Sample showing how to do SFTP append of a local file onto a remote one.
  3. *
  4. * The sample code has default values for host name, user name, password
  5. * and path to copy, but you can specify them on the command line like:
  6. *
  7. * sftp_append 192.168.0.1 user password localfile /tmp/remotefile
  8. */
  9. #include "libssh2_config.h"
  10. #include <libssh2.h>
  11. #include <libssh2_sftp.h>
  12. #ifdef HAVE_WINSOCK2_H
  13. # include <winsock2.h>
  14. #endif
  15. #ifdef HAVE_SYS_SOCKET_H
  16. # include <sys/socket.h>
  17. #endif
  18. #ifdef HAVE_NETINET_IN_H
  19. # include <netinet/in.h>
  20. #endif
  21. # ifdef HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #ifdef HAVE_ARPA_INET_H
  25. # include <arpa/inet.h>
  26. #endif
  27. #include <sys/types.h>
  28. #include <fcntl.h>
  29. #include <errno.h>
  30. #include <stdio.h>
  31. #include <ctype.h>
  32. int main(int argc, char *argv[])
  33. {
  34. unsigned long hostaddr;
  35. int sock, i, auth_pw = 1;
  36. struct sockaddr_in sin;
  37. const char *fingerprint;
  38. LIBSSH2_SESSION *session;
  39. const char *username = "username";
  40. const char *password = "password";
  41. const char *loclfile = "sftp_write.c";
  42. const char *sftppath = "/tmp/TEST";
  43. int rc;
  44. FILE *local;
  45. LIBSSH2_SFTP *sftp_session;
  46. LIBSSH2_SFTP_HANDLE *sftp_handle;
  47. LIBSSH2_SFTP_ATTRIBUTES attrs;
  48. char mem[1024*100];
  49. size_t nread;
  50. char *ptr;
  51. #ifdef WIN32
  52. WSADATA wsadata;
  53. int err;
  54. err = WSAStartup(MAKEWORD(2, 0), &wsadata);
  55. if(err) {
  56. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  57. return 1;
  58. }
  59. #endif
  60. if(argc > 1) {
  61. hostaddr = inet_addr(argv[1]);
  62. }
  63. else {
  64. hostaddr = htonl(0x7F000001);
  65. }
  66. if(argc > 2) {
  67. username = argv[2];
  68. }
  69. if(argc > 3) {
  70. password = argv[3];
  71. }
  72. if(argc > 4) {
  73. loclfile = argv[4];
  74. }
  75. if(argc > 5) {
  76. sftppath = argv[5];
  77. }
  78. rc = libssh2_init(0);
  79. if(rc) {
  80. fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  81. return 1;
  82. }
  83. local = fopen(loclfile, "rb");
  84. if(!local) {
  85. fprintf(stderr, "Can't open local file %s\n", loclfile);
  86. return -1;
  87. }
  88. /*
  89. * The application code is responsible for creating the socket
  90. * and establishing the connection
  91. */
  92. sock = socket(AF_INET, SOCK_STREAM, 0);
  93. sin.sin_family = AF_INET;
  94. sin.sin_port = htons(22);
  95. sin.sin_addr.s_addr = hostaddr;
  96. if(connect(sock, (struct sockaddr*)(&sin),
  97. sizeof(struct sockaddr_in)) != 0) {
  98. fprintf(stderr, "failed to connect!\n");
  99. return -1;
  100. }
  101. /* Create a session instance
  102. */
  103. session = libssh2_session_init();
  104. if(!session)
  105. return -1;
  106. /* Since we have set non-blocking, tell libssh2 we are blocking */
  107. libssh2_session_set_blocking(session, 1);
  108. /* ... start it up. This will trade welcome banners, exchange keys,
  109. * and setup crypto, compression, and MAC layers
  110. */
  111. rc = libssh2_session_handshake(session, sock);
  112. if(rc) {
  113. fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
  114. return -1;
  115. }
  116. /* At this point we havn't yet authenticated. The first thing to do
  117. * is check the hostkey's fingerprint against our known hosts Your app
  118. * may have it hard coded, may go to a file, may present it to the
  119. * user, that's your call
  120. */
  121. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  122. fprintf(stderr, "Fingerprint: ");
  123. for(i = 0; i < 20; i++) {
  124. fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
  125. }
  126. fprintf(stderr, "\n");
  127. if(auth_pw) {
  128. /* We could authenticate via password */
  129. if(libssh2_userauth_password(session, username, password)) {
  130. fprintf(stderr, "Authentication by password failed.\n");
  131. goto shutdown;
  132. }
  133. }
  134. else {
  135. /* Or by public key */
  136. #define HOME "/home/username/"
  137. if(libssh2_userauth_publickey_fromfile(session, username,
  138. HOME ".ssh/id_rsa.pub",
  139. HOME ".ssh/id_rsa",
  140. password)) {
  141. fprintf(stderr, "\tAuthentication by public key failed\n");
  142. goto shutdown;
  143. }
  144. }
  145. fprintf(stderr, "libssh2_sftp_init()!\n");
  146. sftp_session = libssh2_sftp_init(session);
  147. if(!sftp_session) {
  148. fprintf(stderr, "Unable to init SFTP session\n");
  149. goto shutdown;
  150. }
  151. fprintf(stderr, "libssh2_sftp_open() for READ and WRITE!\n");
  152. /* Request a file via SFTP */
  153. sftp_handle =
  154. libssh2_sftp_open(sftp_session, sftppath,
  155. LIBSSH2_FXF_WRITE|LIBSSH2_FXF_READ,
  156. LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
  157. LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
  158. if(!sftp_handle) {
  159. fprintf(stderr, "Unable to open file with SFTP\n");
  160. goto shutdown;
  161. }
  162. if(libssh2_sftp_fstat_ex(sftp_handle, &attrs, 0) < 0) {
  163. fprintf(stderr, "libssh2_sftp_fstat_ex failed\n");
  164. goto shutdown;
  165. }
  166. else
  167. libssh2_sftp_seek64(sftp_handle, attrs.filesize);
  168. fprintf(stderr, "Did a seek to position %ld\n", (long) attrs.filesize);
  169. fprintf(stderr, "libssh2_sftp_open() a handle for APPEND\n");
  170. if(!sftp_handle) {
  171. fprintf(stderr, "Unable to open file with SFTP\n");
  172. goto shutdown;
  173. }
  174. fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n");
  175. do {
  176. nread = fread(mem, 1, sizeof(mem), local);
  177. if(nread <= 0) {
  178. /* end of file */
  179. break;
  180. }
  181. ptr = mem;
  182. do {
  183. /* write data in a loop until we block */
  184. rc = libssh2_sftp_write(sftp_handle, ptr, nread);
  185. if(rc < 0)
  186. break;
  187. ptr += rc;
  188. nread -= rc;
  189. } while(nread);
  190. } while(rc > 0);
  191. libssh2_sftp_close(sftp_handle);
  192. libssh2_sftp_shutdown(sftp_session);
  193. shutdown:
  194. libssh2_session_disconnect(session,
  195. "Normal Shutdown, Thank you for playing");
  196. libssh2_session_free(session);
  197. #ifdef WIN32
  198. closesocket(sock);
  199. #else
  200. close(sock);
  201. #endif
  202. if(local)
  203. fclose(local);
  204. fprintf(stderr, "all done\n");
  205. libssh2_exit();
  206. return 0;
  207. }