1
/* Copyright (C) The libssh2 project and its contributors.
3
* SPDX-License-Identifier: BSD-3-Clause
6
#include "libssh2_setup.h"
10
#include <ws2tcpip.h> /* for socklen_t */
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
/* resolved by the server */
44
static const char *remote_listenhost = "localhost";
46
static int remote_wantport = 2222;
47
static int remote_listenport;
49
static const char *local_destip = "127.0.0.1";
50
static int local_destport = 22;
58
int main(int argc, char *argv[])
60
int i, auth = AUTH_NONE;
61
struct sockaddr_in sin;
62
socklen_t sinlen = sizeof(sin);
63
const char *fingerprint;
66
LIBSSH2_SESSION *session = NULL;
67
LIBSSH2_LISTENER *listener = NULL;
68
LIBSSH2_CHANNEL *channel = NULL;
72
libssh2_socket_t sock;
73
libssh2_socket_t forwardsock = LIBSSH2_INVALID_SOCKET;
78
rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
80
fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
92
remote_listenhost = argv[4];
94
remote_wantport = atoi(argv[5]);
96
local_destip = argv[6];
98
local_destport = atoi(argv[7]);
100
rc = libssh2_init(0);
102
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
106
/* Connect to SSH server */
107
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
108
if(sock == LIBSSH2_INVALID_SOCKET) {
109
fprintf(stderr, "failed to open socket.\n");
113
sin.sin_family = AF_INET;
114
sin.sin_addr.s_addr = inet_addr(server_ip);
115
if(INADDR_NONE == sin.sin_addr.s_addr) {
116
fprintf(stderr, "inet_addr: Invalid IP address '%s'\n", server_ip);
119
sin.sin_port = htons(22);
120
if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
121
fprintf(stderr, "Failed to connect to %s.\n", inet_ntoa(sin.sin_addr));
125
/* Create a session instance */
126
session = libssh2_session_init();
128
fprintf(stderr, "Could not initialize SSH session.\n");
132
/* ... start it up. This will trade welcome banners, exchange keys,
133
* and setup crypto, compression, and MAC layers
135
rc = libssh2_session_handshake(session, sock);
137
fprintf(stderr, "Error when starting up SSH session: %d\n", rc);
141
/* At this point we have not yet authenticated. The first thing to do
142
* is check the hostkey's fingerprint against our known hosts Your app
143
* may have it hard coded, may go to a file, may present it to the
144
* user, that's your call
146
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
147
fprintf(stderr, "Fingerprint: ");
148
for(i = 0; i < 20; i++)
149
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
150
fprintf(stderr, "\n");
152
/* check what authentication methods are available */
153
userauthlist = libssh2_userauth_list(session, username,
154
(unsigned int)strlen(username));
156
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
157
if(strstr(userauthlist, "password"))
158
auth |= AUTH_PASSWORD;
159
if(strstr(userauthlist, "publickey"))
160
auth |= AUTH_PUBLICKEY;
162
/* check for options */
164
if((auth & AUTH_PASSWORD) && !strcmp(argv[8], "-p"))
165
auth = AUTH_PASSWORD;
166
if((auth & AUTH_PUBLICKEY) && !strcmp(argv[8], "-k"))
167
auth = AUTH_PUBLICKEY;
170
if(auth & AUTH_PASSWORD) {
171
if(libssh2_userauth_password(session, username, password)) {
172
fprintf(stderr, "Authentication by password failed.\n");
176
else if(auth & AUTH_PUBLICKEY) {
177
if(libssh2_userauth_publickey_fromfile(session, username,
180
fprintf(stderr, "Authentication by public key failed.\n");
184
fprintf(stderr, "Authentication by public key succeeded.\n");
188
fprintf(stderr, "No supported authentication methods found.\n");
193
fprintf(stderr, "Asking server to listen on remote %s:%d\n",
194
remote_listenhost, remote_wantport);
196
listener = libssh2_channel_forward_listen_ex(session, remote_listenhost,
198
&remote_listenport, 1);
200
fprintf(stderr, "Could not start the tcpip-forward listener.\n"
201
"(Note that this can be a problem at the server."
202
" Please review the server logs.)\n");
206
fprintf(stderr, "Server is listening on %s:%d\n", remote_listenhost,
209
fprintf(stderr, "Waiting for remote connection\n");
210
channel = libssh2_channel_forward_accept(listener);
212
fprintf(stderr, "Could not accept connection.\n"
213
"(Note that this can be a problem at the server."
214
" Please review the server logs.)\n");
219
"Accepted remote connection. Connecting to local server %s:%d\n",
220
local_destip, local_destport);
221
forwardsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
222
if(forwardsock == LIBSSH2_INVALID_SOCKET) {
223
fprintf(stderr, "failed to open forward socket.\n");
227
sin.sin_family = AF_INET;
228
sin.sin_port = htons((unsigned short)local_destport);
229
sin.sin_addr.s_addr = inet_addr(local_destip);
230
if(INADDR_NONE == sin.sin_addr.s_addr) {
231
fprintf(stderr, "failed in inet_addr().\n");
234
if(-1 == connect(forwardsock, (struct sockaddr *)&sin, sinlen)) {
235
fprintf(stderr, "failed to connect().\n");
239
fprintf(stderr, "Forwarding connection from remote %s:%d to local %s:%d\n",
240
remote_listenhost, remote_listenport,
241
local_destip, local_destport);
243
/* Must use non-blocking IO hereafter due to the current libssh2 API */
244
libssh2_session_set_blocking(session, 0);
250
#pragma GCC diagnostic push
251
#pragma GCC diagnostic ignored "-Wsign-conversion"
253
FD_SET(forwardsock, &fds);
255
#pragma GCC diagnostic pop
259
rc = select((int)(forwardsock + 1), &fds, NULL, NULL, &tv);
261
fprintf(stderr, "failed to select().\n");
265
#pragma GCC diagnostic push
266
#pragma GCC diagnostic ignored "-Wsign-conversion"
268
if(rc && FD_ISSET(forwardsock, &fds)) {
270
#pragma GCC diagnostic pop
273
len = recv(forwardsock, buf, sizeof(buf), 0);
275
fprintf(stderr, "failed to recv().\n");
279
fprintf(stderr, "The local server at %s:%d disconnected.\n",
280
local_destip, local_destport);
285
nwritten = libssh2_channel_write(channel, buf, (size_t)len);
287
fprintf(stderr, "libssh2_channel_write: %ld\n",
292
} while(nwritten > 0 && wr < len);
296
len = libssh2_channel_read(channel, buf, sizeof(buf));
297
if(LIBSSH2_ERROR_EAGAIN == len)
300
fprintf(stderr, "libssh2_channel_read: %ld",
306
nsent = send(forwardsock, buf + wr, (size_t)(len - wr), 0);
308
fprintf(stderr, "failed to send().\n");
313
if(libssh2_channel_eof(channel)) {
314
fprintf(stderr, "The remote client at %s:%d disconnected.\n",
315
remote_listenhost, remote_listenport);
323
if(forwardsock != LIBSSH2_INVALID_SOCKET) {
324
shutdown(forwardsock, 2);
325
LIBSSH2_SOCKET_CLOSE(forwardsock);
329
libssh2_channel_free(channel);
332
libssh2_channel_forward_cancel(listener);
335
libssh2_session_disconnect(session, "Normal Shutdown");
336
libssh2_session_free(session);
339
if(sock != LIBSSH2_INVALID_SOCKET) {
341
LIBSSH2_SOCKET_CLOSE(sock);