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.
 
 
 
 

344 lines
9.3 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 *local_listenip = "127.0.0.1";
  35. unsigned int local_listenport = 2222;
  36. const char *remote_desthost = "localhost"; /* resolved by the server */
  37. unsigned int remote_destport = 22;
  38. enum {
  39. AUTH_NONE = 0,
  40. AUTH_PASSWORD,
  41. AUTH_PUBLICKEY
  42. };
  43. int main(int argc, char *argv[])
  44. {
  45. int rc, i, auth = AUTH_NONE;
  46. struct sockaddr_in sin;
  47. socklen_t sinlen;
  48. const char *fingerprint;
  49. char *userauthlist;
  50. LIBSSH2_SESSION *session;
  51. LIBSSH2_CHANNEL *channel = NULL;
  52. const char *shost;
  53. unsigned int sport;
  54. fd_set fds;
  55. struct timeval tv;
  56. ssize_t len, wr;
  57. char buf[16384];
  58. #ifdef WIN32
  59. char sockopt;
  60. SOCKET sock = INVALID_SOCKET;
  61. SOCKET listensock = INVALID_SOCKET, forwardsock = INVALID_SOCKET;
  62. WSADATA wsadata;
  63. int err;
  64. err = WSAStartup(MAKEWORD(2, 0), &wsadata);
  65. if(err != 0) {
  66. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  67. return 1;
  68. }
  69. #else
  70. int sockopt, sock = -1;
  71. int listensock = -1, forwardsock = -1;
  72. #endif
  73. if(argc > 1)
  74. server_ip = argv[1];
  75. if(argc > 2)
  76. username = argv[2];
  77. if(argc > 3)
  78. password = argv[3];
  79. if(argc > 4)
  80. local_listenip = argv[4];
  81. if(argc > 5)
  82. local_listenport = atoi(argv[5]);
  83. if(argc > 6)
  84. remote_desthost = argv[6];
  85. if(argc > 7)
  86. remote_destport = atoi(argv[7]);
  87. rc = libssh2_init(0);
  88. if(rc) {
  89. fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  90. return 1;
  91. }
  92. /* Connect to SSH server */
  93. sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  94. #ifdef WIN32
  95. if(sock == INVALID_SOCKET) {
  96. fprintf(stderr, "failed to open socket!\n");
  97. return -1;
  98. }
  99. #else
  100. if(sock == -1) {
  101. perror("socket");
  102. return -1;
  103. }
  104. #endif
  105. sin.sin_family = AF_INET;
  106. sin.sin_addr.s_addr = inet_addr(server_ip);
  107. if(INADDR_NONE == sin.sin_addr.s_addr) {
  108. perror("inet_addr");
  109. return -1;
  110. }
  111. sin.sin_port = htons(22);
  112. if(connect(sock, (struct sockaddr*)(&sin),
  113. sizeof(struct sockaddr_in)) != 0) {
  114. fprintf(stderr, "failed to connect!\n");
  115. return -1;
  116. }
  117. /* Create a session instance */
  118. session = libssh2_session_init();
  119. if(!session) {
  120. fprintf(stderr, "Could not initialize SSH session!\n");
  121. return -1;
  122. }
  123. /* ... start it up. This will trade welcome banners, exchange keys,
  124. * and setup crypto, compression, and MAC layers
  125. */
  126. rc = libssh2_session_handshake(session, sock);
  127. if(rc) {
  128. fprintf(stderr, "Error when starting up SSH session: %d\n", rc);
  129. return -1;
  130. }
  131. /* At this point we havn't yet authenticated. The first thing to do
  132. * is check the hostkey's fingerprint against our known hosts Your app
  133. * may have it hard coded, may go to a file, may present it to the
  134. * user, that's your call
  135. */
  136. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  137. fprintf(stderr, "Fingerprint: ");
  138. for(i = 0; i < 20; i++)
  139. fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
  140. fprintf(stderr, "\n");
  141. /* check what authentication methods are available */
  142. userauthlist = libssh2_userauth_list(session, username, strlen(username));
  143. fprintf(stderr, "Authentication methods: %s\n", userauthlist);
  144. if(strstr(userauthlist, "password"))
  145. auth |= AUTH_PASSWORD;
  146. if(strstr(userauthlist, "publickey"))
  147. auth |= AUTH_PUBLICKEY;
  148. /* check for options */
  149. if(argc > 8) {
  150. if((auth & AUTH_PASSWORD) && !strcasecmp(argv[8], "-p"))
  151. auth = AUTH_PASSWORD;
  152. if((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[8], "-k"))
  153. auth = AUTH_PUBLICKEY;
  154. }
  155. if(auth & AUTH_PASSWORD) {
  156. if(libssh2_userauth_password(session, username, password)) {
  157. fprintf(stderr, "Authentication by password failed.\n");
  158. goto shutdown;
  159. }
  160. }
  161. else if(auth & AUTH_PUBLICKEY) {
  162. if(libssh2_userauth_publickey_fromfile(session, username, keyfile1,
  163. keyfile2, password)) {
  164. fprintf(stderr, "\tAuthentication by public key failed!\n");
  165. goto shutdown;
  166. }
  167. fprintf(stderr, "\tAuthentication by public key succeeded.\n");
  168. }
  169. else {
  170. fprintf(stderr, "No supported authentication methods found!\n");
  171. goto shutdown;
  172. }
  173. listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  174. #ifdef WIN32
  175. if(listensock == INVALID_SOCKET) {
  176. fprintf(stderr, "failed to open listen socket!\n");
  177. return -1;
  178. }
  179. #else
  180. if(listensock == -1) {
  181. perror("socket");
  182. return -1;
  183. }
  184. #endif
  185. sin.sin_family = AF_INET;
  186. sin.sin_port = htons(local_listenport);
  187. sin.sin_addr.s_addr = inet_addr(local_listenip);
  188. if(INADDR_NONE == sin.sin_addr.s_addr) {
  189. perror("inet_addr");
  190. goto shutdown;
  191. }
  192. sockopt = 1;
  193. setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &sockopt,
  194. sizeof(sockopt));
  195. sinlen = sizeof(sin);
  196. if(-1 == bind(listensock, (struct sockaddr *)&sin, sinlen)) {
  197. perror("bind");
  198. goto shutdown;
  199. }
  200. if(-1 == listen(listensock, 2)) {
  201. perror("listen");
  202. goto shutdown;
  203. }
  204. fprintf(stderr, "Waiting for TCP connection on %s:%d...\n",
  205. inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
  206. forwardsock = accept(listensock, (struct sockaddr *)&sin, &sinlen);
  207. #ifdef WIN32
  208. if(forwardsock == INVALID_SOCKET) {
  209. fprintf(stderr, "failed to accept forward socket!\n");
  210. goto shutdown;
  211. }
  212. #else
  213. if(forwardsock == -1) {
  214. perror("accept");
  215. goto shutdown;
  216. }
  217. #endif
  218. shost = inet_ntoa(sin.sin_addr);
  219. sport = ntohs(sin.sin_port);
  220. fprintf(stderr, "Forwarding connection from %s:%d here to remote %s:%d\n",
  221. shost, sport, remote_desthost, remote_destport);
  222. channel = libssh2_channel_direct_tcpip_ex(session, remote_desthost,
  223. remote_destport, shost, sport);
  224. if(!channel) {
  225. fprintf(stderr, "Could not open the direct-tcpip channel!\n"
  226. "(Note that this can be a problem at the server!"
  227. " Please review the server logs.)\n");
  228. goto shutdown;
  229. }
  230. /* Must use non-blocking IO hereafter due to the current libssh2 API */
  231. libssh2_session_set_blocking(session, 0);
  232. while(1) {
  233. FD_ZERO(&fds);
  234. FD_SET(forwardsock, &fds);
  235. tv.tv_sec = 0;
  236. tv.tv_usec = 100000;
  237. rc = select(forwardsock + 1, &fds, NULL, NULL, &tv);
  238. if(-1 == rc) {
  239. perror("select");
  240. goto shutdown;
  241. }
  242. if(rc && FD_ISSET(forwardsock, &fds)) {
  243. len = recv(forwardsock, buf, sizeof(buf), 0);
  244. if(len < 0) {
  245. perror("read");
  246. goto shutdown;
  247. }
  248. else if(0 == len) {
  249. fprintf(stderr, "The client at %s:%d disconnected!\n", shost,
  250. sport);
  251. goto shutdown;
  252. }
  253. wr = 0;
  254. while(wr < len) {
  255. i = libssh2_channel_write(channel, buf + wr, len - wr);
  256. if(LIBSSH2_ERROR_EAGAIN == i) {
  257. continue;
  258. }
  259. if(i < 0) {
  260. fprintf(stderr, "libssh2_channel_write: %d\n", i);
  261. goto shutdown;
  262. }
  263. wr += i;
  264. }
  265. }
  266. while(1) {
  267. len = libssh2_channel_read(channel, buf, sizeof(buf));
  268. if(LIBSSH2_ERROR_EAGAIN == len)
  269. break;
  270. else if(len < 0) {
  271. fprintf(stderr, "libssh2_channel_read: %d", (int)len);
  272. goto shutdown;
  273. }
  274. wr = 0;
  275. while(wr < len) {
  276. i = send(forwardsock, buf + wr, len - wr, 0);
  277. if(i <= 0) {
  278. perror("write");
  279. goto shutdown;
  280. }
  281. wr += i;
  282. }
  283. if(libssh2_channel_eof(channel)) {
  284. fprintf(stderr, "The server at %s:%d disconnected!\n",
  285. remote_desthost, remote_destport);
  286. goto shutdown;
  287. }
  288. }
  289. }
  290. shutdown:
  291. #ifdef WIN32
  292. closesocket(forwardsock);
  293. closesocket(listensock);
  294. #else
  295. close(forwardsock);
  296. close(listensock);
  297. #endif
  298. if(channel)
  299. libssh2_channel_free(channel);
  300. libssh2_session_disconnect(session, "Client disconnecting normally");
  301. libssh2_session_free(session);
  302. #ifdef WIN32
  303. closesocket(sock);
  304. #else
  305. close(sock);
  306. #endif
  307. libssh2_exit();
  308. return 0;
  309. }