1
/* Copyright (C) The libssh2 project and its contributors.
3
* SPDX-License-Identifier: BSD-3-Clause
5
* Self test, based on example/ssh2.c.
8
#include "libssh2_setup.h"
11
#ifdef HAVE_SYS_SOCKET_H
12
#include <sys/socket.h>
17
#ifdef HAVE_NETINET_IN_H
18
#include <netinet/in.h>
20
#ifdef HAVE_ARPA_INET_H
25
#include <stdlib.h> /* for getenv() */
27
static const char *hostname = "127.0.0.1";
28
static const int port_number = 4711;
29
static const char *pubkey = "key_rsa.pub";
30
static const char *privkey = "key_rsa";
31
static const char *username = "username";
32
static const char *password = "password";
34
static void portable_sleep(unsigned int seconds)
43
int main(int argc, char *argv[])
46
libssh2_socket_t sock;
48
struct sockaddr_in sin;
49
const char *fingerprint;
52
LIBSSH2_SESSION *session = NULL;
53
LIBSSH2_CHANNEL *channel;
59
rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
61
fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
70
#define LIBSSH2_FALLBACK_USER_ENV "USERNAME"
72
#define LIBSSH2_FALLBACK_USER_ENV "LOGNAME"
76
username = getenv("USER");
77
else if(getenv(LIBSSH2_FALLBACK_USER_ENV))
78
username = getenv(LIBSSH2_FALLBACK_USER_ENV);
81
privkey = getenv("PRIVKEY");
84
pubkey = getenv("PUBKEY");
86
hostaddr = inet_addr(hostname);
87
if(hostaddr == (uint32_t)(-1)) {
88
fprintf(stderr, "Failed to convert %s host address\n", hostname);
94
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
100
sock = socket(AF_INET, SOCK_STREAM, 0);
101
if(sock == LIBSSH2_INVALID_SOCKET) {
102
fprintf(stderr, "failed to create socket.\n");
106
sin.sin_family = AF_INET;
107
sin.sin_port = htons((unsigned short)port_number);
108
sin.sin_addr.s_addr = hostaddr;
110
for(counter = 0; counter < 3; ++counter) {
111
if(connect(sock, (struct sockaddr*)(&sin),
112
sizeof(struct sockaddr_in))) {
114
"Connection to %s:%d attempt #%d failed: retrying...\n",
115
hostname, port_number, counter);
116
portable_sleep(1 + 2*counter);
122
if(sock == LIBSSH2_INVALID_SOCKET) {
123
fprintf(stderr, "Failed to connect to %s:%d\n",
124
hostname, port_number);
128
/* Create a session instance and start it up. This will trade welcome
129
* banners, exchange keys, and setup crypto, compression, and MAC layers
131
session = libssh2_session_init();
133
fprintf(stderr, "Could not initialize SSH session.\n");
137
if(getenv("FIXTURE_TRACE_ALL_CONNECT") ||
138
getenv("FIXTURE_TRACE_ALL")) {
139
libssh2_trace(session, ~0);
140
fprintf(stdout, "Trace all enabled.\n");
143
libssh2_session_set_blocking(session, 1);
146
int retries = 0, retry = 0;
148
/* FIXME: Retry tests with WinCNG due to flakiness in hostkey
149
verification: https://github.com/libssh2/libssh2/issues/804 */
153
rc = libssh2_session_handshake(session, sock);
157
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
160
rc != LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE ||
165
fprintf(stderr, "Retrying... %d / %d\n", retry, retries);
171
/* At this point we have not yet authenticated. The first thing to do
172
* is check the hostkey's fingerprint against our known hosts Your app
173
* may have it hard coded, may go to a file, may present it to the
174
* user, that's your call
176
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
177
fprintf(stderr, "Fingerprint: ");
178
for(i = 0; i < 20; i++) {
179
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
181
fprintf(stderr, "\n");
183
/* check what authentication methods are available */
184
userauthlist = libssh2_userauth_list(session, username,
185
(unsigned int)strlen(username));
187
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
188
if(strstr(userauthlist, "password")) {
191
if(strstr(userauthlist, "keyboard-interactive")) {
194
if(strstr(userauthlist, "publickey")) {
199
/* Authenticate by public key */
200
if(libssh2_userauth_publickey_fromfile(session, username,
203
fprintf(stderr, "Authentication by public key failed.\n");
207
fprintf(stderr, "Authentication by public key succeeded.\n");
211
fprintf(stderr, "No supported authentication methods found.\n");
216
/* Request a session channel on which to run a shell */
217
channel = libssh2_channel_open_session(session);
219
fprintf(stderr, "Unable to open a session\n");
223
/* Some environment variables may be set,
224
* It's up to the server which ones it'll allow though
226
libssh2_channel_setenv(channel, "FOO", "bar");
228
/* Request a terminal with 'vanilla' terminal emulation
229
* See /etc/termcap for more options. This is useful when opening
230
* an interactive shell.
232
if(libssh2_channel_request_pty(channel, "vanilla")) {
233
fprintf(stderr, "Failed requesting pty\n");
237
/* Open a SHELL on that pty */
238
if(libssh2_channel_shell(channel)) {
239
fprintf(stderr, "Unable to request shell on allocated pty\n");
248
libssh2_channel_free(channel);
255
libssh2_session_disconnect(session, "Normal Shutdown");
256
libssh2_session_free(session);
259
if(sock != LIBSSH2_INVALID_SOCKET) {
260
shutdown(sock, 2 /* SHUT_RDWR */);
268
fprintf(stderr, "all done\n");