Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.24 KB | None | 0 0
  1. /*
  2.  * Sample showing how to do SFTP 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 /tmp/secrets -p|-i|-k"
  8.  */
  9.  
  10. #include "libssh2_config.h"
  11. #include <libssh2.h>
  12. #include <libssh2_sftp.h>
  13.  
  14. #ifdef HAVE_WINSOCK2_H
  15. #include <winsock2.h>
  16. #endif
  17. #ifdef HAVE_SYS_SOCKET_H
  18. #include <sys/socket.h>
  19. #endif
  20. #ifdef HAVE_NETINET_IN_H
  21. #include <netinet/in.h>
  22. #endif
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #ifdef HAVE_ARPA_INET_H
  27. #include <arpa/inet.h>
  28. #endif
  29. #ifdef HAVE_SYS_TIME_H
  30. #include <sys/time.h>
  31. #endif
  32.  
  33. #include <sys/types.h>
  34. #include <fcntl.h>
  35. #include <errno.h>
  36. #include <stdio.h>
  37. #include <ctype.h>
  38.  
  39.  
  40. const char *keyfile1="~/.ssh/id_rsa.pub";
  41. const char *keyfile2="~/.ssh/id_rsa";
  42. const char *username="username";
  43. const char *password="password";
  44. const char *sftppath="/tmp/TEST";
  45.  
  46.  
  47. static void kbd_callback(const char *name, int name_len,
  48.              const char *instruction, int instruction_len, int num_prompts,
  49.              const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
  50.              LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
  51.              void **abstract)
  52. {
  53.     int i;
  54.     size_t n;
  55.     char buf[1024];
  56.     (void)abstract;
  57.  
  58.     fprintf(stderr, "Performing keyboard-interactive authentication.\n");
  59.  
  60.     fprintf(stderr, "Authentication name: '");
  61.     fwrite(name, 1, name_len, stderr);
  62.     fprintf(stderr, "'\n");
  63.  
  64.     fprintf(stderr, "Authentication instruction: '");
  65.     fwrite(instruction, 1, instruction_len, stderr);
  66.     fprintf(stderr, "'\n");
  67.  
  68.     fprintf(stderr, "Number of prompts: %d\n\n", num_prompts);
  69.  
  70.     for (i = 0; i < num_prompts; i++) {
  71.         fprintf(stderr, "Prompt %d from server: '", i);
  72.         fwrite(prompts[i].text, 1, prompts[i].length, stderr);
  73.         fprintf(stderr, "'\n");
  74.  
  75.         fprintf(stderr, "Please type response: ");
  76.         fgets(buf, sizeof(buf), stdin);
  77.         n = strlen(buf);
  78.         while (n > 0 && strchr("\r\n", buf[n - 1]))
  79.           n--;
  80.         buf[n] = 0;
  81.  
  82.         responses[i].text = strdup(buf);
  83.         responses[i].length = n;
  84.  
  85.         fprintf(stderr, "Response %d from user is '", i);
  86.         fwrite(responses[i].text, 1, responses[i].length, stderr);
  87.         fprintf(stderr, "'\n\n");
  88.     }
  89.  
  90.     fprintf(stderr,
  91.         "Done. Sending keyboard-interactive responses to server now.\n");
  92. }
  93.  
  94.  
  95. int main(int argc, char *argv[])
  96. {
  97.     unsigned long hostaddr;
  98.     int sock, i, auth_pw = 0;
  99.     struct sockaddr_in sin;
  100.     const char *fingerprint;
  101.     char *userauthlist;
  102.     LIBSSH2_SESSION *session;
  103.     int rc;
  104.     LIBSSH2_SFTP *sftp_session;
  105.     LIBSSH2_SFTP_HANDLE *sftp_handle;
  106.  
  107. #ifdef WIN32
  108.     WSADATA wsadata;
  109.     int err;
  110.  
  111.     err = WSAStartup(MAKEWORD(2,0), &wsadata);
  112.     if (err != 0) {
  113.         fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  114.         return 1;
  115.     }
  116. #endif
  117.  
  118.     if (argc > 1) {
  119.         hostaddr = inet_addr(argv[1]);
  120.     } else {
  121.         hostaddr = htonl(0x7F000001);
  122.     }
  123.  
  124.     if(argc > 2) {
  125.         username = argv[2];
  126.     }
  127.     if(argc > 3) {
  128.         password = argv[3];
  129.     }
  130.     if(argc > 4) {
  131.         sftppath = argv[4];
  132.     }
  133.  
  134.     rc = libssh2_init (0);
  135.  
  136.     if (rc != 0) {
  137.         fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  138.         return 1;
  139.     }
  140.  
  141.     /*
  142.      * The application code is responsible for creating the socket
  143.      * and establishing the connection
  144.      */
  145.     sock = socket(AF_INET, SOCK_STREAM, 0);
  146.  
  147.     sin.sin_family = AF_INET;
  148.     sin.sin_port = htons(22);
  149.     sin.sin_addr.s_addr = hostaddr;
  150.     if (connect(sock, (struct sockaddr*)(&sin),
  151.                 sizeof(struct sockaddr_in)) != 0) {
  152.         fprintf(stderr, "failed to connect!\n");
  153.         return -1;
  154.     }
  155.  
  156.     /* Create a session instance
  157.      */
  158.     session = libssh2_session_init();
  159.  
  160.     if(!session)
  161.         return -1;
  162.  
  163.     /* Since we have set non-blocking, tell libssh2 we are blocking */
  164.     libssh2_session_set_blocking(session, 1);
  165.  
  166.  
  167.     /* ... start it up. This will trade welcome banners, exchange keys,
  168.      * and setup crypto, compression, and MAC layers
  169.      */
  170.     rc = libssh2_session_handshake(session, sock);
  171.  
  172.     if(rc) {
  173.         fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
  174.         return -1;
  175.     }
  176.  
  177.     /* At this point we havn't yet authenticated.  The first thing to do
  178.      * is check the hostkey's fingerprint against our known hosts Your app
  179.      * may have it hard coded, may go to a file, may present it to the
  180.      * user, that's your call
  181.      */
  182.     fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  183.  
  184.     fprintf(stderr, "Fingerprint: ");
  185.     for(i = 0; i < 20; i++) {
  186.         fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
  187.     }
  188.     fprintf(stderr, "\n");
  189.  
  190.     /* check what authentication methods are available */
  191.     userauthlist = libssh2_userauth_list(session, username, strlen(username));
  192.  
  193.     fprintf(stderr, "Authentication methods: %s\n", userauthlist);
  194.     if (strstr(userauthlist, "password") != NULL) {
  195.         auth_pw |= 1;
  196.     }
  197.     if (strstr(userauthlist, "keyboard-interactive") != NULL) {
  198.         auth_pw |= 2;
  199.     }
  200.     if (strstr(userauthlist, "publickey") != NULL) {
  201.         auth_pw |= 4;
  202.     }
  203.  
  204.     /* if we got an 4. argument we set this option if supported */
  205.     if(argc > 5) {
  206.         if ((auth_pw & 1) && !strcasecmp(argv[5], "-p")) {
  207.             auth_pw = 1;
  208.         }
  209.         if ((auth_pw & 2) && !strcasecmp(argv[5], "-i")) {
  210.             auth_pw = 2;
  211.         }
  212.         if ((auth_pw & 4) && !strcasecmp(argv[5], "-k")) {
  213.             auth_pw = 4;
  214.         }
  215.     }
  216.  
  217.     if (auth_pw & 1) {
  218.         /* We could authenticate via password */
  219.         if (libssh2_userauth_password(session, username, password)) {
  220.  
  221.             fprintf(stderr, "Authentication by password failed.\n");
  222.             goto shutdown;
  223.         }
  224.     } else if (auth_pw & 2) {
  225.         /* Or via keyboard-interactive */
  226.         if (libssh2_userauth_keyboard_interactive(session, username, &kbd_callback) ) {
  227.  
  228.             fprintf(stderr,
  229.                 "\tAuthentication by keyboard-interactive failed!\n");
  230.             goto shutdown;
  231.         } else {
  232.             fprintf(stderr,
  233.                 "\tAuthentication by keyboard-interactive succeeded.\n");
  234.         }
  235.     } else if (auth_pw & 4) {
  236.         /* Or by public key */
  237.         if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, keyfile2, password)) {
  238.  
  239.             fprintf(stderr, "\tAuthentication by public key failed!\n");
  240.             goto shutdown;
  241.         } else {
  242.             fprintf(stderr, "\tAuthentication by public key succeeded.\n");
  243.         }
  244.     } else {
  245.         fprintf(stderr, "No supported authentication methods found!\n");
  246.         goto shutdown;
  247.     }
  248.  
  249.     fprintf(stderr, "libssh2_sftp_init()!\n");
  250.  
  251.     sftp_session = libssh2_sftp_init(session);
  252.  
  253.  
  254.     if (!sftp_session) {
  255.         fprintf(stderr, "Unable to init SFTP session\n");
  256.         goto shutdown;
  257.     }
  258.  
  259.     fprintf(stderr, "libssh2_sftp_open()!\n");
  260.  
  261.     /* Request a file via SFTP */
  262.     sftp_handle =
  263.         libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0);
  264.  
  265.  
  266.     if (!sftp_handle) {
  267.         fprintf(stderr, "Unable to open file with SFTP: %ld\n",
  268.                 libssh2_sftp_last_error(sftp_session));
  269.  
  270.         goto shutdown;
  271.     }
  272.     fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n");
  273.  
  274.     do {
  275.         char mem[1024];
  276.  
  277.         /* loop until we fail */
  278.         fprintf(stderr, "libssh2_sftp_read()!\n");
  279.  
  280.         rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
  281.  
  282.         if (rc > 0) {
  283.             write(1, mem, rc);
  284.         } else {
  285.             break;
  286.         }
  287.     } while (1);
  288.  
  289.     libssh2_sftp_close(sftp_handle);
  290.  
  291.     libssh2_sftp_shutdown(sftp_session);
  292.  
  293.  
  294.   shutdown:
  295.  
  296.     libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
  297.  
  298.     libssh2_session_free(session);
  299.  
  300.  
  301. #ifdef WIN32
  302.     closesocket(sock);
  303. #else
  304.     close(sock);
  305. #endif
  306.     fprintf(stderr, "all done\n");
  307.  
  308.     libssh2_exit();
  309.  
  310.  
  311.     return 0;
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement