1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <poll.h>
#include <unistd.h>
#include "util.h"
#include "sshnc.h"
static bool prompt_yn(const char *text) {
printf("%s", text);
fflush(stdout);
bool response;
char *line = NULL;
size_t linelen = 0;
while (true) {
ssize_t nr = getline(&line, &linelen, stdin);
if (nr == -1) {
perror("getline");
exit(1);
}
while (nr > 0 && isspace(line[nr - 1])) nr--;
for (ssize_t i = 0; i < nr; i++) line[i] = tolower(line[i]);
if ((nr == 1 && line[0] == 'y') || (nr == 3 && memcmp(line, "yes", 3) == 0)) {
response = true;
break;
}
if ((nr == 1 && line[0] == 'n') || (nr == 2 && memcmp(line, "no", 2) == 0)) {
response = false;
break;
}
printf("Please answer with 'y', 'n', 'yes' or 'no'. [y/n]");
fflush(stdout);
}
free(line);
return response;
}
static bool hostkey_checker(const unsigned char *hash, size_t length, void *userdata) {
(void)userdata;
printf("Server host key hash: %s\n", sshnc_print_hash(hash, length));
bool response = prompt_yn(
"Does this hash match the one given to you by the server administrator, or by a\n"
"member that you trust and is already connected to the server? [y/n] ");
if (!response) {
printf("Disconnecting.\n");
}
return response;
}
int main(int argc, char **argv) {
const char *server_host = NULL;
int port = 2222;
if (argc != 2) {
fprintf(stderr, "Usage: %s <server[:port]>\n", argv[0]);
fprintf(stderr, "If port is not specified, %d is assumed.\n", port);
return 1;
}
if (!parse_host_port(argv[1], &server_host, &port)) {
fprintf(stderr, "Cannot parse host:port from argument '%s'\n", argv[1]);
return 1;
}
struct sshnc_client *client;
enum sshnc_retval ret = sshnc_connect(
server_host, port, "tomsg", "tomsg", hostkey_checker, NULL, &client);
if (ret != SSHNC_OK) {
fprintf(stderr, "Could not connect: %s\n", sshnc_strerror(ret));
return 1;
}
struct pollfd polls[2];
polls[0] = (struct pollfd){
.fd = sshnc_poll_fd(client),
.events = POLLIN,
};
polls[1] = (struct pollfd){
.fd = STDIN_FILENO,
.events = POLLIN,
};
while (true) {
int pollret = poll(polls, sizeof polls / sizeof polls[0], -1);
if (pollret < 0) {
perror("poll");
goto cleanup;
}
if (polls[0].revents & (POLLERR | POLLNVAL)) {
fprintf(stderr, "Error reading from SSH socket\n");
goto cleanup;
}
if (polls[1].revents & (POLLERR | POLLNVAL)) {
fprintf(stderr, "Error reading from stdin\n");
goto cleanup;
}
if (polls[0].revents & (POLLIN | POLLHUP)) {
char buffer[4096];
size_t length = 0;
ret = sshnc_maybe_recv(client, sizeof buffer, buffer, &length);
if (ret == SSHNC_OK) {
fwrite(buffer, 1, length, stdout);
} else if (ret == SSHNC_EOF) {
break;
} else if (ret != SSHNC_AGAIN) {
fprintf(stderr, "Error on SSH recv: %s\n", sshnc_strerror(ret));
goto cleanup;
}
}
if (polls[1].revents & (POLLIN | POLLHUP)) {
char buffer[4096];
ssize_t nr = read(STDIN_FILENO, buffer, sizeof buffer);
if (nr < 0) {
perror("Error reading from stdin");
goto cleanup;
}
if (nr == 0) {
break;
}
ret = sshnc_send(client, buffer, nr);
if (ret == SSHNC_EOF) {
break;
} else if (ret != SSHNC_OK) {
fprintf(stderr, "Error on SSH send: %s\n", sshnc_strerror(ret));
goto cleanup;
}
}
}
sshnc_close(client);
return 0;
cleanup:
sshnc_close(client);
return 1;
}
|