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.
 
 
 
 

275 lines
7.5 KiB

  1. /*
  2. * Sample showing how to do SSH2 connect.
  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. * "ssh2 host user password [-p|-i|-k]"
  8. */
  9. #include "libssh2_config.h"
  10. #include <libssh2.h>
  11. #include <libssh2_sftp.h>
  12. #ifdef HAVE_WINDOWS_H
  13. # include <windows.h>
  14. #endif
  15. #ifdef HAVE_WINSOCK2_H
  16. # include <winsock2.h>
  17. #endif
  18. #ifdef HAVE_SYS_SOCKET_H
  19. # include <sys/socket.h>
  20. #endif
  21. #ifdef HAVE_NETINET_IN_H
  22. # include <netinet/in.h>
  23. #endif
  24. # ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. # ifdef HAVE_ARPA_INET_H
  28. #include <arpa/inet.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. const char *keyfile1 = "~/.ssh/id_rsa.pub";
  36. const char *keyfile2 = "~/.ssh/id_rsa";
  37. const char *username = "username";
  38. const char *password = "password";
  39. static void kbd_callback(const char *name, int name_len,
  40. const char *instruction, int instruction_len,
  41. int num_prompts,
  42. const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
  43. LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
  44. void **abstract)
  45. {
  46. (void)name;
  47. (void)name_len;
  48. (void)instruction;
  49. (void)instruction_len;
  50. if(num_prompts == 1) {
  51. responses[0].text = strdup(password);
  52. responses[0].length = strlen(password);
  53. }
  54. (void)prompts;
  55. (void)abstract;
  56. } /* kbd_callback */
  57. int main(int argc, char *argv[])
  58. {
  59. unsigned long hostaddr;
  60. int rc, sock, i, auth_pw = 0;
  61. struct sockaddr_in sin;
  62. const char *fingerprint;
  63. char *userauthlist;
  64. LIBSSH2_SESSION *session;
  65. LIBSSH2_CHANNEL *channel;
  66. #ifdef WIN32
  67. WSADATA wsadata;
  68. int err;
  69. err = WSAStartup(MAKEWORD(2, 0), &wsadata);
  70. if(err != 0) {
  71. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  72. return 1;
  73. }
  74. #endif
  75. if(argc > 1) {
  76. hostaddr = inet_addr(argv[1]);
  77. }
  78. else {
  79. hostaddr = htonl(0x7F000001);
  80. }
  81. if(argc > 2) {
  82. username = argv[2];
  83. }
  84. if(argc > 3) {
  85. password = argv[3];
  86. }
  87. rc = libssh2_init(0);
  88. if(rc != 0) {
  89. fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  90. return 1;
  91. }
  92. /* Ultra basic "connect to port 22 on localhost". Your code is
  93. * responsible for creating the socket establishing the connection
  94. */
  95. sock = socket(AF_INET, SOCK_STREAM, 0);
  96. sin.sin_family = AF_INET;
  97. sin.sin_port = htons(22);
  98. sin.sin_addr.s_addr = hostaddr;
  99. if(connect(sock, (struct sockaddr*)(&sin),
  100. sizeof(struct sockaddr_in)) != 0) {
  101. fprintf(stderr, "failed to connect!\n");
  102. return -1;
  103. }
  104. /* Create a session instance and start it up. This will trade welcome
  105. * banners, exchange keys, and setup crypto, compression, and MAC layers
  106. */
  107. session = libssh2_session_init();
  108. if(libssh2_session_handshake(session, sock)) {
  109. fprintf(stderr, "Failure establishing SSH session\n");
  110. return -1;
  111. }
  112. /* At this point we havn't authenticated. The first thing to do is check
  113. * the hostkey's fingerprint against our known hosts Your app may have it
  114. * hard coded, may go to a file, may present it to the user, that's your
  115. * call
  116. */
  117. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  118. fprintf(stderr, "Fingerprint: ");
  119. for(i = 0; i < 20; i++) {
  120. fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
  121. }
  122. fprintf(stderr, "\n");
  123. /* check what authentication methods are available */
  124. userauthlist = libssh2_userauth_list(session, username, strlen(username));
  125. fprintf(stderr, "Authentication methods: %s\n", userauthlist);
  126. if(strstr(userauthlist, "password") != NULL) {
  127. auth_pw |= 1;
  128. }
  129. if(strstr(userauthlist, "keyboard-interactive") != NULL) {
  130. auth_pw |= 2;
  131. }
  132. if(strstr(userauthlist, "publickey") != NULL) {
  133. auth_pw |= 4;
  134. }
  135. /* if we got an 4. argument we set this option if supported */
  136. if(argc > 4) {
  137. if((auth_pw & 1) && !strcasecmp(argv[4], "-p")) {
  138. auth_pw = 1;
  139. }
  140. if((auth_pw & 2) && !strcasecmp(argv[4], "-i")) {
  141. auth_pw = 2;
  142. }
  143. if((auth_pw & 4) && !strcasecmp(argv[4], "-k")) {
  144. auth_pw = 4;
  145. }
  146. }
  147. if(auth_pw & 1) {
  148. /* We could authenticate via password */
  149. if(libssh2_userauth_password(session, username, password)) {
  150. fprintf(stderr, "\tAuthentication by password failed!\n");
  151. goto shutdown;
  152. }
  153. else {
  154. fprintf(stderr, "\tAuthentication by password succeeded.\n");
  155. }
  156. }
  157. else if(auth_pw & 2) {
  158. /* Or via keyboard-interactive */
  159. if(libssh2_userauth_keyboard_interactive(session, username,
  160. &kbd_callback) ) {
  161. fprintf(stderr,
  162. "\tAuthentication by keyboard-interactive failed!\n");
  163. goto shutdown;
  164. }
  165. else {
  166. fprintf(stderr,
  167. "\tAuthentication by keyboard-interactive succeeded.\n");
  168. }
  169. }
  170. else if(auth_pw & 4) {
  171. /* Or by public key */
  172. if(libssh2_userauth_publickey_fromfile(session, username, keyfile1,
  173. keyfile2, password)) {
  174. fprintf(stderr, "\tAuthentication by public key failed!\n");
  175. goto shutdown;
  176. }
  177. else {
  178. fprintf(stderr, "\tAuthentication by public key succeeded.\n");
  179. }
  180. }
  181. else {
  182. fprintf(stderr, "No supported authentication methods found!\n");
  183. goto shutdown;
  184. }
  185. /* Request a shell */
  186. channel = libssh2_channel_open_session(session);
  187. if(!channel) {
  188. fprintf(stderr, "Unable to open a session\n");
  189. goto shutdown;
  190. }
  191. /* Some environment variables may be set,
  192. * It's up to the server which ones it'll allow though
  193. */
  194. libssh2_channel_setenv(channel, "FOO", "bar");
  195. /* Request a terminal with 'vanilla' terminal emulation
  196. * See /etc/termcap for more options
  197. */
  198. if(libssh2_channel_request_pty(channel, "vanilla")) {
  199. fprintf(stderr, "Failed requesting pty\n");
  200. goto skip_shell;
  201. }
  202. /* Open a SHELL on that pty */
  203. if(libssh2_channel_shell(channel)) {
  204. fprintf(stderr, "Unable to request shell on allocated pty\n");
  205. goto shutdown;
  206. }
  207. /* At this point the shell can be interacted with using
  208. * libssh2_channel_read()
  209. * libssh2_channel_read_stderr()
  210. * libssh2_channel_write()
  211. * libssh2_channel_write_stderr()
  212. *
  213. * Blocking mode may be (en|dis)abled with: libssh2_channel_set_blocking()
  214. * If the server send EOF, libssh2_channel_eof() will return non-0
  215. * To send EOF to the server use: libssh2_channel_send_eof()
  216. * A channel can be closed with: libssh2_channel_close()
  217. * A channel can be freed with: libssh2_channel_free()
  218. */
  219. skip_shell:
  220. if(channel) {
  221. libssh2_channel_free(channel);
  222. channel = NULL;
  223. }
  224. /* Other channel types are supported via:
  225. * libssh2_scp_send()
  226. * libssh2_scp_recv2()
  227. * libssh2_channel_direct_tcpip()
  228. */
  229. shutdown:
  230. libssh2_session_disconnect(session,
  231. "Normal Shutdown, Thank you for playing");
  232. libssh2_session_free(session);
  233. #ifdef WIN32
  234. closesocket(sock);
  235. #else
  236. close(sock);
  237. #endif
  238. fprintf(stderr, "all done!\n");
  239. libssh2_exit();
  240. return 0;
  241. }