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.
 
 
 
 

329 lines
9.2 KiB

  1. #include "libssh2_config.h"
  2. #include <libssh2.h>
  3. #ifdef WIN32
  4. #include <windows.h>
  5. #include <winsock2.h>
  6. #include <ws2tcpip.h>
  7. #else
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>
  11. #include <sys/time.h>
  12. #endif
  13. #include <fcntl.h>
  14. #include <errno.h>
  15. #include <stdio.h>
  16. #ifdef HAVE_STDLIB_H
  17. #include <stdlib.h>
  18. #endif
  19. #ifdef HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #include <sys/types.h>
  23. #ifdef HAVE_SYS_SELECT_H
  24. #include <sys/select.h>
  25. #endif
  26. #ifndef INADDR_NONE
  27. #define INADDR_NONE (in_addr_t)-1
  28. #endif
  29. const char *keyfile1 = "/home/username/.ssh/id_rsa.pub";
  30. const char *keyfile2 = "/home/username/.ssh/id_rsa";
  31. const char *username = "username";
  32. const char *password = "";
  33. const char *server_ip = "127.0.0.1";
  34. const char *remote_listenhost = "localhost"; /* resolved by the server */
  35. int remote_wantport = 2222;
  36. int remote_listenport;
  37. const char *local_destip = "127.0.0.1";
  38. int local_destport = 22;
  39. enum {
  40. AUTH_NONE = 0,
  41. AUTH_PASSWORD,
  42. AUTH_PUBLICKEY
  43. };
  44. int main(int argc, char *argv[])
  45. {
  46. int rc, i, auth = AUTH_NONE;
  47. struct sockaddr_in sin;
  48. socklen_t sinlen = sizeof(sin);
  49. const char *fingerprint;
  50. char *userauthlist;
  51. LIBSSH2_SESSION *session;
  52. LIBSSH2_LISTENER *listener = NULL;
  53. LIBSSH2_CHANNEL *channel = NULL;
  54. fd_set fds;
  55. struct timeval tv;
  56. ssize_t len, wr;
  57. char buf[16384];
  58. #ifdef WIN32
  59. SOCKET sock = INVALID_SOCKET, forwardsock = INVALID_SOCKET;
  60. WSADATA wsadata;
  61. int err;
  62. err = WSAStartup(MAKEWORD(2, 0), &wsadata);
  63. if(err != 0) {
  64. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  65. return 1;
  66. }
  67. #else
  68. int sock = -1, forwardsock = -1;
  69. #endif
  70. if(argc > 1)
  71. server_ip = argv[1];
  72. if(argc > 2)
  73. username = argv[2];
  74. if(argc > 3)
  75. password = argv[3];
  76. if(argc > 4)
  77. remote_listenhost = argv[4];
  78. if(argc > 5)
  79. remote_wantport = atoi(argv[5]);
  80. if(argc > 6)
  81. local_destip = argv[6];
  82. if(argc > 7)
  83. local_destport = atoi(argv[7]);
  84. rc = libssh2_init(0);
  85. if(rc != 0) {
  86. fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  87. return 1;
  88. }
  89. /* Connect to SSH server */
  90. sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  91. #ifdef WIN32
  92. if(sock == INVALID_SOCKET) {
  93. fprintf(stderr, "failed to open socket!\n");
  94. return -1;
  95. }
  96. #else
  97. if(sock == -1) {
  98. perror("socket");
  99. return -1;
  100. }
  101. #endif
  102. sin.sin_family = AF_INET;
  103. sin.sin_addr.s_addr = inet_addr(server_ip);
  104. if(INADDR_NONE == sin.sin_addr.s_addr) {
  105. perror("inet_addr");
  106. return -1;
  107. }
  108. sin.sin_port = htons(22);
  109. if(connect(sock, (struct sockaddr*)(&sin),
  110. sizeof(struct sockaddr_in)) != 0) {
  111. fprintf(stderr, "failed to connect!\n");
  112. return -1;
  113. }
  114. /* Create a session instance */
  115. session = libssh2_session_init();
  116. if(!session) {
  117. fprintf(stderr, "Could not initialize SSH session!\n");
  118. return -1;
  119. }
  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, "Error when starting up 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. fprintf(stderr, "\n");
  138. /* check what authentication methods are available */
  139. userauthlist = libssh2_userauth_list(session, username, strlen(username));
  140. fprintf(stderr, "Authentication methods: %s\n", userauthlist);
  141. if(strstr(userauthlist, "password"))
  142. auth |= AUTH_PASSWORD;
  143. if(strstr(userauthlist, "publickey"))
  144. auth |= AUTH_PUBLICKEY;
  145. /* check for options */
  146. if(argc > 8) {
  147. if((auth & AUTH_PASSWORD) && !strcasecmp(argv[8], "-p"))
  148. auth = AUTH_PASSWORD;
  149. if((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[8], "-k"))
  150. auth = AUTH_PUBLICKEY;
  151. }
  152. if(auth & AUTH_PASSWORD) {
  153. if(libssh2_userauth_password(session, username, password)) {
  154. fprintf(stderr, "Authentication by password failed.\n");
  155. goto shutdown;
  156. }
  157. }
  158. else if(auth & AUTH_PUBLICKEY) {
  159. if(libssh2_userauth_publickey_fromfile(session, username, keyfile1,
  160. keyfile2, password)) {
  161. fprintf(stderr, "\tAuthentication by public key failed!\n");
  162. goto shutdown;
  163. }
  164. fprintf(stderr, "\tAuthentication by public key succeeded.\n");
  165. }
  166. else {
  167. fprintf(stderr, "No supported authentication methods found!\n");
  168. goto shutdown;
  169. }
  170. fprintf(stderr, "Asking server to listen on remote %s:%d\n",
  171. remote_listenhost, remote_wantport);
  172. listener = libssh2_channel_forward_listen_ex(session, remote_listenhost,
  173. remote_wantport, &remote_listenport, 1);
  174. if(!listener) {
  175. fprintf(stderr, "Could not start the tcpip-forward listener!\n"
  176. "(Note that this can be a problem at the server!"
  177. " Please review the server logs.)\n");
  178. goto shutdown;
  179. }
  180. fprintf(stderr, "Server is listening on %s:%d\n", remote_listenhost,
  181. remote_listenport);
  182. fprintf(stderr, "Waiting for remote connection\n");
  183. channel = libssh2_channel_forward_accept(listener);
  184. if(!channel) {
  185. fprintf(stderr, "Could not accept connection!\n"
  186. "(Note that this can be a problem at the server!"
  187. " Please review the server logs.)\n");
  188. goto shutdown;
  189. }
  190. fprintf(stderr,
  191. "Accepted remote connection. Connecting to local server %s:%d\n",
  192. local_destip, local_destport);
  193. forwardsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  194. #ifdef WIN32
  195. if(forwardsock == INVALID_SOCKET) {
  196. fprintf(stderr, "failed to open forward socket!\n");
  197. goto shutdown;
  198. }
  199. #else
  200. if(forwardsock == -1) {
  201. perror("socket");
  202. goto shutdown;
  203. }
  204. #endif
  205. sin.sin_family = AF_INET;
  206. sin.sin_port = htons(local_destport);
  207. sin.sin_addr.s_addr = inet_addr(local_destip);
  208. if(INADDR_NONE == sin.sin_addr.s_addr) {
  209. perror("inet_addr");
  210. goto shutdown;
  211. }
  212. if(-1 == connect(forwardsock, (struct sockaddr *)&sin, sinlen)) {
  213. perror("connect");
  214. goto shutdown;
  215. }
  216. fprintf(stderr, "Forwarding connection from remote %s:%d to local %s:%d\n",
  217. remote_listenhost, remote_listenport, local_destip, local_destport);
  218. /* Must use non-blocking IO hereafter due to the current libssh2 API */
  219. libssh2_session_set_blocking(session, 0);
  220. while(1) {
  221. FD_ZERO(&fds);
  222. FD_SET(forwardsock, &fds);
  223. tv.tv_sec = 0;
  224. tv.tv_usec = 100000;
  225. rc = select(forwardsock + 1, &fds, NULL, NULL, &tv);
  226. if(-1 == rc) {
  227. perror("select");
  228. goto shutdown;
  229. }
  230. if(rc && FD_ISSET(forwardsock, &fds)) {
  231. len = recv(forwardsock, buf, sizeof(buf), 0);
  232. if(len < 0) {
  233. perror("read");
  234. goto shutdown;
  235. }
  236. else if(0 == len) {
  237. fprintf(stderr, "The local server at %s:%d disconnected!\n",
  238. local_destip, local_destport);
  239. goto shutdown;
  240. }
  241. wr = 0;
  242. do {
  243. i = libssh2_channel_write(channel, buf, len);
  244. if(i < 0) {
  245. fprintf(stderr, "libssh2_channel_write: %d\n", i);
  246. goto shutdown;
  247. }
  248. wr += i;
  249. } while(i > 0 && wr < len);
  250. }
  251. while(1) {
  252. len = libssh2_channel_read(channel, buf, sizeof(buf));
  253. if(LIBSSH2_ERROR_EAGAIN == len)
  254. break;
  255. else if(len < 0) {
  256. fprintf(stderr, "libssh2_channel_read: %d", (int)len);
  257. goto shutdown;
  258. }
  259. wr = 0;
  260. while(wr < len) {
  261. i = send(forwardsock, buf + wr, len - wr, 0);
  262. if(i <= 0) {
  263. perror("write");
  264. goto shutdown;
  265. }
  266. wr += i;
  267. }
  268. if(libssh2_channel_eof(channel)) {
  269. fprintf(stderr, "The remote client at %s:%d disconnected!\n",
  270. remote_listenhost, remote_listenport);
  271. goto shutdown;
  272. }
  273. }
  274. }
  275. shutdown:
  276. #ifdef WIN32
  277. closesocket(forwardsock);
  278. #else
  279. close(forwardsock);
  280. #endif
  281. if(channel)
  282. libssh2_channel_free(channel);
  283. if(listener)
  284. libssh2_channel_forward_cancel(listener);
  285. libssh2_session_disconnect(session, "Client disconnecting normally");
  286. libssh2_session_free(session);
  287. #ifdef WIN32
  288. closesocket(sock);
  289. #else
  290. close(sock);
  291. #endif
  292. libssh2_exit();
  293. return 0;
  294. }