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.
 
 
 
 

223 lines
5.7 KiB

  1. /*
  2. * Sample showing how to do SFTP write transfers.
  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 192.168.0.1 user password sftp_write.c /tmp/secrets"
  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. char mem[1024*100];
  48. size_t nread;
  49. char *ptr;
  50. #ifdef WIN32
  51. WSADATA wsadata;
  52. int err;
  53. err = WSAStartup(MAKEWORD(2, 0), &wsadata);
  54. if(err != 0) {
  55. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  56. return 1;
  57. }
  58. #endif
  59. if(argc > 1) {
  60. hostaddr = inet_addr(argv[1]);
  61. }
  62. else {
  63. hostaddr = htonl(0x7F000001);
  64. }
  65. if(argc > 2) {
  66. username = argv[2];
  67. }
  68. if(argc > 3) {
  69. password = argv[3];
  70. }
  71. if(argc > 4) {
  72. loclfile = argv[4];
  73. }
  74. if(argc > 5) {
  75. sftppath = argv[5];
  76. }
  77. rc = libssh2_init(0);
  78. if(rc != 0) {
  79. fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  80. return 1;
  81. }
  82. local = fopen(loclfile, "rb");
  83. if(!local) {
  84. fprintf(stderr, "Can't open local file %s\n", loclfile);
  85. return -1;
  86. }
  87. /*
  88. * The application code is responsible for creating the socket
  89. * and establishing the connection
  90. */
  91. sock = socket(AF_INET, SOCK_STREAM, 0);
  92. sin.sin_family = AF_INET;
  93. sin.sin_port = htons(22);
  94. sin.sin_addr.s_addr = hostaddr;
  95. if(connect(sock, (struct sockaddr*)(&sin),
  96. sizeof(struct sockaddr_in)) != 0) {
  97. fprintf(stderr, "failed to connect!\n");
  98. return -1;
  99. }
  100. /* Create a session instance
  101. */
  102. session = libssh2_session_init();
  103. if(!session)
  104. return -1;
  105. /* Since we have set non-blocking, tell libssh2 we are blocking */
  106. libssh2_session_set_blocking(session, 1);
  107. /* ... start it up. This will trade welcome banners, exchange keys,
  108. * and setup crypto, compression, and MAC layers
  109. */
  110. rc = libssh2_session_handshake(session, sock);
  111. if(rc) {
  112. fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
  113. return -1;
  114. }
  115. /* At this point we havn't yet authenticated. The first thing to do
  116. * is check the hostkey's fingerprint against our known hosts Your app
  117. * may have it hard coded, may go to a file, may present it to the
  118. * user, that's your call
  119. */
  120. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  121. fprintf(stderr, "Fingerprint: ");
  122. for(i = 0; i < 20; i++) {
  123. fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
  124. }
  125. fprintf(stderr, "\n");
  126. if(auth_pw) {
  127. /* We could authenticate via password */
  128. if(libssh2_userauth_password(session, username, password)) {
  129. fprintf(stderr, "Authentication by password failed.\n");
  130. goto shutdown;
  131. }
  132. }
  133. else {
  134. /* Or by public key */
  135. const char *pubkey = "/home/username/.ssh/id_rsa.pub";
  136. const char *privkey = "/home/username/.ssh/id_rsa.pub";
  137. if(libssh2_userauth_publickey_fromfile(session, username,
  138. pubkey, privkey,
  139. password)) {
  140. fprintf(stderr, "\tAuthentication by public key failed\n");
  141. goto shutdown;
  142. }
  143. }
  144. fprintf(stderr, "libssh2_sftp_init()!\n");
  145. sftp_session = libssh2_sftp_init(session);
  146. if(!sftp_session) {
  147. fprintf(stderr, "Unable to init SFTP session\n");
  148. goto shutdown;
  149. }
  150. fprintf(stderr, "libssh2_sftp_open()!\n");
  151. /* Request a file via SFTP */
  152. sftp_handle =
  153. libssh2_sftp_open(sftp_session, sftppath,
  154. LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
  155. LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
  156. LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
  157. if(!sftp_handle) {
  158. fprintf(stderr, "Unable to open file with SFTP\n");
  159. goto shutdown;
  160. }
  161. fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n");
  162. do {
  163. nread = fread(mem, 1, sizeof(mem), local);
  164. if(nread <= 0) {
  165. /* end of file */
  166. break;
  167. }
  168. ptr = mem;
  169. do {
  170. /* write data in a loop until we block */
  171. rc = libssh2_sftp_write(sftp_handle, ptr, nread);
  172. if(rc < 0)
  173. break;
  174. ptr += rc;
  175. nread -= rc;
  176. } while(nread);
  177. } while(rc > 0);
  178. libssh2_sftp_close(sftp_handle);
  179. libssh2_sftp_shutdown(sftp_session);
  180. shutdown:
  181. libssh2_session_disconnect(session,
  182. "Normal Shutdown, Thank you for playing");
  183. libssh2_session_free(session);
  184. #ifdef WIN32
  185. closesocket(sock);
  186. #else
  187. close(sock);
  188. #endif
  189. if(local)
  190. fclose(local);
  191. fprintf(stderr, "all done\n");
  192. libssh2_exit();
  193. return 0;
  194. }