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.
 
 
 
 

305 lines
8.2 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. 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. static void kbd_callback(const char *name, int name_len,
  45. const char *instruction, int instruction_len,
  46. int num_prompts,
  47. const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
  48. LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
  49. void **abstract)
  50. {
  51. (void)name;
  52. (void)name_len;
  53. (void)instruction;
  54. (void)instruction_len;
  55. if(num_prompts == 1) {
  56. responses[0].text = strdup(password);
  57. responses[0].length = strlen(password);
  58. }
  59. (void)prompts;
  60. (void)abstract;
  61. } /* kbd_callback */
  62. int main(int argc, char *argv[])
  63. {
  64. unsigned long hostaddr;
  65. int rc, sock, i, auth_pw = 0;
  66. struct sockaddr_in sin;
  67. const char *fingerprint;
  68. char *userauthlist;
  69. LIBSSH2_SESSION *session;
  70. const char *sftppath = "/tmp/secretdir";
  71. LIBSSH2_SFTP *sftp_session;
  72. LIBSSH2_SFTP_HANDLE *sftp_handle;
  73. #ifdef WIN32
  74. WSADATA wsadata;
  75. int err;
  76. err = WSAStartup(MAKEWORD(2, 0), &wsadata);
  77. if(err != 0) {
  78. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  79. return 1;
  80. }
  81. #endif
  82. if(argc > 1) {
  83. hostaddr = inet_addr(argv[1]);
  84. }
  85. else {
  86. hostaddr = htonl(0x7F000001);
  87. }
  88. if(argc > 2) {
  89. username = argv[2];
  90. }
  91. if(argc > 3) {
  92. password = argv[3];
  93. }
  94. if(argc > 4) {
  95. sftppath = argv[4];
  96. }
  97. rc = libssh2_init(0);
  98. if(rc != 0) {
  99. fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  100. return 1;
  101. }
  102. /*
  103. * The application code is responsible for creating the socket
  104. * and establishing the connection
  105. */
  106. sock = socket(AF_INET, SOCK_STREAM, 0);
  107. sin.sin_family = AF_INET;
  108. sin.sin_port = htons(22);
  109. sin.sin_addr.s_addr = hostaddr;
  110. if(connect(sock, (struct sockaddr*)(&sin),
  111. sizeof(struct sockaddr_in)) != 0) {
  112. fprintf(stderr, "failed to connect!\n");
  113. return -1;
  114. }
  115. /* Create a session instance
  116. */
  117. session = libssh2_session_init();
  118. if(!session)
  119. return -1;
  120. /* ... start it up. This will trade welcome banners, exchange keys,
  121. * and setup crypto, compression, and MAC layers
  122. */
  123. rc = libssh2_session_handshake(session, sock);
  124. if(rc) {
  125. fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
  126. return -1;
  127. }
  128. /* At this point we havn't yet authenticated. The first thing to do
  129. * is check the hostkey's fingerprint against our known hosts Your app
  130. * may have it hard coded, may go to a file, may present it to the
  131. * user, that's your call
  132. */
  133. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  134. fprintf(stderr, "Fingerprint: ");
  135. for(i = 0; i < 20; i++) {
  136. fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
  137. }
  138. fprintf(stderr, "\n");
  139. /* check what authentication methods are available */
  140. userauthlist = libssh2_userauth_list(session, username, strlen(username));
  141. fprintf(stderr, "Authentication methods: %s\n", userauthlist);
  142. if(strstr(userauthlist, "password") != NULL) {
  143. auth_pw |= 1;
  144. }
  145. if(strstr(userauthlist, "keyboard-interactive") != NULL) {
  146. auth_pw |= 2;
  147. }
  148. if(strstr(userauthlist, "publickey") != NULL) {
  149. auth_pw |= 4;
  150. }
  151. /* if we got an 5. argument we set this option if supported */
  152. if(argc > 5) {
  153. if((auth_pw & 1) && !strcasecmp(argv[5], "-p")) {
  154. auth_pw = 1;
  155. }
  156. if((auth_pw & 2) && !strcasecmp(argv[5], "-i")) {
  157. auth_pw = 2;
  158. }
  159. if((auth_pw & 4) && !strcasecmp(argv[5], "-k")) {
  160. auth_pw = 4;
  161. }
  162. }
  163. if(auth_pw & 1) {
  164. /* We could authenticate via password */
  165. if(libssh2_userauth_password(session, username, password)) {
  166. fprintf(stderr, "\tAuthentication by password failed!\n");
  167. goto shutdown;
  168. }
  169. else {
  170. fprintf(stderr, "\tAuthentication by password succeeded.\n");
  171. }
  172. }
  173. else if(auth_pw & 2) {
  174. /* Or via keyboard-interactive */
  175. if(libssh2_userauth_keyboard_interactive(session, username,
  176. &kbd_callback) ) {
  177. fprintf(stderr,
  178. "\tAuthentication by keyboard-interactive failed!\n");
  179. goto shutdown;
  180. }
  181. else {
  182. fprintf(stderr,
  183. "\tAuthentication by keyboard-interactive succeeded.\n");
  184. }
  185. }
  186. else if(auth_pw & 4) {
  187. /* Or by public key */
  188. if(libssh2_userauth_publickey_fromfile(session, username, keyfile1,
  189. keyfile2, password)) {
  190. fprintf(stderr, "\tAuthentication by public key failed!\n");
  191. goto shutdown;
  192. }
  193. else {
  194. fprintf(stderr, "\tAuthentication by public key succeeded.\n");
  195. }
  196. }
  197. else {
  198. fprintf(stderr, "No supported authentication methods found!\n");
  199. goto shutdown;
  200. }
  201. fprintf(stderr, "libssh2_sftp_init()!\n");
  202. sftp_session = libssh2_sftp_init(session);
  203. if(!sftp_session) {
  204. fprintf(stderr, "Unable to init SFTP session\n");
  205. goto shutdown;
  206. }
  207. /* Since we have not set non-blocking, tell libssh2 we are blocking */
  208. libssh2_session_set_blocking(session, 1);
  209. fprintf(stderr, "libssh2_sftp_opendir()!\n");
  210. /* Request a dir listing via SFTP */
  211. sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath);
  212. if(!sftp_handle) {
  213. fprintf(stderr, "Unable to open dir with SFTP\n");
  214. goto shutdown;
  215. }
  216. fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n");
  217. do {
  218. char mem[512];
  219. char longentry[512];
  220. LIBSSH2_SFTP_ATTRIBUTES attrs;
  221. /* loop until we fail */
  222. rc = libssh2_sftp_readdir_ex(sftp_handle, mem, sizeof(mem),
  223. longentry, sizeof(longentry), &attrs);
  224. if(rc > 0) {
  225. /* rc is the length of the file name in the mem
  226. buffer */
  227. if(longentry[0] != '\0') {
  228. printf("%s\n", longentry);
  229. }
  230. else {
  231. if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
  232. /* this should check what permissions it
  233. is and print the output accordingly */
  234. printf("--fix----- ");
  235. }
  236. else {
  237. printf("---------- ");
  238. }
  239. if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) {
  240. printf("%4d %4d ", (int) attrs.uid, (int) attrs.gid);
  241. }
  242. else {
  243. printf(" - - ");
  244. }
  245. if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
  246. printf("%8" __FILESIZE " ", attrs.filesize);
  247. }
  248. printf("%s\n", mem);
  249. }
  250. }
  251. else
  252. break;
  253. } while(1);
  254. libssh2_sftp_closedir(sftp_handle);
  255. libssh2_sftp_shutdown(sftp_session);
  256. shutdown:
  257. libssh2_session_disconnect(session, "Normal Shutdown");
  258. libssh2_session_free(session);
  259. #ifdef WIN32
  260. closesocket(sock);
  261. #else
  262. close(sock);
  263. #endif
  264. fprintf(stderr, "all done\n");
  265. libssh2_exit();
  266. return 0;
  267. }