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.
 
 
 
 

229 lines
5.7 KiB

  1. /*
  2. * Sample showing how to do an SCP upload.
  3. */
  4. #include "libssh2_config.h"
  5. #include <libssh2.h>
  6. #ifdef HAVE_WINSOCK2_H
  7. # include <winsock2.h>
  8. #endif
  9. #ifdef HAVE_SYS_SOCKET_H
  10. # include <sys/socket.h>
  11. #endif
  12. #ifdef HAVE_NETINET_IN_H
  13. # include <netinet/in.h>
  14. #endif
  15. # ifdef HAVE_UNISTD_H
  16. #include <unistd.h>
  17. #endif
  18. #ifdef HAVE_ARPA_INET_H
  19. # include <arpa/inet.h>
  20. #endif
  21. #ifdef HAVE_SYS_TIME_H
  22. # include <sys/time.h>
  23. #endif
  24. #include <sys/types.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. int main(int argc, char *argv[])
  30. {
  31. unsigned long hostaddr;
  32. int sock, i, auth_pw = 1;
  33. struct sockaddr_in sin;
  34. const char *fingerprint;
  35. LIBSSH2_SESSION *session = NULL;
  36. LIBSSH2_CHANNEL *channel;
  37. const char *username = "username";
  38. const char *password = "password";
  39. const char *loclfile = "scp_write.c";
  40. const char *scppath = "/tmp/TEST";
  41. FILE *local;
  42. int rc;
  43. char mem[1024];
  44. size_t nread;
  45. char *ptr;
  46. struct stat fileinfo;
  47. #ifdef WIN32
  48. WSADATA wsadata;
  49. int err;
  50. err = WSAStartup(MAKEWORD(2, 0), &wsadata);
  51. if(err != 0) {
  52. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  53. return 1;
  54. }
  55. #endif
  56. if(argc > 1) {
  57. hostaddr = inet_addr(argv[1]);
  58. }
  59. else {
  60. hostaddr = htonl(0x7F000001);
  61. }
  62. if(argc > 2) {
  63. username = argv[2];
  64. }
  65. if(argc > 3) {
  66. password = argv[3];
  67. }
  68. if(argc > 4) {
  69. loclfile = argv[4];
  70. }
  71. if(argc > 5) {
  72. scppath = argv[5];
  73. }
  74. rc = libssh2_init(0);
  75. if(rc != 0) {
  76. fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  77. return 1;
  78. }
  79. local = fopen(loclfile, "rb");
  80. if(!local) {
  81. fprintf(stderr, "Can't open local file %s\n", loclfile);
  82. return -1;
  83. }
  84. stat(loclfile, &fileinfo);
  85. /* Ultra basic "connect to port 22 on localhost"
  86. * Your code is responsible for creating the socket establishing the
  87. * connection
  88. */
  89. sock = socket(AF_INET, SOCK_STREAM, 0);
  90. if(-1 == sock) {
  91. fprintf(stderr, "failed to create socket!\n");
  92. return -1;
  93. }
  94. sin.sin_family = AF_INET;
  95. sin.sin_port = htons(22);
  96. sin.sin_addr.s_addr = hostaddr;
  97. if(connect(sock, (struct sockaddr*)(&sin),
  98. sizeof(struct sockaddr_in)) != 0) {
  99. fprintf(stderr, "failed to connect!\n");
  100. return -1;
  101. }
  102. /* Create a session instance
  103. */
  104. session = libssh2_session_init();
  105. if(!session)
  106. return -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. #define HOME "/home/username/"
  136. if(libssh2_userauth_publickey_fromfile(session, username,
  137. HOME ".ssh/id_rsa.pub",
  138. HOME ".ssh/id_rsa",
  139. password)) {
  140. fprintf(stderr, "\tAuthentication by public key failed\n");
  141. goto shutdown;
  142. }
  143. }
  144. /* Send a file via scp. The mode parameter must only have permissions! */
  145. channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777,
  146. (unsigned long)fileinfo.st_size);
  147. if(!channel) {
  148. char *errmsg;
  149. int errlen;
  150. int err = libssh2_session_last_error(session, &errmsg, &errlen, 0);
  151. fprintf(stderr, "Unable to open a session: (%d) %s\n", err, errmsg);
  152. goto shutdown;
  153. }
  154. fprintf(stderr, "SCP session waiting to send file\n");
  155. do {
  156. nread = fread(mem, 1, sizeof(mem), local);
  157. if(nread <= 0) {
  158. /* end of file */
  159. break;
  160. }
  161. ptr = mem;
  162. do {
  163. /* write the same data over and over, until error or completion */
  164. rc = libssh2_channel_write(channel, ptr, nread);
  165. if(rc < 0) {
  166. fprintf(stderr, "ERROR %d\n", rc);
  167. break;
  168. }
  169. else {
  170. /* rc indicates how many bytes were written this time */
  171. ptr += rc;
  172. nread -= rc;
  173. }
  174. } while(nread);
  175. } while(1);
  176. fprintf(stderr, "Sending EOF\n");
  177. libssh2_channel_send_eof(channel);
  178. fprintf(stderr, "Waiting for EOF\n");
  179. libssh2_channel_wait_eof(channel);
  180. fprintf(stderr, "Waiting for channel to close\n");
  181. libssh2_channel_wait_closed(channel);
  182. libssh2_channel_free(channel);
  183. channel = NULL;
  184. shutdown:
  185. if(session) {
  186. libssh2_session_disconnect(session, "Normal Shutdown");
  187. libssh2_session_free(session);
  188. }
  189. #ifdef WIN32
  190. closesocket(sock);
  191. #else
  192. close(sock);
  193. #endif
  194. if(local)
  195. fclose(local);
  196. fprintf(stderr, "all done\n");
  197. libssh2_exit();
  198. return 0;
  199. }