aboutsummaryrefslogtreecommitdiff
path: root/ssh/server.c
blob: 96fc09ef833df73c4b8273fb83f493b0695f07d6 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <pthread.h>
#include <netdb.h>
#include <poll.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <libssh/server.h>
#include <libssh/callbacks.h>
#include "util.h"


#define RESOURCE_ERROR_SLEEP_MS 10000


static void xxd(FILE *stream, const void *buf_, size_t length) {
	unsigned char *buf = (unsigned char*)buf_;

	for (size_t cursor = 0; cursor < length;) {
		fprintf(stream, "%08zx:", cursor);

		for (int i = 0; i < 16; i++) {
			if (i % 2 == 0) fprintf(stream, " ");
			if (i % 8 == 0) fprintf(stream, " ");
			if (cursor + i < length) fprintf(stream, "%02x", (unsigned)buf[cursor + i]);
			else fprintf(stream, "  ");
		}

		fprintf(stream, "  |");

		for (int i = 0; i < 16 && cursor + i < length; i++) {
			if (isprint(buf[cursor + i])) fprintf(stream, "%c", buf[cursor + i]);
			else fprintf(stream, ".");
		}

		fprintf(stream, "|\n");

		cursor += 16;
	}
}

static atomic_int g_thread_count;

struct thread_data {
	struct addrinfo backend_addr;

	int backend_fd;

	int thread_id;
	ssh_session session;
	ssh_channel channel;  // NULL before channel has been opened
	bool should_close;

	struct ssh_server_callbacks_struct server_cb;
	struct ssh_channel_callbacks_struct chan_cb;
};

///////// CHANNEL CALLBACKS //////////

static int channel_subsystem_request_cb(ssh_session session, ssh_channel channel, const char *subsystem, void *tdata_) {
	(void)session;
	(void)channel;
	struct thread_data *tdata = (struct thread_data*)tdata_;
	if (strcmp(subsystem, "tomsg") == 0) {
		printf("[%d] subsystem request: <%s>, allowing\n", tdata->thread_id, subsystem);
		return 0;
	} else {
		printf("[%d] subsystem request: <%s>, denying!\n", tdata->thread_id, subsystem);
		return 1;
	}
}

static void channel_close_cb(ssh_session session, ssh_channel channel, void *tdata_) {
	(void)session; (void)channel;
	struct thread_data *tdata = (struct thread_data*)tdata_;
	printf("[%d] channel close!\n", tdata->thread_id);
}

static void channel_eof_cb(ssh_session session, ssh_channel channel, void *tdata_) {
	(void)session; (void)channel;
	struct thread_data *tdata = (struct thread_data*)tdata_;
	printf("[%d] eof on channel, setting close flag\n", tdata->thread_id);
	tdata->should_close = true;
}

static int channel_data_cb(ssh_session session, ssh_channel channel, void *data, uint32_t len, int is_stderr, void *tdata_) {
	(void)is_stderr; (void)data; (void)channel; (void)session;
	struct thread_data *tdata = (struct thread_data*)tdata_;
	printf("[%d] data on channel (length %u):\n", tdata->thread_id, len);
	xxd(stdout, data, len);
	// printf("[%d] echoing back!\n", tdata->thread_id);
	// if (ssh_channel_write(channel, data, len) == SSH_ERROR) {
	//     printf("[%d] write to channel failed! Setting close flag\n", tdata->thread_id);
	//     tdata->should_close = true;
	// }
	const char *start = (const char*)data;
	const char *cursor = start;
	const char *end = start + len;
	while (cursor < end) {
		ssize_t nw = write(tdata->backend_fd, cursor, end - cursor);
		if (nw < 0) {
			if (errno == EINTR) continue;
			printf("[%d] error writing to backend socket: %s\n", tdata->thread_id, strerror(errno));
			tdata->should_close = true;
			return cursor - start;
		}
		if (nw == 0) {  // should not happen?
			printf("[%d] write(2) returned 0?\n", tdata->thread_id);
			tdata->should_close = true;
			return cursor - start;
		}
		cursor += nw;
	}
	return len;
}

static int channel_write_wontblock_cb(ssh_session session, ssh_channel channel, size_t bytes, void *tdata_) {
	(void)channel; (void)session;
	printf("[%d] write won't block for %zu bytes notification\n", ((struct thread_data*)tdata_)->thread_id, bytes);
	return 0;
}

////////// SERVER CALLBACKS //////////

