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.
 
 
 
 

250 lines
6.5 KiB

  1. /*
  2. * Sample doing an SFTP directory listing.
  3. *
  4. * The sample code has default values for host name, user name, password and
  5. * path, but you can specify them on the command line like:
  6. *
  7. * "sftpdir 192.168.0.1 user password /tmp/secretdir"
  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. #ifdef HAVE_INTTYPES_H
  28. # include <inttypes.h>
  29. #endif
  30. #include <sys/types.h>
  31. #include <fcntl.h>
  32. #include <errno.h>
  33. #include <stdio.h>
  34. #include <ctype.h>
  35. #ifdef WIN32
  36. #define __FILESIZE "I64"
  37. #else
  38. #define __FILESIZE "llu"
  39. #endif
  40. int main(int argc, char *argv[])
  41. {
  42. unsigned long hostaddr;
  43. int sock, i, auth_pw = 1;
  44. struct sockaddr_in sin;
  45. const char *fingerprint;
  46. LIBSSH2_SESSION *session;
  47. const char *username = "username";
  48. const char *password = "password";
  49. const char *sftppath = "/tmp/secretdir";
  50. const char *pubkey = "/home/username/.ssh/id_rsa.pub";
  51. const char *privkey = "/home/username/.ssh/id_rsa";
  52. int rc;
  53. LIBSSH2_SFTP *sftp_session;
  54. LIBSSH2_SFTP_HANDLE *sftp_handle;
  55. #ifdef WIN32
  56. WSADATA wsadata;
  57. int err;
  58. err = WSAStartup(MAKEWORD(2, 0), &wsadata);
  59. if(err != 0) {
  60. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  61. return 1;
  62. }
  63. #endif
  64. if(argc > 1) {
  65. hostaddr = inet_addr(argv[1]);
  66. }
  67. else {
  68. hostaddr = htonl(0x7F000001);
  69. }
  70. if(argc > 2) {
  71. username = argv[2];
  72. }
  73. if(argc > 3) {
  74. password = argv[3];
  75. }
  76. if(argc > 4) {
  77. sftppath = argv[4];
  78. }
  79. rc = libssh2_init(0);
  80. if(rc != 0) {
  81. fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  82. return 1;
  83. }
  84. /*
  85. * The application code is responsible for creating the socket
  86. * and establishing the connection
  87. */
  88. sock = socket(AF_INET, SOCK_STREAM, 0);
  89. sin.sin_family = AF_INET;
  90. sin.sin_port = htons(22);
  91. sin.sin_addr.s_addr = hostaddr;
  92. if(connect(sock, (struct sockaddr*)(&sin),
  93. sizeof(struct sockaddr_in)) != 0) {
  94. fprintf(stderr, "failed to connect!\n");
  95. return -1;
  96. }
  97. /* Create a session instance
  98. */
  99. session = libssh2_session_init();
  100. if(!session)
  101. return -1;
  102. /* Since we have set non-blocking, tell libssh2 we are non-blocking */
  103. libssh2_session_set_blocking(session, 0);
  104. /* ... start it up. This will trade welcome banners, exchange keys,
  105. * and setup crypto, compression, and MAC layers
  106. */
  107. while((rc = libssh2_session_handshake(session, sock)) ==
  108. LIBSSH2_ERROR_EAGAIN);
  109. if(rc) {
  110. fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
  111. return -1;
  112. }
  113. /* At this point we havn't yet authenticated. The first thing to do
  114. * is check the hostkey's fingerprint against our known hosts Your app
  115. * may have it hard coded, may go to a file, may present it to the
  116. * user, that's your call
  117. */
  118. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  119. fprintf(stderr, "Fingerprint: ");
  120. for(i = 0; i < 20; i++) {
  121. fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
  122. }
  123. fprintf(stderr, "\n");
  124. if(auth_pw) {
  125. /* We could authenticate via password */
  126. while((rc = libssh2_userauth_password(session, username, password)) ==
  127. LIBSSH2_ERROR_EAGAIN);
  128. if(rc) {
  129. fprintf(stderr, "Authentication by password failed.\n");
  130. goto shutdown;
  131. }
  132. }
  133. else {
  134. /* Or by public key */
  135. while((rc = libssh2_userauth_publickey_fromfile(session, username,
  136. pubkey, privkey,
  137. password)) ==
  138. LIBSSH2_ERROR_EAGAIN);
  139. if(rc) {
  140. fprintf(stderr, "\tAuthentication by public key failed\n");
  141. goto shutdown;
  142. }
  143. }
  144. fprintf(stderr, "libssh2_sftp_init()!\n");
  145. do {
  146. sftp_session = libssh2_sftp_init(session);
  147. if((!sftp_session) && (libssh2_session_last_errno(session) !=
  148. LIBSSH2_ERROR_EAGAIN)) {
  149. fprintf(stderr, "Unable to init SFTP session\n");
  150. goto shutdown;
  151. }
  152. } while(!sftp_session);
  153. fprintf(stderr, "libssh2_sftp_opendir()!\n");
  154. /* Request a dir listing via SFTP */
  155. do {
  156. sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath);
  157. if((!sftp_handle) && (libssh2_session_last_errno(session) !=
  158. LIBSSH2_ERROR_EAGAIN)) {
  159. fprintf(stderr, "Unable to open dir with SFTP\n");
  160. goto shutdown;
  161. }
  162. } while(!sftp_handle);
  163. fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n");
  164. do {
  165. char mem[512];
  166. LIBSSH2_SFTP_ATTRIBUTES attrs;
  167. /* loop until we fail */
  168. while((rc = libssh2_sftp_readdir(sftp_handle, mem, sizeof(mem),
  169. &attrs)) == LIBSSH2_ERROR_EAGAIN) {
  170. ;
  171. }
  172. if(rc > 0) {
  173. /* rc is the length of the file name in the mem
  174. buffer */
  175. if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
  176. /* this should check what permissions it
  177. is and print the output accordingly */
  178. printf("--fix----- ");
  179. }
  180. else {
  181. printf("---------- ");
  182. }
  183. if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) {
  184. printf("%4d %4d ", (int) attrs.uid, (int) attrs.gid);
  185. }
  186. else {
  187. printf(" - - ");
  188. }
  189. if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
  190. printf("%8" __FILESIZE " ", attrs.filesize);
  191. }
  192. printf("%s\n", mem);
  193. }
  194. else if(rc == LIBSSH2_ERROR_EAGAIN) {
  195. /* blocking */
  196. fprintf(stderr, "Blocking\n");
  197. }
  198. else {
  199. break;
  200. }
  201. } while(1);
  202. libssh2_sftp_closedir(sftp_handle);
  203. libssh2_sftp_shutdown(sftp_session);
  204. shutdown:
  205. libssh2_session_disconnect(session, "Normal Shutdown");
  206. libssh2_session_free(session);
  207. #ifdef WIN32
  208. closesocket(sock);
  209. #else
  210. close(sock);
  211. #endif
  212. fprintf(stderr, "all done\n");
  213. libssh2_exit();
  214. return 0;
  215. }