This repository has been archived by the owner on Jun 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.c
297 lines (239 loc) · 6.67 KB
/
main.c
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
/* vim: set tabstop=8 shiftwidth=2 softtabstop=2 expandtab smarttab colorcolumn=80: */
/*
* Copyright 2018 Red Hat, Inc.
*
* Author: Nathaniel McCallum
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../lib/tlssock.h"
#include "opt.h"
#include "hex.h"
#include "exe.h"
#include "non.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <poll.h>
#define addrinfo_auto_t addrinfo_t __attribute__((cleanup(addrinfo_cleanup)))
#define fd_auto_t fd_t __attribute__((cleanup(fd_cleanup)))
typedef struct addrinfo addrinfo_t;
typedef int fd_t;
typedef enum {
STATUS_SUCCESS = EXIT_SUCCESS,
STATUS_FAILURE = EXIT_FAILURE,
STATUS_CONTINUE,
} status_t;
static void
addrinfo_cleanup(addrinfo_t **ai)
{
if (!ai || !*ai)
return;
freeaddrinfo(*ai);
}
static void
fd_cleanup(fd_t *fd)
{
if (!fd || *fd < 0)
return;
close(*fd);
}
static bool
ai_family_applicable(const options_t *opts, const struct addrinfo *ai)
{
switch (ai->ai_family) {
case AF_INET6: return !opts->ipv4 || opts->ipv6;
case AF_INET: return opts->ipv4 || !opts->ipv6;
default: return false;
}
}
static bool
ai_socktype_applicable(const options_t *opts, const struct addrinfo *ai)
{
switch (ai->ai_socktype) {
case SOCK_STREAM: return !opts->udp;
case SOCK_DGRAM: return opts->udp;
default: return false;
}
}
static bool
ai_protocol_applicable(const options_t *opts, const struct addrinfo *ai)
{
switch (ai->ai_protocol) {
case IPPROTO_IP: return true;
case IPPROTO_TCP: return !opts->udp;
case IPPROTO_UDP: return opts->udp;
default: return false;
}
}
static bool
ai_applicable(const options_t *opts, const struct addrinfo *ai)
{
return ai_family_applicable(opts, ai)
&& ai_socktype_applicable(opts, ai)
&& ai_protocol_applicable(opts, ai);
}
static ssize_t
keydup(const options_t *o, uint8_t **key)
{
size_t size = strlen(o->pskk) / 2;
*key = malloc(size);
if (!*key)
return -1;
if (!hex2bin(o->pskk, *key, size))
return -1;
return size;
}
static ssize_t
srv_psk_cb(void *m, const char *username, uint8_t **key)
{
const options_t *o = m;
if (strcmp(username, o->psku) != 0)
return -1;
return keydup(o, key);
}
static ssize_t
clt_psk_cb(void *m, char **username, uint8_t **key)
{
const options_t *o = m;
*username = strdup(o->psku);
if (!*username)
return -1;
return keydup(o, key);
}
static status_t
on_conn(options_t *opts, int con, int in, int out, const struct addrinfo *ai)
{
int outs[] = { out, con };
struct pollfd pfds[] = {
{ .fd = in, .events = POLLIN },
{ .fd = con, .events = POLLIN },
};
if (ai->ai_protocol == IPPROTO_TLS) {
int ret;
if (opts->listen) {
tls_srv_handshake_t srv = { .misc = opts };
if (opts->psku)
srv.psk = srv_psk_cb;
ret = non_setsockopt(con, IPPROTO_TLS,
TLS_SRV_HANDSHAKE, &srv, sizeof(srv));
} else {
tls_clt_handshake_t clt = { .misc = opts };
if (opts->psku)
clt.psk = clt_psk_cb;
ret = non_setsockopt(con, IPPROTO_TLS,
TLS_CLT_HANDSHAKE, &clt, sizeof(clt));
}
if (ret != 0) {
fprintf(stderr, "%m: Unable to complete TLS handshake!\n");
shutdown(con, SHUT_RDWR);
return STATUS_FAILURE;
}
}
while (poll(pfds, 2, -1) >= 0) {
char buffer[64 * 1024] = {};
ssize_t ret;
for (int i = 0; i < 2; i++) {
if (!pfds[i].revents)
continue;
ret = read(pfds[i].fd, buffer, sizeof(buffer));
if (ret <= 0) {
if (pfds[i].revents != POLLHUP &&
(errno == EAGAIN || errno == EWOULDBLOCK))
continue;
shutdown(con, SHUT_RDWR);
if (ret == 0)
return STATUS_SUCCESS;
if (errno == 0 || (opts->listen && errno == EIO))
return STATUS_SUCCESS;
return STATUS_FAILURE;
}
if (non_write(outs[(i + 1) % 2], buffer, ret) != ret) {
fprintf(stderr, "%m: Error during write()!\n");
shutdown(con, SHUT_RDWR);
return STATUS_FAILURE;
}
}
}
fprintf(stderr, "%m: Error during poll()!\n");
shutdown(con, SHUT_RDWR);
return STATUS_FAILURE;
}
static status_t
on_sock(options_t *opts, int fd, const struct addrinfo *ai)
{
int out = STDOUT_FILENO;
exe_auto_t *exe = NULL;
int in = STDIN_FILENO;
fd_auto_t con = -1;
if (opts->exec) {
exe = exe_run(opts->exec, opts->shell, ai->ai_socktype);
if (!exe) {
fprintf(stderr, "%m: error executing '%s'!\n", opts->exec);
return STATUS_FAILURE;
}
in = out = exe_fd(exe);
}
if (opts->listen) {
if (bind(fd, ai->ai_addr, ai->ai_addrlen) != 0)
return STATUS_CONTINUE;
if (listen(fd, 0) != 0)
return STATUS_CONTINUE;
con = non_accept(fd, NULL, NULL);
if (con < 0)
return STATUS_CONTINUE;
shutdown(fd, SHUT_RDWR);
} else if (non_connect(fd, ai->ai_addr, ai->ai_addrlen) != 0) {
return STATUS_CONTINUE;
}
return on_conn(opts, opts->listen ? con : fd, in, out, ai);
}
int
main(int argc, char *argv[])
{
addrinfo_auto_t *ai = NULL;
options_t opts = {};
signal(SIGPIPE, SIG_IGN);
if (!opts_parse(&opts, argc, argv))
return EXIT_FAILURE;
if (getaddrinfo(opts.host, opts.port, NULL, &ai) != 0) {
fprintf(stderr, "Invalid host (%s) or port (%s)!", opts.host, opts.port);
return EXIT_FAILURE;
}
for (struct addrinfo *i = ai; i; i = i->ai_next) {
int flags = SOCK_CLOEXEC | opts.block ? 0 : SOCK_NONBLOCK;
fd_auto_t fd = -1;
if (opts.tls) {
if (!ai_applicable(&opts, i))
continue;
i->ai_protocol = IPPROTO_TLS;
}
fd = socket(i->ai_family, i->ai_socktype | flags, i->ai_protocol);
if (fd < 0)
continue;
switch(on_sock(&opts, fd, i)) {
case STATUS_SUCCESS: return EXIT_SUCCESS;
case STATUS_FAILURE: return EXIT_FAILURE;
case STATUS_CONTINUE: continue;
}
}
fprintf(stderr, "No valid configuration!\n");
return EXIT_FAILURE;
}