6
#include "libssh2_setup.h"
11
#define recv(s, b, l, f) recv((s), (b), (int)(l), (f))
12
#define send(s, b, l, f) send((s), (b), (int)(l), (f))
15
#ifdef HAVE_SYS_SOCKET_H
16
#include <sys/socket.h>
21
#ifdef HAVE_NETINET_IN_H
22
#include <netinet/in.h>
24
#ifdef HAVE_ARPA_INET_H
33
#define INADDR_NONE (in_addr_t)~0
36
static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
37
static const char *privkey = "/home/username/.ssh/id_rsa";
38
static const char *username = "username";
39
static const char *password = "";
41
static const char *server_ip = "127.0.0.1";
43
static const char *local_listenip = "127.0.0.1";
44
static int local_listenport = 2222;
46
static const char *remote_desthost = "localhost";
47
static int remote_destport = 22;
55
int main(int argc, char *argv[])
57
int i, auth = AUTH_NONE;
58
struct sockaddr_in sin;
60
const char *fingerprint;
63
LIBSSH2_SESSION *session = NULL;
64
LIBSSH2_CHANNEL *channel = NULL;
70
libssh2_socket_t sock;
71
libssh2_socket_t listensock = LIBSSH2_INVALID_SOCKET;
72
libssh2_socket_t forwardsock = LIBSSH2_INVALID_SOCKET;
78
rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
80
fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
94
local_listenip = argv[4];
96
local_listenport = atoi(argv[5]);
98
remote_desthost = argv[6];
100
remote_destport = atoi(argv[7]);
102
rc = libssh2_init(0);
104
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
109
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
110
if(sock == LIBSSH2_INVALID_SOCKET) {
111
fprintf(stderr, "failed to open socket.\n");
115
sin.sin_family = AF_INET;
116
sin.sin_addr.s_addr = inet_addr(server_ip);
117
if(INADDR_NONE == sin.sin_addr.s_addr) {
118
fprintf(stderr, "inet_addr: Invalid IP address '%s'\n", server_ip);
121
sin.sin_port = htons(22);
122
if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
123
fprintf(stderr, "Failed to connect to %s.\n", inet_ntoa(sin.sin_addr));
128
session = libssh2_session_init();
130
fprintf(stderr, "Could not initialize SSH session.\n");
137
rc = libssh2_session_handshake(session, sock);
139
fprintf(stderr, "Error when starting up SSH session: %d\n", rc);
148
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
149
fprintf(stderr, "Fingerprint: ");
150
for(i = 0; i < 20; i++)
151
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
152
fprintf(stderr, "\n");
155
userauthlist = libssh2_userauth_list(session, username,
156
(unsigned int)strlen(username));
158
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
159
if(strstr(userauthlist, "password"))
160
auth |= AUTH_PASSWORD;
161
if(strstr(userauthlist, "publickey"))
162
auth |= AUTH_PUBLICKEY;
166
if((auth & AUTH_PASSWORD) && !strcmp(argv[8], "-p"))
167
auth = AUTH_PASSWORD;
168
if((auth & AUTH_PUBLICKEY) && !strcmp(argv[8], "-k"))
169
auth = AUTH_PUBLICKEY;
172
if(auth & AUTH_PASSWORD) {
173
if(libssh2_userauth_password(session, username, password)) {
174
fprintf(stderr, "Authentication by password failed.\n");
178
else if(auth & AUTH_PUBLICKEY) {
179
if(libssh2_userauth_publickey_fromfile(session, username,
182
fprintf(stderr, "Authentication by public key failed.\n");
186
fprintf(stderr, "Authentication by public key succeeded.\n");
190
fprintf(stderr, "No supported authentication methods found.\n");
195
listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
196
if(listensock == LIBSSH2_INVALID_SOCKET) {
197
fprintf(stderr, "failed to open listen socket.\n");
201
sin.sin_family = AF_INET;
202
sin.sin_port = htons((unsigned short)local_listenport);
203
sin.sin_addr.s_addr = inet_addr(local_listenip);
204
if(INADDR_NONE == sin.sin_addr.s_addr) {
205
fprintf(stderr, "failed in inet_addr().\n");
209
setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &sockopt,
211
sinlen = sizeof(sin);
212
if(-1 == bind(listensock, (struct sockaddr *)&sin, sinlen)) {
213
fprintf(stderr, "failed to bind().\n");
216
if(-1 == listen(listensock, 2)) {
217
fprintf(stderr, "failed to listen().\n");
221
fprintf(stderr, "Waiting for TCP connection on %s:%d...\n",
222
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
224
forwardsock = accept(listensock, (struct sockaddr *)&sin, &sinlen);
225
if(forwardsock == LIBSSH2_INVALID_SOCKET) {
226
fprintf(stderr, "failed to accept forward socket.\n");
230
shost = inet_ntoa(sin.sin_addr);
231
sport = ntohs(sin.sin_port);
233
fprintf(stderr, "Forwarding connection from %s:%d here to remote %s:%d\n",
234
shost, sport, remote_desthost, remote_destport);
236
channel = libssh2_channel_direct_tcpip_ex(session, remote_desthost,
237
remote_destport, shost, sport);
239
fprintf(stderr, "Could not open the direct-tcpip channel.\n"
240
"(Note that this can be a problem at the server."
241
" Please review the server logs.)\n");
246
libssh2_session_set_blocking(session, 0);
252
#pragma GCC diagnostic push
253
#pragma GCC diagnostic ignored "-Wsign-conversion"
255
FD_SET(forwardsock, &fds);
257
#pragma GCC diagnostic pop
261
rc = select((int)(forwardsock + 1), &fds, NULL, NULL, &tv);
263
fprintf(stderr, "failed to select().\n");
267
#pragma GCC diagnostic push
268
#pragma GCC diagnostic ignored "-Wsign-conversion"
270
if(rc && FD_ISSET(forwardsock, &fds)) {
272
#pragma GCC diagnostic pop
274
len = recv(forwardsock, buf, sizeof(buf), 0);
276
fprintf(stderr, "failed to recv().\n");
280
fprintf(stderr, "The client at %s:%d disconnected.\n", shost,
286
ssize_t nwritten = libssh2_channel_write(channel,
289
if(nwritten == LIBSSH2_ERROR_EAGAIN) {
293
fprintf(stderr, "libssh2_channel_write: %ld\n",
301
len = libssh2_channel_read(channel, buf, sizeof(buf));
302
if(LIBSSH2_ERROR_EAGAIN == len)
305
fprintf(stderr, "libssh2_channel_read: %ld", (long)len);
310
ssize_t nsent = send(forwardsock, buf + wr,
311
(size_t)(len - wr), 0);
313
fprintf(stderr, "failed to send().\n");
318
if(libssh2_channel_eof(channel)) {
319
fprintf(stderr, "The server at %s:%d disconnected.\n",
320
remote_desthost, remote_destport);
328
if(forwardsock != LIBSSH2_INVALID_SOCKET) {
329
shutdown(forwardsock, 2);
330
LIBSSH2_SOCKET_CLOSE(forwardsock);
333
if(listensock != LIBSSH2_INVALID_SOCKET) {
334
shutdown(listensock, 2);
335
LIBSSH2_SOCKET_CLOSE(listensock);
339
libssh2_channel_free(channel);
342
libssh2_session_disconnect(session, "Normal Shutdown");
343
libssh2_session_free(session);
346
if(sock != LIBSSH2_INVALID_SOCKET) {
348
LIBSSH2_SOCKET_CLOSE(sock);