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.
 
 
 
 

180 lines
4.6 KiB

  1. /*
  2. * Sample showing how to do SFTP non-blocking mkdir.
  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 /tmp/sftp_write_nonblock.c"
  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 *sftppath = "/tmp/sftp_mkdir_nonblock";
  42. int rc;
  43. LIBSSH2_SFTP *sftp_session;
  44. #ifdef WIN32
  45. WSADATA wsadata;
  46. int err;
  47. err = WSAStartup(MAKEWORD(2, 0), &wsadata);
  48. if(err != 0) {
  49. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  50. return 1;
  51. }
  52. #endif
  53. if(argc > 1) {
  54. hostaddr = inet_addr(argv[1]);
  55. }
  56. else {
  57. hostaddr = htonl(0x7F000001);
  58. }
  59. if(argc > 2) {
  60. username = argv[2];
  61. }
  62. if(argc > 3) {
  63. password = argv[3];
  64. }
  65. if(argc > 4) {
  66. sftppath = argv[4];
  67. }
  68. rc = libssh2_init(0);
  69. if(rc != 0) {
  70. fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  71. return 1;
  72. }
  73. /*
  74. * The application code is responsible for creating the socket
  75. * and establishing the connection
  76. */
  77. sock = socket(AF_INET, SOCK_STREAM, 0);
  78. sin.sin_family = AF_INET;
  79. sin.sin_port = htons(22);
  80. sin.sin_addr.s_addr = hostaddr;
  81. if(connect(sock, (struct sockaddr*)(&sin),
  82. sizeof(struct sockaddr_in)) != 0) {
  83. fprintf(stderr, "failed to connect!\n");
  84. return -1;
  85. }
  86. /* Create a session instance
  87. */
  88. session = libssh2_session_init();
  89. if(!session)
  90. return -1;
  91. /* ... start it up. This will trade welcome banners, exchange keys,
  92. * and setup crypto, compression, and MAC layers
  93. */
  94. rc = libssh2_session_handshake(session, sock);
  95. if(rc) {
  96. fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
  97. return -1;
  98. }
  99. /* At this point we havn't yet authenticated. The first thing to do
  100. * is check the hostkey's fingerprint against our known hosts Your app
  101. * may have it hard coded, may go to a file, may present it to the
  102. * user, that's your call
  103. */
  104. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  105. fprintf(stderr, "Fingerprint: ");
  106. for(i = 0; i < 20; i++) {
  107. fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
  108. }
  109. fprintf(stderr, "\n");
  110. if(auth_pw) {
  111. /* We could authenticate via password */
  112. if(libssh2_userauth_password(session, username, password)) {
  113. fprintf(stderr, "Authentication by password failed.\n");
  114. goto shutdown;
  115. }
  116. }
  117. else {
  118. /* Or by public key */
  119. if(libssh2_userauth_publickey_fromfile(session, username,
  120. "/home/username/.ssh/id_rsa.pub",
  121. "/home/username/.ssh/id_rsa",
  122. password)) {
  123. fprintf(stderr, "\tAuthentication by public key failed\n");
  124. goto shutdown;
  125. }
  126. }
  127. fprintf(stderr, "libssh2_sftp_init()!\n");
  128. sftp_session = libssh2_sftp_init(session);
  129. if(!sftp_session) {
  130. fprintf(stderr, "Unable to init SFTP session\n");
  131. goto shutdown;
  132. }
  133. /* Since we have set non-blocking, tell libssh2 we are non-blocking */
  134. libssh2_session_set_blocking(session, 0);
  135. fprintf(stderr, "libssh2_sftp_mkdirnb()!\n");
  136. /* Make a directory via SFTP */
  137. while(libssh2_sftp_mkdir(sftp_session, sftppath,
  138. LIBSSH2_SFTP_S_IRWXU|
  139. LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IXGRP|
  140. LIBSSH2_SFTP_S_IROTH|LIBSSH2_SFTP_S_IXOTH)
  141. == LIBSSH2_ERROR_EAGAIN);
  142. libssh2_sftp_shutdown(sftp_session);
  143. shutdown:
  144. libssh2_session_disconnect(session, "Normal Shutdown");
  145. libssh2_session_free(session);
  146. #ifdef WIN32
  147. closesocket(sock);
  148. #else
  149. close(sock);
  150. #endif
  151. fprintf(stderr, "all done\n");
  152. libssh2_exit();
  153. return 0;
  154. }