static int auth_none_cb(ssh_session session, const char *user, void *tdata_) {
	(void)session;
	struct thread_data *tdata = (struct thread_data*)tdata_;
	printf("[%d] auth none (user <%s>), accepting\n", tdata->thread_id, user);
	return SSH_AUTH_SUCCESS;
}

static int service_request_cb(ssh_session session, const char *service, void *tdata_) {
	(void)session;
	struct thread_data *tdata = (struct thread_data*)tdata_;
	if (strcmp(service, "ssh-userauth") == 0) {
		printf("[%d] ssh-userauth service request, allowing through\n", tdata->thread_id);
		return 0;
	} else {
		printf("[%d] service request <%s>, not allowing\n", tdata->thread_id, service);
		return -1;
	}
}

static ssh_channel chan_open_request_cb(ssh_session session, void *tdata_) {
	struct thread_data *tdata = (struct thread_data*)tdata_;
	if (tdata->channel == NULL) {
		ssh_channel chan = ssh_channel_new(session);
		if (chan != NULL) {
			if (ssh_set_channel_callbacks(chan, &tdata->chan_cb) == SSH_OK) {
				printf("[%d] channel open request, allowing\n", tdata->thread_id);
				tdata->channel = chan;
				return chan;
			}
			ssh_channel_close(chan);
			ssh_channel_free(chan);
		}
	}
	printf("[%d] channel open request, denying!\n", tdata->thread_id);
	return NULL;
}

static int backend_data_cb(int fd, int revents, void *tdata_) {
	struct thread_data *tdata = (struct thread_data*)tdata_;

	if (revents & (POLLERR|POLLHUP|POLLNVAL)) {
		char descr[64] = "";
		if (revents & POLLERR) strcat(descr, "|POLLERR");
		if (revents & POLLHUP) strcat(descr, "|POLLHUP");
		if (revents & POLLNVAL) strcat(descr, "|POLLNVAL");
		printf("[%d] %s on backend\n", tdata->thread_id, descr + 1);
		close(fd);
		tdata->should_close = true;
	}

	if (revents & POLLIN) {
		char buffer[1024];
		ssize_t nr = read(fd, buffer, sizeof buffer);
		if (nr < 0) {
			if (errno == EINTR) return 0;
			printf("[%d] Error reading from backend socket: %s\n", tdata->thread_id, strerror(errno));
			tdata->should_close = true;
			return 0;
		}

		if (nr == 0) {  // eof
			tdata->should_close = true;
			return 0;
		}

		int cursor = 0;
		while (cursor < nr) {
			int nw = ssh_channel_write(tdata->channel, buffer + cursor, nr - cursor);
			if (nw == SSH_ERROR) {
				printf("[%d] Error writing to ssh channel: %s\n", tdata->thread_id, ssh_get_error(tdata->channel));
				tdata->should_close = true;
				return 0;
			}
			cursor += nw;
		}
	}

	return 0;
}

static void print_addrinfo(FILE *stream, const struct addrinfo *info) {
	if (info->ai_family == AF_INET) fprintf(stream, "inet ");
	else if (info->ai_family == AF_INET6) fprintf(stream, "inet6 ");
	else fprintf(stream, "(family=%d) ", info->ai_family);

	if (info->ai_socktype == SOCK_STREAM) fprintf(stream, "stream ");
	else if (info->ai_socktype == SOCK_DGRAM) fprintf(stream, "datagram ");
	else fprintf(stream, "(socktype=%d) ", info->ai_socktype);

	if (info->ai_protocol == IPPROTO_TCP) fprintf(stream, "TCP ");
	else if (info->ai_protocol == IPPROTO_UDP) fprintf(stream, "UDP ");
	else fprintf(stream, "(protocol=%d) ", info->ai_protocol);

	bool success = false;
	if (info->ai_family == AF_INET) {
		char addrbuf[INET_ADDRSTRLEN];
		struct sockaddr_in *sin = (struct sockaddr_in*)info->ai_addr;
		if (inet_ntop(AF_INET, &sin->sin_addr, addrbuf, INET_ADDRSTRLEN)) {
			fprintf(stream, "%s\n", addrbuf);
			success = true;
		}
	} else if (info->ai_family == AF_INET6) {
		char addrbuf[INET6_ADDRSTRLEN];
		struct sockaddr_in6 *sin = (struct sockaddr_in6*)info->ai_addr;
		if (inet_ntop(AF_INET6, &sin->sin6_addr, addrbuf, INET6_ADDRSTRLEN)) {
			fprintf(stream, "%s\n", addrbuf);
			success = true;
		}
	}
	if (!success) {
		fprintf(stream, "(unknown address format: %s)\n", strerror(errno));
	}
}

// Returns whether successful.
static bool lookup_backend(const char *host, int port, struct addrinfo *dst) {
	char port_string[16];
	sprintf(port_string, "%d", port);

	struct addrinfo hints;
	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_ADDRCONFIG;

	struct addrinfo *result;
	int ret = getaddrinfo(host, port_string, &hints, &result);

	if (ret < 0) {
		fprintf(stderr, "Could not resolve backend: %s\n", gai_strerror(ret));
		return false;
	}

	int last_failure = 0;
	bool success = false;
	for (struct addrinfo *item = result; item; ) {
		printf("lookup_backend: option ");
		print_addrinfo(stdout, item);

		int sock = socket(item->ai_family, item->ai_socktype, item->ai_protocol);
		if (sock == -1) {
			last_failure = errno;
			printf("  socket() failure: %s\n", strerror(last_failure));
			continue;
		}

		int ret = connect(sock, item->ai_addr, item->ai_addrlen);
		last_failure = errno;
		close(sock);

		if (ret == 0) {
			printf("  success!\n");
			success = true;
			// Free the rest of the linked list, keeping this item intact.
			freeaddrinfo(item->ai_next);
			*dst = *item;
			dst->ai_next = NULL;
			break;
		} else {
			printf("  connect() failure: %s\n", strerror(last_failure));
		}

		printf("  next=%p\n", item->ai_next);

		// Free this element in the linked list, but preserve (and switch to) the tail.
		struct addrinfo *next = item->ai_next;
		item->ai_next = NULL;
		freeaddrinfo(item);
		item = next;
	}

	if (success) {
		return true;
	} else {
		fprintf(stderr, "Could not connect to backend: %s\n", strerror(last_failure));
		return false;
	}
}

static int connect_backend(const struct thread_data *tdata) {
	const struct addrinfo *item = &tdata->backend_addr;
	int sock = socket(item->ai_family, item->ai_socktype, item->ai_protocol);
	if (sock == -1) return -1;

	if (connect(sock, item->ai_addr, item->ai_addrlen) == 0) {
		printf("connect_backend: sock=%d\n", sock);
		return sock;
	}

	close(sock);
	return -1;
}

static void* thread_entry(void *tdata_) {
	struct thread_data *tdata = (struct thread_data*)tdata_;
	const int tid = tdata->thread_id;
	const ssh_session session = tdata->session;
	ssh_event event = NULL;

	printf("[%d] Thread started\n", tid);

	memset(&tdata->server_cb, 0, sizeof tdata->server_cb);
	ssh_callbacks_init(&tdata->server_cb);
	tdata->server_cb.userdata = tdata;
	tdata->server_cb.auth_none_function = auth_none_cb;
	tdata->server_cb.channel_open_request_session_function = chan_open_request_cb;
	tdata->server_cb.service_request_function = service_request_cb;

	memset(&tdata->chan_cb, 0, sizeof tdata->chan_cb);
	ssh_callbacks_init(&tdata->chan_cb);
	tdata->chan_cb.userdata = tdata;
	tdata->chan_cb.channel_subsystem_request_function = channel_subsystem_request_cb;
	tdata->chan_cb.channel_close_function = channel_close_cb;
	tdata->chan_cb.channel_eof_function = channel_eof_cb;
	tdata->chan_cb.channel_data_function = channel_data_cb;
	tdata->chan_cb.channel_write_wontblock_function = channel_write_wontblock_cb;

	if (ssh_set_server_callbacks(session, &tdata->server_cb) != SSH_OK) {
		printf("[%d] Failed setting server callbacks: %s\n", tid, ssh_get_error(session));
		goto cleanup;
	}

	ssh_set_auth_methods(session, SSH_AUTH_METHOD_NONE);

	if (ssh_handle_key_exchange(session) != SSH_OK) {
		printf("[%d] Key exchange failed: %s\n", tid, ssh_get_error(session));
		goto cleanup;
	}
	printf("[%d] Handled key exchange\n", tid);

	tdata->backend_fd = connect_backend(tdata);
	if (tdata->backend_fd == -1) {
		printf("[%d] Failed to connect to backend: %s\n", tid, strerror(errno));
		goto cleanup;
	}

	printf("[%d] Connected to backend (fd=%d)\n", tid, tdata->backend_fd);

	event = ssh_event_new();
	if (!event
			|| ssh_event_add_session(event, session) != SSH_OK
			|| ssh_event_add_fd(event, tdata->backend_fd, POLLIN, backend_data_cb, tdata) != SSH_OK) {
		printf("[%d] Failed to create ssh event context\n", tid);
		goto cleanup;
	}

	while (!tdata->should_close) {
		// printf("[%d] poll loop...\n", tid);
		ssh_event_dopoll(event, -1);
		int status = ssh_get_status(session);
		if (status & (SSH_CLOSED | SSH_CLOSED_ERROR)) goto cleanup;
		if (status & SSH_READ_PENDING) {
			printf("[%d] read pending?\n", tid);
		}
	}

cleanup:
	if (tdata->backend_fd != -1) close(tdata->backend_fd);
	if (event) ssh_event_free(event);
	if (tdata->channel) {
		ssh_channel_close(tdata->channel);
		ssh_channel_free(tdata->channel);
	}
	if (session) {
		ssh_disconnect(session);
		ssh_free(session);
		printf("[%d] Disconnected\n", tid);
	}
	free(tdata);
	int num_threads = atomic_fetch_sub(&g_thread_count, 1);
	printf("[%d] Exiting! (%d threads remaining)\n", tid, num_threads - 1);
	pthread_exit(NULL);
}

static void generate_key(const char *outfile) {
	ssh_key host_key;
	int ret = ssh_pki_generate(SSH_KEYTYPE_RSA, 4096, &host_key);
	if (ret != SSH_OK) {
		fprintf(stderr, "Key generation failed (RSA4096)!\n");
		exit(1);
	}

	ret = ssh_pki_export_privkey_file(host_key, NULL, NULL, NULL, outfile);
	if (ret != SSH_OK) {
		fprintf(stderr, "Failed to export generated host key to file '%s'; is that location accessible?\n", outfile);
		exit(1);
	}

	if (chmod(outfile, S_IRUSR | S_IWUSR) != 0) {
		fprintf(stderr, "Failed to set mode 600 on generated host key file '%s'; this is insecure!\n", outfile);
		exit(1);
	}

	printf("RSA4096 host key generated and written to '%s'.\n", outfile);
}

static void usage(const char *argv0) {
	fprintf(stderr,
			"Usage: %s <ssh host key file> <ssh port> [backendhost:port]\n"
			"       %s --generate <host key output file>\n"
			"SSH-TCP bridge for tomsg. Accepts SSH connections with a channel for subsystem\n"
			"'tomsg', and matches each SSH connection with a plain TCP connection to the\n"
			"backend server (which defaults to localhost:29536). All data is forwarded\n"
			"transparently.\n"
			"Use the '--generate' form to generate a host key for use in the main invocation\n"
			"form.\n",
			argv0, argv0);
}

int main(int argc, char **argv) {
	const char *host_key_fname;
	int ssh_port = 2222;
	const char *backend_host = "localhost";
	int backend_port = 29536;

	if (argc == 3 && strcmp(argv[1], "--generate") == 0) {
		generate_key(argv[2]);
		return 0;
	} else if (3 <= argc && argc <= 4) {
		host_key_fname = argv[1];
		char *endp;
		ssh_port = strtol(argv[2], &endp, 10);
		if (argv[2][0] == '\0' || *endp != '\0' || ssh_port < 0 || ssh_port > 65535) {
			fprintf(stderr, "Cannot parse port number from argument '%s'\n", argv[2]);
			return 1;
		}
		if (argc == 4) {
			if (!parse_host_port(argv[3], &backend_host, &backend_port)) {
				fprintf(stderr, "Cannot parse host:port from argument '%s'\n", argv[3]);
				return 1;
			}
		}
	} else {
		usage(argv[0]);
		return 1;
	}

	// We prefer to detect socket closure through return codes, not signals.
	signal(SIGPIPE, SIG_IGN);

	if (ssh_init() != SSH_OK) {
		fprintf(stderr, "Could not initialise libssh\n");
		return 1;
	}

	ssh_key host_key;
	if (ssh_pki_import_privkey_file(host_key_fname, NULL, NULL, NULL, &host_key) != SSH_OK) {
		fprintf(stderr, "Failed to read host private key file '%s'\n", host_key_fname);
		return 1;
	}

	size_t host_key_hash_length = 0;
	unsigned char *host_key_hash = NULL;
	if (ssh_get_publickey_hash(host_key, SSH_PUBLICKEY_HASH_SHA256, &host_key_hash, &host_key_hash_length) != SSH_OK) {
		fprintf(stderr, "Failed to hash host key!\n");
		return 1;
	}

	printf("Host key hash: ");
	fflush(stdout);
	ssh_print_hash(SSH_PUBLICKEY_HASH_SHA256, host_key_hash, host_key_hash_length);

	ssh_bind srvbind = ssh_bind_new();
	if (!srvbind) {
		fprintf(stderr, "Failed to create new bind socket\n");
		return 1;
	}

	bool procconfig = false;
	const char *ciphers_str = "aes256-gcm@openssh.com,aes256-ctr,aes256-cbc";
	bool ok = true;
	ok &= ssh_bind_options_set(srvbind, SSH_BIND_OPTIONS_PROCESS_CONFIG, &procconfig) == SSH_OK;
	ok &= ssh_bind_options_set(srvbind, SSH_BIND_OPTIONS_BINDPORT, &ssh_port) == SSH_OK;
	ok &= ssh_bind_options_set(srvbind, SSH_BIND_OPTIONS_IMPORT_KEY, host_key) == SSH_OK;
	ok &= ssh_bind_options_set(srvbind, SSH_BIND_OPTIONS_CIPHERS_C_S, ciphers_str) == SSH_OK;
	ok &= ssh_bind_options_set(srvbind, SSH_BIND_OPTIONS_CIPHERS_S_C, ciphers_str) == SSH_OK;

	if (!ok) {
		fprintf(stderr, "Could not set options on SSH bind socket: %s\n", ssh_get_error(srvbind));
		return 1;
	}

	if (ssh_bind_listen(srvbind) != SSH_OK) {
		fprintf(stderr, "Could not listen on SSH bind socket: %s\n", ssh_get_error(srvbind));
		return 1;
	}

	struct addrinfo backend_addr;
	if (!lookup_backend(backend_host, backend_port, &backend_addr)) {
		// Error already printed in lookup_backend
		return 1;
	}

	printf("Listening for SSH connections on port %d\n", ssh_port);
	printf("Forwarding to backend at %s:%d\n", backend_host, backend_port);

	pthread_attr_t thread_attrs;
	assert(pthread_attr_init(&thread_attrs) == 0);
	assert(pthread_attr_setdetachstate(&thread_attrs, PTHREAD_CREATE_DETACHED) == 0);

	int next_thread_id = 0;
	atomic_store(&g_thread_count, 0);

	while (true) {
		ssh_session session = ssh_new();
		if (!session) {
			fprintf(stderr, "ERROR: Cannot create new SSH session object!\n");
			usleep(1000 * RESOURCE_ERROR_SLEEP_MS);
			continue;
		}

		if (ssh_bind_accept(srvbind, session) != SSH_OK) {
			fprintf(stderr, "ERROR: Cannot accept on bind socket: %s", ssh_get_error(srvbind));
			ssh_free(session);
			usleep(1000 * RESOURCE_ERROR_SLEEP_MS);
			continue;
		}

		int num_existing_threads = atomic_fetch_add(&g_thread_count, 1);
		printf("Accepted connection, spinning up thread (currently %d threads)\n",
				num_existing_threads + 1);

		struct thread_data *tdata = calloc(1, sizeof(struct thread_data));
		if (!tdata) {
			fprintf(stderr, "ERROR: Out of memory, cannot allocate thread_data!\n");
			ssh_disconnect(session);
			ssh_free(session);
			usleep(1000 * RESOURCE_ERROR_SLEEP_MS);
			continue;
		}

		tdata->backend_addr = backend_addr;
		tdata->backend_fd = -1;
		tdata->thread_id = next_thread_id++;
		tdata->session = session;
		tdata->channel = NULL;
		tdata->should_close = false;

		pthread_t thread;
		if (pthread_create(&thread, &thread_attrs, thread_entry, tdata) != 0) {
			fprintf(stderr, "ERROR: Could not spawn thread: %s!\n", strerror(errno));
			free(tdata);
			ssh_disconnect(session);
			ssh_free(session);
			usleep(1000 * RESOURCE_ERROR_SLEEP_MS);
			continue;
		}
	}
}