mirror of
https://github.com/yrutschle/sslh.git
synced 2026-10-22 18:12:10 +03:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1a3341c2a4 | ||
|
|
4a6bbda60d | ||
|
|
fa848f2ae9 | ||
|
|
6aa19d080a | ||
|
|
a43385b8db | ||
|
|
1261a5f4c7 | ||
|
|
40da147efd | ||
|
|
e4936454c5 | ||
|
|
63f9c4a582 | ||
|
|
0e45107797 | ||
|
|
4584e719e1 | ||
|
|
75e426c80f | ||
|
|
e40d43bea5 | ||
|
|
2196146224 | ||
|
|
2f822741b3 | ||
|
|
760def3444 | ||
|
|
4b885b4a2c | ||
|
|
ce23f202b7 | ||
|
|
82b8ba547e | ||
|
|
8e38d56167 | ||
|
|
ea3b6794e1 | ||
|
|
e42f165ef5 | ||
|
|
37d2756703 | ||
|
|
ec25ab56e8 | ||
|
|
7fb65ad0ac | ||
|
|
c43882c85f | ||
|
|
4a4c571116 | ||
|
|
c049885758 | ||
|
|
de0ec959d9 | ||
|
|
e7df8eeaa1 | ||
|
|
8af87ebbad | ||
|
|
bf57d63c3a | ||
|
|
862e33cfec | ||
|
|
24e7f46a43 | ||
|
|
bf2053eb79 | ||
|
|
1ad450a444 | ||
|
|
300e1916c3 | ||
|
|
e678428334 | ||
|
|
edc42ca13b | ||
|
|
c41ec489e0 | ||
|
|
79f49f4481 | ||
|
|
8e130882fc | ||
|
|
030ef64b99 | ||
|
|
cf4f4cbebe | ||
|
|
905ac95ca1 | ||
|
|
da3b51e056 | ||
|
|
308b54aa8a | ||
|
|
987643878f | ||
|
|
46e741e9c2 | ||
|
|
c3d019284d | ||
|
|
4c570e8d57 | ||
|
|
0a00220b42 | ||
|
|
a0f9fc164e | ||
|
|
517e4ad5b4 | ||
|
|
b258b0e0f7 | ||
|
|
02f6c6999d | ||
|
|
41a914b350 | ||
|
|
a487ad4cbe | ||
|
|
21ed247acb | ||
|
|
dfa5d3cedb | ||
|
|
74ce2acdc5 | ||
|
|
0e3242c115 | ||
|
|
5715a9f0bd | ||
|
|
de474e1d07 | ||
|
|
cb199ed3c6 | ||
|
|
7485afe397 | ||
|
|
417722e3c1 | ||
|
|
9fca586735 | ||
|
|
0613d412a2 | ||
|
|
b2ee07655a | ||
|
|
465ecdda5b | ||
|
|
3edf6a9d22 | ||
|
|
7a88a50c32 | ||
|
|
e7a5bbc4e1 | ||
|
|
40557c58ad | ||
|
|
f0e1aaf82c | ||
|
|
f51781664c | ||
|
|
5948270b95 | ||
|
|
758be2a8ab | ||
|
|
654ff0b5b0 | ||
|
|
15a5073559 | ||
|
|
c2b6bf246c | ||
|
|
6366d50b89 | ||
|
|
5b93e4ab55 | ||
|
|
1c63b06cc8 | ||
|
|
71c617050d | ||
|
|
0409775a1a | ||
|
|
b72baa0622 | ||
|
|
790d639ad0 | ||
|
|
e884fc616d | ||
|
|
7e63dedca3 | ||
|
|
49d4080afd | ||
|
|
be4e126f3a | ||
|
|
34bcc2bbc8 | ||
|
|
90975fd6c3 | ||
|
|
f91f16d753 | ||
|
|
dae8101a50 | ||
|
|
68fc10aed0 | ||
|
|
49c136691c | ||
|
|
adb27aa4a3 | ||
|
|
9ff9723278 | ||
|
|
52a9356c35 | ||
|
|
ac3b43d4bd | ||
|
|
368f286ce5 | ||
|
|
05a835ff1f | ||
|
|
0a3d5874b4 | ||
|
|
a42d4f8049 | ||
|
|
1e33455fe7 | ||
|
|
a3d9df87cb | ||
|
|
247d60d8e6 | ||
|
|
7d820049a2 | ||
|
|
09597bfa42 | ||
|
|
41e3b0cd1d | ||
|
|
76d0351452 | ||
|
|
fccaa5fa9f | ||
|
|
f077101835 | ||
|
|
f3230b4a94 | ||
|
|
c12f7a1ade | ||
|
|
ebeabb6c18 | ||
|
|
8a1dae8c22 | ||
|
|
07719f55b8 | ||
|
|
6c94bf71b8 | ||
|
|
0d9e004d32 | ||
|
|
0c388cb367 | ||
|
|
9f99f296b1 | ||
|
|
8b6e06e6c7 | ||
|
|
5cdff8c558 | ||
|
|
72438dc1ae | ||
|
|
e67d6ff905 | ||
|
|
0af37a1bed | ||
|
|
08ce89b6e2 | ||
|
|
99d4a6d4a2 | ||
|
|
2c93a015ea | ||
|
|
3315a727dc | ||
|
|
8638199f13 | ||
|
|
d7cf82424b | ||
|
|
ac8563525e | ||
|
|
2f983625d0 | ||
|
|
f6b11a424b | ||
|
|
cf46cd5bb4 | ||
|
|
b473622698 | ||
|
|
de8e5725c2 | ||
|
|
0cc516bf51 | ||
|
|
337bd34fb7 | ||
|
|
46d9796bd6 | ||
|
|
219163ac27 | ||
|
|
8ddbe59883 | ||
|
|
c5b0932bad | ||
|
|
9e2cc93973 | ||
|
|
408c5741f8 | ||
|
|
c595aadb4d | ||
|
|
61191edf7c | ||
|
|
1009f556fc | ||
|
|
98807771e0 | ||
|
|
4610e220bd | ||
|
|
dfa764e2e8 | ||
|
|
10fe9c6e27 | ||
|
|
d1f782e1fe | ||
|
|
9ed44ee09b | ||
|
|
e913eeeb88 | ||
|
|
9d1d9f850a | ||
|
|
588883eb42 | ||
|
|
bae16ab29e | ||
|
|
32851d2041 | ||
|
|
49e8bff01b | ||
|
|
87aaa156e0 | ||
|
|
ef8233a839 | ||
|
|
125458df51 | ||
|
|
d2cfa3c603 | ||
|
|
891bcf9966 | ||
|
|
544c2b6d2f | ||
|
|
0380a4309f | ||
|
|
3aa245efa5 | ||
|
|
bc28d6ce19 | ||
|
|
d78c810d84 | ||
|
|
ed3cd40e23 | ||
|
|
12edc3dbca | ||
|
|
0931dfdf0b | ||
|
|
0d5a2f6922 | ||
|
|
49e59734cb | ||
|
|
914dc98fb3 | ||
|
|
1dc0088b74 | ||
|
|
9416617456 | ||
|
|
7e047e35b3 | ||
|
|
8ffcf5ab03 | ||
|
|
a917149e14 | ||
|
|
08a64e99b4 | ||
|
|
0ef57f1500 | ||
|
|
fde86a778d | ||
|
|
5bd0235d38 | ||
|
|
177d337ac9 | ||
|
|
8e203c897e | ||
|
|
6e9f24153f | ||
|
|
f029b4098b | ||
|
|
c1369910c9 | ||
|
|
d756f7d504 | ||
|
|
6d99f780ef | ||
|
|
d5baed3f18 | ||
|
|
5684959dd6 | ||
|
|
95a6577cda | ||
|
|
b5d8b2d199 | ||
|
|
e528f519bc | ||
|
|
b529069029 | ||
|
|
67eb471c6f | ||
|
|
2705face30 | ||
|
|
4e725e1520 | ||
|
|
b0c3c8fdbc | ||
|
|
15f733e572 | ||
|
|
ff91f94315 | ||
|
|
0a880ea607 | ||
|
|
e0312b4a9d | ||
|
|
5a213c9650 | ||
|
|
7baf7f724c | ||
|
|
848b107f2b | ||
|
|
a7b4462c6e | ||
|
|
5b309a9b97 | ||
|
|
d2b64c7f38 | ||
|
|
d647b4eb55 | ||
|
|
a584348a55 | ||
|
|
530acc7c72 | ||
|
|
dbc0667ad3 | ||
|
|
e8654da78c | ||
|
|
343b0a0fbf | ||
|
|
ad0adfb0e1 | ||
|
|
33ab9d535d | ||
|
|
d3d4fd657a | ||
|
|
e2fddf17fc | ||
|
|
e7ce929020 | ||
|
|
7af31c45c9 |
No files matched your search
@@ -1,3 +1,78 @@
|
||||
v1.22: 17AUG2021
|
||||
sslh-select now supports UDP protocols.
|
||||
Probes specified in the `protocols`
|
||||
configuration entry are tried on incoming packets,
|
||||
TCP or UDP, and forwarded based on the input
|
||||
protocol (an incoming TCP connection will be
|
||||
forwarded as TCP, and same with UDP).
|
||||
This has been tested with DNS as shown in udp.cfg:
|
||||
incoming packets that contain my domain name are
|
||||
assumed to be a DNS request and forwarded
|
||||
accordingly. Note this could cause problems if
|
||||
combined with incoming TLS with SNI. UDP clients
|
||||
and servers need to agree on the IPv4/IPv6 they use:
|
||||
use the same protocol on all sides! Often, this
|
||||
means explicitely using 'ip4-localhost'.
|
||||
UDP sender-receiver pairs (connections, so to speak)
|
||||
are kept for 60s, which can be changed with
|
||||
`udp_timeout` in the configuration.
|
||||
|
||||
Added probes for UDP protocols QUICK and Teamspeak.
|
||||
|
||||
Added probes for syslog protocol.
|
||||
|
||||
sslh-select refactored to change linear searches
|
||||
through connections to linear searches through
|
||||
fd_set.
|
||||
|
||||
Fixed a libconfig call to support libconfig 1.7.3.
|
||||
|
||||
Added symbol to support libconfig 1.4.9, still in
|
||||
use in CentOS7.
|
||||
|
||||
Warn about unknown settings in the configuration
|
||||
file.
|
||||
|
||||
Added per-protocol `transparent` option. sslh-fork
|
||||
drops the capability after creating the server-side
|
||||
transparent socket. Transparent now uses CAP_NET_RAW
|
||||
instead of CAP_NET_ADMIN.
|
||||
|
||||
Removed compile-time option to use POSIX regex. Now
|
||||
regex must be PCRE2 (Perl-Compatible). This was in
|
||||
fact the case since v1.21, as PCRE are used to parse
|
||||
the config file.
|
||||
|
||||
v1.21: 11JUL2020
|
||||
WARNING:
|
||||
Moved configuration and command-line management to
|
||||
use conf2struct. Changes are:
|
||||
* `--ssl` and using `name: 'ssl'` in config file is no longer supported, use `tls` instead.
|
||||
* command line option <-F|--config> no longer defaults to /etc/sslh.cfg, so you have to
|
||||
specify it explicitly.
|
||||
* command line option <-v|--verbose> takes a mandatory integer parameter
|
||||
|
||||
Added TCP_FASTOPEN support for client sockets (if
|
||||
tfo_ok is specified in their configuration) and for
|
||||
listenint socket, if all client protocols support it.
|
||||
(Craig Andrews)
|
||||
|
||||
Added 'minlength' option to skip a probe if less
|
||||
than that many bytes have been received (mostly for
|
||||
regex)
|
||||
|
||||
Update Let's Encrypt entry in example.cfg for tls-alpn-01
|
||||
challenges; tls-sni-* challenges are now deprecated.
|
||||
|
||||
Log to syslog even if in foreground (for people who
|
||||
use fail2ban)
|
||||
|
||||
Use syslog_facility: "none" to disable syslog
|
||||
output.
|
||||
|
||||
Changed exit code for illegal command line parameter
|
||||
from 1 to 6 (for testing purposes)
|
||||
|
||||
v1.20: 20NOV2018
|
||||
Added support for socks5 protocol (Eugene Protozanov)
|
||||
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
FROM alpine:latest as build
|
||||
|
||||
ADD . /sslh
|
||||
|
||||
RUN \
|
||||
apk add \
|
||||
gcc \
|
||||
libconfig-dev \
|
||||
make \
|
||||
musl-dev \
|
||||
pcre-dev \
|
||||
perl && \
|
||||
cd /sslh && \
|
||||
make sslh-select && \
|
||||
strip sslh-select
|
||||
|
||||
FROM alpine:latest
|
||||
|
||||
COPY --from=build /sslh/sslh-select /sslh
|
||||
|
||||
RUN apk --no-cache add libconfig pcre
|
||||
|
||||
ENTRYPOINT [ "/sslh", "--foreground"]
|
||||
@@ -1,12 +1,14 @@
|
||||
# Configuration
|
||||
|
||||
VERSION=$(shell ./genver.sh -r)
|
||||
|
||||
# Configuration -- you probably need to `make clean` if you
|
||||
# change any of these
|
||||
ENABLE_REGEX=1 # Enable regex probes
|
||||
USELIBCONFIG=1 # Use libconfig? (necessary to use configuration files)
|
||||
USELIBPCRE=1 # Use libpcre? (needed for regex on musl)
|
||||
USELIBWRAP?= # Use libwrap?
|
||||
USELIBCAP= # Use libcap?
|
||||
USESYSTEMD= # Make use of systemd socket activation
|
||||
USELIBBSD?= # Use libbsd (needed to update process name in `ps`)
|
||||
COV_TEST= # Perform test coverage?
|
||||
PREFIX?=/usr
|
||||
BINDIR?=$(PREFIX)/sbin
|
||||
@@ -22,10 +24,10 @@ ifneq ($(strip $(COV_TEST)),)
|
||||
endif
|
||||
|
||||
CC ?= gcc
|
||||
CFLAGS ?=-Wall -g $(CFLAGS_COV)
|
||||
CFLAGS ?=-Wall -DLIBPCRE -g $(CFLAGS_COV)
|
||||
|
||||
LIBS=
|
||||
OBJS=common.o sslh-main.o probe.o tls.o
|
||||
LIBS=-lm -lpcre2-8
|
||||
OBJS=sslh-conf.o common.o sslh-main.o probe.o tls.o argtable3.o udp-listener.o collection.o gap.o
|
||||
|
||||
CONDITIONAL_TARGETS=
|
||||
|
||||
@@ -38,11 +40,6 @@ ifneq ($(strip $(ENABLE_REGEX)),)
|
||||
CPPFLAGS+=-DENABLE_REGEX
|
||||
endif
|
||||
|
||||
ifneq ($(strip $(USELIBPCRE)),)
|
||||
CPPFLAGS+=-DLIBPCRE
|
||||
LIBS:=$(LIBS) -lpcreposix
|
||||
endif
|
||||
|
||||
ifneq ($(strip $(USELIBCONFIG)),)
|
||||
LIBS:=$(LIBS) -lconfig
|
||||
CPPFLAGS+=-DLIBCONFIG
|
||||
@@ -59,6 +56,11 @@ ifneq ($(strip $(USESYSTEMD)),)
|
||||
CONDITIONAL_TARGETS+=systemd-sslh-generator
|
||||
endif
|
||||
|
||||
ifneq ($(strip $(USELIBBSD)),)
|
||||
LIBS:=$(LIBS) -lbsd
|
||||
CPPFLAGS+=-DLIBBSD
|
||||
endif
|
||||
|
||||
|
||||
all: sslh $(MAN) echosrv $(CONDITIONAL_TARGETS)
|
||||
|
||||
@@ -70,21 +72,27 @@ version.h:
|
||||
|
||||
sslh: sslh-fork sslh-select
|
||||
|
||||
$(OBJS): version.h
|
||||
$(OBJS): version.h common.h collection.h sslh-conf.h gap.h
|
||||
|
||||
sslh-fork: version.h $(OBJS) sslh-fork.o Makefile common.h
|
||||
sslh-conf.c sslh-conf.h: sslhconf.cfg
|
||||
conf2struct sslhconf.cfg
|
||||
|
||||
sslh-fork: version.h $(OBJS) sslh-fork.o Makefile
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o sslh-fork sslh-fork.o $(OBJS) $(LIBS)
|
||||
#strip sslh-fork
|
||||
|
||||
sslh-select: version.h $(OBJS) sslh-select.o Makefile common.h
|
||||
sslh-select: version.h $(OBJS) sslh-select.o Makefile
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o sslh-select sslh-select.o $(OBJS) $(LIBS)
|
||||
#strip sslh-select
|
||||
|
||||
systemd-sslh-generator: systemd-sslh-generator.o
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o systemd-sslh-generator systemd-sslh-generator.o -lconfig
|
||||
|
||||
echosrv: version.h $(OBJS) echosrv.o
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o echosrv echosrv.o probe.o common.o tls.o $(LIBS)
|
||||
echosrv-conf.c echosrv-conf.h: echosrv.cfg
|
||||
conf2struct echosrv.cfg
|
||||
|
||||
echosrv: version.h echosrv-conf.c echosrv.o echosrv-conf.o argtable3.o
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o echosrv echosrv.o echosrv-conf.o argtable3.o $(LIBS)
|
||||
|
||||
$(MAN): sslh.pod Makefile
|
||||
pod2man --section=8 --release=$(VERSION) --center=" " sslh.pod | gzip -9 - > $(MAN)
|
||||
@@ -93,6 +101,16 @@ $(MAN): sslh.pod Makefile
|
||||
# configuration
|
||||
release:
|
||||
git archive master --prefix="sslh-$(VERSION)/" | gzip > /tmp/sslh-$(VERSION).tar.gz
|
||||
gpg --detach-sign --armor /tmp/sslh-$(VERSION).tar.gz
|
||||
|
||||
# Build docker image
|
||||
docker:
|
||||
docker image build -t "sslh:${VERSION}" .
|
||||
docker image tag "sslh:${VERSION}" sslh:latest
|
||||
|
||||
docker-clean:
|
||||
yes | docker image rm "sslh:${VERSION}" sslh:latest
|
||||
yes | docker image prune
|
||||
|
||||
# generic install: install binary and man page
|
||||
install: sslh $(MAN)
|
||||
@@ -112,7 +130,7 @@ uninstall:
|
||||
update-rc.d sslh remove
|
||||
|
||||
distclean: clean
|
||||
rm -f tags cscope.*
|
||||
rm -f tags sslh-conf.[ch] echosrv-conf.[ch] cscope.*
|
||||
|
||||
clean:
|
||||
rm -f sslh-fork sslh-select echosrv version.h $(MAN) systemd-sslh-generator *.o *.gcov *.gcno *.gcda *.png *.html *.css *.info
|
||||
|
||||
@@ -5,444 +5,94 @@ sslh -- A ssl/ssh multiplexer
|
||||
them further based on tests performed on the first data
|
||||
packet sent by the remote client.
|
||||
|
||||
Probes for HTTP, SSL, SSH, OpenVPN, tinc, XMPP are
|
||||
implemented, and any other protocol that can be tested using
|
||||
a regular expression, can be recognised. A typical use case
|
||||
is to allow serving several services on port 443 (e.g. to
|
||||
connect to SSH from inside a corporate firewall, which
|
||||
almost never block port 443) while still serving HTTPS on
|
||||
that port.
|
||||
Probes for HTTP, TLS/SSL (including SNI and ALPN), SSH,
|
||||
OpenVPN, tinc, XMPP, SOCKS5, are implemented, and any other
|
||||
protocol that can be tested using a regular expression, can
|
||||
be recognised. A typical use case is to allow serving
|
||||
several services on port 443 (e.g. to connect to SSH from
|
||||
inside a corporate firewall, which almost never block port
|
||||
443) while still serving HTTPS on that port.
|
||||
|
||||
Hence `sslh` acts as a protocol demultiplexer, or a
|
||||
switchboard. Its name comes from its original function to
|
||||
serve SSH and HTTPS on the same port.
|
||||
switchboard. With the SNI and ALPN probe, it makes a good
|
||||
front-end to a virtual host farm hosted behind a single IP
|
||||
address.
|
||||
|
||||
Compile and install
|
||||
===================
|
||||
`sslh` has the bells and whistles expected from a mature
|
||||
daemon: privilege and capabilities dropping, inetd support,
|
||||
systemd support, transparent proxying, chroot, logging,
|
||||
IPv4 and IPv6, TCP and UDP, a fork-based and a select-based
|
||||
model, and more.
|
||||
|
||||
Dependencies
|
||||
------------
|
||||
Install
|
||||
=======
|
||||
|
||||
`sslh` uses [libconfig](http://www.hyperrealm.com/libconfig/)
|
||||
and [libwrap](http://packages.debian.org/source/unstable/tcp-wrappers).
|
||||
|
||||
For Debian, these are contained in packages `libwrap0-dev` and
|
||||
`libconfig8-dev`.
|
||||
|
||||
For OpenSUSE, these are contained in packages libconfig9 and
|
||||
libconfig-dev in repository
|
||||
<http://download.opensuse.org/repositories/multimedia:/libs/openSUSE_12.1/>
|
||||
|
||||
For Fedora, you'll need packages `libconfig` and
|
||||
`libconfig-devel`:
|
||||
|
||||
yum install libconfig libconfig-devel
|
||||
|
||||
If you can't find `libconfig`, or just don't want a
|
||||
configuration file, set `USELIBCONFIG=` in the Makefile.
|
||||
|
||||
Compilation
|
||||
-----------
|
||||
|
||||
After this, the Makefile should work:
|
||||
|
||||
make install
|
||||
|
||||
There are a couple of configuration options at the beginning
|
||||
of the Makefile:
|
||||
|
||||
* `USELIBWRAP` compiles support for host access control (see
|
||||
`hosts_access(3)`), you will need `libwrap` headers and
|
||||
library to compile (`libwrap0-dev` in Debian).
|
||||
|
||||
* `USELIBCONFIG` compiles support for the configuration
|
||||
file. You will need `libconfig` headers to compile
|
||||
(`libconfig8-dev` in Debian).
|
||||
|
||||
* `USESYSTEMD` compiles support for using systemd socket activation.
|
||||
You will need `systemd` headers to compile (`systemd-devel` in Fedora).
|
||||
|
||||
Binaries
|
||||
--------
|
||||
|
||||
The Makefile produces two different executables: `sslh-fork`
|
||||
and `sslh-select`:
|
||||
|
||||
* `sslh-fork` forks a new process for each incoming connection.
|
||||
It is well-tested and very reliable, but incurs the overhead
|
||||
of many processes.
|
||||
If you are going to use `sslh` for a "small" setup (less than
|
||||
a dozen ssh connections and a low-traffic https server) then
|
||||
`sslh-fork` is probably more suited for you.
|
||||
|
||||
* `sslh-select` uses only one thread, which monitors all connections
|
||||
at once. It is more recent and less tested, but only incurs a 16
|
||||
byte overhead per connection. Also, if it stops, you'll lose all
|
||||
connections, which means you can't upgrade it remotely.
|
||||
If you are going to use `sslh` on a "medium" setup (a few thousand ssh
|
||||
connections, and another few thousand ssl connections),
|
||||
`sslh-select` will be better.
|
||||
|
||||
If you have a very large site (tens of thousands of connections),
|
||||
you'll need a vapourware version that would use libevent or
|
||||
something like that.
|
||||
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
* In general:
|
||||
|
||||
make
|
||||
cp sslh-fork /usr/local/sbin/sslh
|
||||
cp basic.cfg /etc/sslh.cfg
|
||||
vi /etc/sslh.cfg
|
||||
|
||||
* For Debian:
|
||||
|
||||
cp scripts/etc.init.d.sslh /etc/init.d/sslh
|
||||
|
||||
* For CentOS:
|
||||
|
||||
cp scripts/etc.rc.d.init.d.sslh.centos /etc/rc.d/init.d/sslh
|
||||
|
||||
|
||||
You might need to create links in /etc/rc<x>.d so that the server
|
||||
start automatically at boot-up, e.g. under Debian:
|
||||
|
||||
update-rc.d sslh defaults
|
||||
Please refer to the [install guide](doc/INSTALL.md).
|
||||
|
||||
|
||||
Configuration
|
||||
=============
|
||||
|
||||
If you use the scripts provided, sslh will get its
|
||||
configuration from /etc/sslh.cfg. Please refer to
|
||||
example.cfg for an overview of all the settings.
|
||||
Please refer to the [configuration guide](doc/config.md).
|
||||
|
||||
A good scheme is to use the external name of the machine in
|
||||
`listen`, and bind `httpd` to `localhost:443` (instead of all
|
||||
binding to all interfaces): that way, HTTPS connections
|
||||
coming from inside your network don't need to go through
|
||||
`sslh`, and `sslh` is only there as a frontal for connections
|
||||
coming from the internet.
|
||||
|
||||
Note that 'external name' in this context refers to the
|
||||
actual IP address of the machine as seen from your network,
|
||||
i.e. that that is not `127.0.0.1` in the output of
|
||||
`ifconfig(8)`.
|
||||
|
||||
Libwrap support
|
||||
---------------
|
||||
Docker image
|
||||
------------
|
||||
|
||||
Sslh can optionally perform `libwrap` checks for the sshd
|
||||
service: because the connection to `sshd` will be coming
|
||||
locally from `sslh`, `sshd` cannot determine the IP of the
|
||||
client.
|
||||
How to use
|
||||
|
||||
OpenVPN support
|
||||
---------------
|
||||
---
|
||||
|
||||
OpenVPN clients connecting to OpenVPN running with
|
||||
`-port-share` reportedly take more than one second between
|
||||
the time the TCP connection is established and the time they
|
||||
send the first data packet. This results in `sslh` with
|
||||
default settings timing out and assuming an SSH connection.
|
||||
To support OpenVPN connections reliably, it is necessary to
|
||||
increase `sslh`'s timeout to 5 seconds.
|
||||
Build docker image
|
||||
|
||||
Instead of using OpenVPN's port sharing, it is more reliable
|
||||
to use `sslh`'s `--openvpn` option to get `sslh` to do the
|
||||
port sharing.
|
||||
make docker
|
||||
|
||||
Using proxytunnel with sslh
|
||||
---------------------------
|
||||
|
||||
If you are connecting through a proxy that checks that the
|
||||
outgoing connection really is SSL and rejects SSH, you can
|
||||
encapsulate all your traffic in SSL using `proxytunnel` (this
|
||||
should work with `corkscrew` as well). On the server side you
|
||||
receive the traffic with `stunnel` to decapsulate SSL, then
|
||||
pipe through `sslh` to switch HTTP on one side and SSL on the
|
||||
other.
|
||||
```bash
|
||||
docker container run \
|
||||
--rm \
|
||||
-it \
|
||||
--listen=0.0.0.0:443 \
|
||||
--ssh=hostname:22 \
|
||||
--tlshostname:443 \
|
||||
sslh:latest
|
||||
```
|
||||
|
||||
In that case, you end up with something like this:
|
||||
docker-compose example
|
||||
|
||||
ssh -> proxytunnel -e ----[ssh/ssl]---> stunnel ---[ssh]---> sslh --> sshd
|
||||
Web browser -------------[http/ssl]---> stunnel ---[http]--> sslh --> httpd
|
||||
```
|
||||
---
|
||||
version: "3"
|
||||
|
||||
Configuration goes like this on the server side, using `stunnel3`:
|
||||
services:
|
||||
sslh:
|
||||
image: sslh:latest
|
||||
hostname: sslh
|
||||
ports:
|
||||
- 443:443/tcp
|
||||
command: --listen=0.0.0.0:443 --tlshostname:443 --openvpn=openvpn:1194
|
||||
depends_on:
|
||||
- nginx
|
||||
- openvpn
|
||||
|
||||
stunnel -f -p mycert.pem -d thelonious:443 -l /usr/local/sbin/sslh -- \
|
||||
sslh -i --http localhost:80 --ssh localhost:22
|
||||
nginx:
|
||||
image: nginx
|
||||
hostname: nginx
|
||||
|
||||
* stunnel options:
|
||||
* `-f` for foreground/debugging
|
||||
* `-p` for specifying the key and certificate
|
||||
* `-d` for specifying which interface and port
|
||||
we're listening to for incoming connexions
|
||||
* `-l` summons `sslh` in inetd mode.
|
||||
|
||||
* sslh options:
|
||||
* `-i` for inetd mode
|
||||
* `--http` to forward HTTP connexions to port 80,
|
||||
and SSH connexions to port 22.
|
||||
|
||||
Capabilities support
|
||||
--------------------
|
||||
|
||||
On Linux (only?), you can compile sslh with `USELIBCAP=1` to
|
||||
make use of POSIX capabilities; this will save the required
|
||||
capabilities needed for transparent proxying for unprivileged
|
||||
processes.
|
||||
|
||||
Alternatively, you may use filesystem capabilities instead
|
||||
of starting sslh as root and asking it to drop privileges.
|
||||
You will need `CAP_NET_BIND_SERVICE` for listening on port 443
|
||||
and `CAP_NET_ADMIN` for transparent proxying (see
|
||||
`capabilities(7)`).
|
||||
|
||||
You can use the `setcap(8)` utility to give these capabilities
|
||||
to the executable:
|
||||
|
||||
sudo setcap cap_net_bind_service,cap_net_admin+pe sslh-select
|
||||
|
||||
Then you can run sslh-select as an unpriviledged user, e.g.:
|
||||
|
||||
sslh-select -p myname:443 --ssh localhost:22 --ssl localhost:443
|
||||
|
||||
Caveat: `CAP_NET_ADMIN` does give sslh too many rights, e.g.
|
||||
configuring the interface. If you're not going to use
|
||||
transparent proxying, just don't use it (or use the libcap method).
|
||||
|
||||
Transparent proxy support
|
||||
-------------------------
|
||||
|
||||
On Linux and FreeBSD you can use the `--transparent` option to
|
||||
request transparent proxying. This means services behind `sslh`
|
||||
(Apache, `sshd` and so on) will see the external IP and ports
|
||||
as if the external world connected directly to them. This
|
||||
simplifies IP-based access control (or makes it possible at
|
||||
all).
|
||||
|
||||
Linux:
|
||||
|
||||
`sslh` needs extended rights to perform this: you'll need to
|
||||
give it `CAP_NET_ADMIN` capabilities (see appropriate chapter)
|
||||
or run it as root (but don't do that).
|
||||
|
||||
The firewalling tables also need to be adjusted as follows.
|
||||
I don't think it is possible to have `httpd` and `sslh` both listen to 443 in
|
||||
this scheme -- let me know if you manage that:
|
||||
|
||||
# Set route_localnet = 1 on all interfaces so that ssl can use "localhost" as destination
|
||||
sysctl -w net.ipv4.conf.default.route_localnet=1
|
||||
sysctl -w net.ipv4.conf.all.route_localnet=1
|
||||
|
||||
# DROP martian packets as they would have been if route_localnet was zero
|
||||
# Note: packets not leaving the server aren't affected by this, thus sslh will still work
|
||||
iptables -t raw -A PREROUTING ! -i lo -d 127.0.0.0/8 -j DROP
|
||||
iptables -t mangle -A POSTROUTING ! -o lo -s 127.0.0.0/8 -j DROP
|
||||
|
||||
# Mark all connections made by ssl for special treatment (here sslh is run as user "sslh")
|
||||
iptables -t nat -A OUTPUT -m owner --uid-owner sslh -p tcp --tcp-flags FIN,SYN,RST,ACK SYN -j CONNMARK --set-xmark 0x01/0x0f
|
||||
|
||||
# Outgoing packets that should go to sslh instead have to be rerouted, so mark them accordingly (copying over the connection mark)
|
||||
iptables -t mangle -A OUTPUT ! -o lo -p tcp -m connmark --mark 0x01/0x0f -j CONNMARK --restore-mark --mask 0x0f
|
||||
|
||||
# Configure routing for those marked packets
|
||||
ip rule add fwmark 0x1 lookup 100
|
||||
ip route add local 0.0.0.0/0 dev lo table 100
|
||||
|
||||
Tranparent proxying with IPv6 is similarly set up as follows:
|
||||
|
||||
# Set route_localnet = 1 on all interfaces so that ssl can use "localhost" as destination
|
||||
# Not sure if this is needed for ipv6 though
|
||||
sysctl -w net.ipv4.conf.default.route_localnet=1
|
||||
sysctl -w net.ipv4.conf.all.route_localnet=1
|
||||
|
||||
# DROP martian packets as they would have been if route_localnet was zero
|
||||
# Note: packets not leaving the server aren't affected by this, thus sslh will still work
|
||||
ip6tables -t raw -A PREROUTING ! -i lo -d ::1/128 -j DROP
|
||||
ip6tables -t mangle -A POSTROUTING ! -o lo -s ::1/128 -j DROP
|
||||
|
||||
# Mark all connections made by ssl for special treatment (here sslh is run as user "sslh")
|
||||
ip6tables -t nat -A OUTPUT -m owner --uid-owner sslh -p tcp --tcp-flags FIN,SYN,RST,ACK SYN -j CONNMARK --set-xmark 0x01/0x0f
|
||||
|
||||
# Outgoing packets that should go to sslh instead have to be rerouted, so mark them accordingly (copying over the connection mark)
|
||||
ip6tables -t mangle -A OUTPUT ! -o lo -p tcp -m connmark --mark 0x01/0x0f -j CONNMARK --restore-mark --mask 0x0f
|
||||
|
||||
# Configure routing for those marked packets
|
||||
ip -6 rule add fwmark 0x1 lookup 100
|
||||
ip -6 route add local ::/0 dev lo table 100
|
||||
|
||||
Explanation:
|
||||
To be able to use `localhost` as destination in your sslh config along with transparent proxying
|
||||
you have to allow routing of loopback addresses as done above.
|
||||
This is something you usually should not do (see [this stackoverflow post](https://serverfault.com/questions/656279/how-to-force-linux-to-accept-packet-with-loopback-ip/656484#656484))
|
||||
The two `DROP` iptables rules emulate the behaviour of `route_localnet` set to off (with one small difference:
|
||||
allowing the reroute-check to happen after the fwmark is set on packets destined for sslh).
|
||||
See [this diagram](https://upload.wikimedia.org/wikipedia/commons/3/37/Netfilter-packet-flow.svg) for a good visualisation
|
||||
showing how packets will traverse the iptables chains.
|
||||
|
||||
Note:
|
||||
You have to run `sslh` as dedicated user (in this example the user is also named `sslh`), to not mess up with your normal networking.
|
||||
These rules will allow you to connect directly to ssh on port
|
||||
22 (or to any other service behind sslh) as well as through sslh on port 443.
|
||||
|
||||
Also remember that iptables configuration and ip routes and
|
||||
rules won't be necessarily persisted after you reboot. Make
|
||||
sure to save them properly. For example in CentOS7, you would
|
||||
do `iptables-save > /etc/sysconfig/iptables`, and add both
|
||||
`ip` commands to your `/etc/rc.local`.
|
||||
|
||||
FreeBSD:
|
||||
|
||||
Given you have no firewall defined yet, you can use the following configuration
|
||||
to have ipfw properly redirect traffic back to sslh
|
||||
|
||||
/etc/rc.conf
|
||||
firewall_enable="YES"
|
||||
firewall_type="open"
|
||||
firewall_logif="YES"
|
||||
firewall_coscripts="/etc/ipfw/sslh.rules"
|
||||
|
||||
|
||||
/etc/ipfw/sslh.rules
|
||||
|
||||
#! /bin/sh
|
||||
|
||||
# ssl
|
||||
ipfw add 20000 fwd 192.0.2.1,443 log tcp from 192.0.2.1 8443 to any out
|
||||
ipfw add 20010 fwd 2001:db8::1,443 log tcp from 2001:db8::1 8443 to any out
|
||||
|
||||
# ssh
|
||||
ipfw add 20100 fwd 192.0.2.1,443 log tcp from 192.0.2.1 8022 to any out
|
||||
ipfw add 20110 fwd 2001:db8::1,443 log tcp from 2001:db8::1 8022 to any out
|
||||
|
||||
# xmpp
|
||||
ipfw add 20200 fwd 192.0.2.1,443 log tcp from 192.0.2.1 5222 to any out
|
||||
ipfw add 20210 fwd 2001:db8::1,443 log tcp from 2001:db8::1 5222 to any out
|
||||
|
||||
# openvpn (running on other internal system)
|
||||
ipfw add 20300 fwd 192.0.2.1,443 log tcp from 198.51.100.7 1194 to any out
|
||||
ipfw add 20310 fwd 2001:db8::1,443 log tcp from 2001:db8:1::7 1194 to any out
|
||||
|
||||
General notes:
|
||||
|
||||
|
||||
This will only work if `sslh` does not use any loopback
|
||||
addresses (no `127.0.0.1` or `localhost`), you'll need to use
|
||||
explicit IP addresses (or names):
|
||||
|
||||
sslh --listen 192.168.0.1:443 --ssh 192.168.0.1:22 --ssl 192.168.0.1:4443
|
||||
|
||||
This will not work:
|
||||
|
||||
sslh --listen 192.168.0.1:443 --ssh 127.0.0.1:22 --ssl 127.0.0.1:4443
|
||||
|
||||
Transparent proxying means the target server sees the real
|
||||
origin address, so it means if the client connects using
|
||||
IPv6, the server must also support IPv6. It is easy to
|
||||
support both IPv4 and IPv6 by configuring the server
|
||||
accordingly, and setting `sslh` to connect to a name that
|
||||
resolves to both IPv4 and IPv6, e.g.:
|
||||
|
||||
sslh --transparent --listen <extaddr>:443 --ssh insideaddr:22
|
||||
|
||||
/etc/hosts:
|
||||
192.168.0.1 insideaddr
|
||||
201::::2 insideaddr
|
||||
|
||||
Upon incoming IPv6 connection, `sslh` will first try to
|
||||
connect to the IPv4 address (which will fail), then connect
|
||||
to the IPv6 address.
|
||||
|
||||
Systemd Socket Activation
|
||||
-------------------------
|
||||
If compiled with `USESYSTEMD` then it is possible to activate
|
||||
the service on demand and avoid running any code as root.
|
||||
|
||||
In this mode any listen configuration options are ignored and
|
||||
the sockets are passed by systemd to the service.
|
||||
|
||||
Example socket unit:
|
||||
|
||||
[Unit]
|
||||
Before=sslh.service
|
||||
|
||||
[Socket]
|
||||
ListenStream=1.2.3.4:443
|
||||
ListenStream=5.6.7.8:444
|
||||
ListenStream=9.10.11.12:445
|
||||
FreeBind=true
|
||||
|
||||
[Install]
|
||||
WantedBy=sockets.target
|
||||
|
||||
Example service unit:
|
||||
|
||||
[Unit]
|
||||
PartOf=sslh.socket
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/sbin/sslh -v -f --ssh 127.0.0.1:22 --ssl 127.0.0.1:443
|
||||
KillMode=process
|
||||
CapabilityBoundingSet=CAP_NET_BIND_SERVICE CAP_NET_ADMIN CAP_SETGID CAP_SETUID
|
||||
PrivateTmp=true
|
||||
PrivateDevices=true
|
||||
ProtectSystem=full
|
||||
ProtectHome=true
|
||||
User=sslh
|
||||
|
||||
|
||||
With this setup only the socket needs to be enabled. The sslh service
|
||||
will be started on demand and does not need to run as root to bind the
|
||||
sockets as systemd has already bound and passed them over. If the sslh
|
||||
service is started on its own without the sockets being passed by systemd
|
||||
then it will look to use those defined on the command line or config
|
||||
file as usual. Any number of ListenStreams can be defined in the socket
|
||||
file and systemd will pass them all over to sslh to use as usual.
|
||||
|
||||
To avoid inconsistency between starting via socket and starting directly
|
||||
via the service Requires=sslh.socket can be added to the service unit to
|
||||
mandate the use of the socket configuration.
|
||||
|
||||
Rather than overwriting the entire socket file drop in values can be placed
|
||||
in /etc/systemd/system/sslh.socket.d/<name>.conf with additional ListenStream
|
||||
values that will be merged.
|
||||
|
||||
In addition to the above with manual .socket file configuration there is an
|
||||
optional systemd generator which can be compiled - systemd-sslh-generator
|
||||
|
||||
This parses the /etc/sslh.cfg (or /etc/sslh/sslh.cfg file if that exists
|
||||
instead) configuration file and dynamically generates a socket file to use.
|
||||
|
||||
This will also merge with any sslh.socket.d drop in configuration but will be
|
||||
overriden by a /etc/systemd/system/sslh.socket file.
|
||||
|
||||
To use the generator place it in /usr/lib/systemd/system-generators and then
|
||||
call systemctl daemon-reload after any changes to /etc/sslh.cfg to generate
|
||||
the new dynamic socket unit.
|
||||
|
||||
Fail2ban
|
||||
--------
|
||||
|
||||
If using transparent proxying, just use the standard ssh
|
||||
rules. If you can't or don't want to use transparent
|
||||
proxying, you can set `fail2ban` rules to block repeated ssh
|
||||
connections from an IP address (obviously this depends
|
||||
on the site, there might be legitimate reasons you would get
|
||||
many connections to ssh from the same IP address...)
|
||||
|
||||
See example files in scripts/fail2ban.
|
||||
openvpn:
|
||||
image: openvpn:latest
|
||||
hostname: openvpn
|
||||
```
|
||||
|
||||
Comments? Questions?
|
||||
====================
|
||||
|
||||
You can subscribe to the `sslh` mailing list here:
|
||||
<http://rutschle.net/cgi-bin/mailman/listinfo/sslh>
|
||||
<https://lists.rutschle.net/mailman/listinfo/sslh>
|
||||
|
||||
This mailing list should be used for discussion, feature
|
||||
requests, and will be the prefered channel for announcements.
|
||||
requests, and will be the preferred channel for announcements.
|
||||
|
||||
Of course, check the [FAQ](doc/FAQ.md) first!
|
||||
|
||||
+6006
File diff suppressed because it is too large.
Load diff
+273
@@ -0,0 +1,273 @@
|
||||
/*******************************************************************************
|
||||
* argtable3: Declares the main interfaces of the library
|
||||
*
|
||||
* This file is part of the argtable3 library.
|
||||
*
|
||||
* Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
|
||||
* <sheitmann@users.sourceforge.net>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of STEWART HEITMANN nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef ARGTABLE3
|
||||
#define ARGTABLE3
|
||||
|
||||
#include <stdio.h> /* FILE */
|
||||
#include <time.h> /* struct tm */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ARG_REX_ICASE 1
|
||||
#define ARG_DSTR_SIZE 200
|
||||
#define ARG_CMD_NAME_LEN 100
|
||||
#define ARG_CMD_DESCRIPTION_LEN 256
|
||||
|
||||
#ifndef ARG_REPLACE_GETOPT
|
||||
#define ARG_REPLACE_GETOPT 1 /* use the embedded getopt as the system getopt(3) */
|
||||
#endif /* ARG_REPLACE_GETOPT */
|
||||
|
||||
/* bit masks for arg_hdr.flag */
|
||||
enum { ARG_TERMINATOR = 0x1, ARG_HASVALUE = 0x2, ARG_HASOPTVALUE = 0x4 };
|
||||
|
||||
#if defined(_WIN32)
|
||||
#if defined(argtable3_EXPORTS)
|
||||
#define ARG_EXTERN __declspec(dllexport)
|
||||
#elif defined(argtable3_IMPORTS)
|
||||
#define ARG_EXTERN __declspec(dllimport)
|
||||
#else
|
||||
#define ARG_EXTERN
|
||||
#endif
|
||||
#else
|
||||
#define ARG_EXTERN
|
||||
#endif
|
||||
|
||||
typedef struct _internal_arg_dstr* arg_dstr_t;
|
||||
typedef void* arg_cmd_itr_t;
|
||||
|
||||
typedef void(arg_resetfn)(void* parent);
|
||||
typedef int(arg_scanfn)(void* parent, const char* argval);
|
||||
typedef int(arg_checkfn)(void* parent);
|
||||
typedef void(arg_errorfn)(void* parent, arg_dstr_t ds, int error, const char* argval, const char* progname);
|
||||
typedef void(arg_dstr_freefn)(char* buf);
|
||||
typedef int(arg_cmdfn)(int argc, char* argv[], arg_dstr_t res);
|
||||
typedef int(arg_comparefn)(const void* k1, const void* k2);
|
||||
|
||||
/*
|
||||
* The arg_hdr struct defines properties that are common to all arg_xxx structs.
|
||||
* The argtable library requires each arg_xxx struct to have an arg_hdr
|
||||
* struct as its first data member.
|
||||
* The argtable library functions then use this data to identify the
|
||||
* properties of the command line option, such as its option tags,
|
||||
* datatype string, and glossary strings, and so on.
|
||||
* Moreover, the arg_hdr struct contains pointers to custom functions that
|
||||
* are provided by each arg_xxx struct which perform the tasks of parsing
|
||||
* that particular arg_xxx arguments, performing post-parse checks, and
|
||||
* reporting errors.
|
||||
* These functions are private to the individual arg_xxx source code
|
||||
* and are the pointer to them are initiliased by that arg_xxx struct's
|
||||
* constructor function. The user could alter them after construction
|
||||
* if desired, but the original intention is for them to be set by the
|
||||
* constructor and left unaltered.
|
||||
*/
|
||||
typedef struct arg_hdr {
|
||||
char flag; /* Modifier flags: ARG_TERMINATOR, ARG_HASVALUE. */
|
||||
const char* shortopts; /* String defining the short options */
|
||||
const char* longopts; /* String defiing the long options */
|
||||
const char* datatype; /* Description of the argument data type */
|
||||
const char* glossary; /* Description of the option as shown by arg_print_glossary function */
|
||||
int mincount; /* Minimum number of occurences of this option accepted */
|
||||
int maxcount; /* Maximum number of occurences if this option accepted */
|
||||
void* parent; /* Pointer to parent arg_xxx struct */
|
||||
arg_resetfn* resetfn; /* Pointer to parent arg_xxx reset function */
|
||||
arg_scanfn* scanfn; /* Pointer to parent arg_xxx scan function */
|
||||
arg_checkfn* checkfn; /* Pointer to parent arg_xxx check function */
|
||||
arg_errorfn* errorfn; /* Pointer to parent arg_xxx error function */
|
||||
void* priv; /* Pointer to private header data for use by arg_xxx functions */
|
||||
} arg_hdr_t;
|
||||
|
||||
typedef struct arg_rem {
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
} arg_rem_t;
|
||||
|
||||
typedef struct arg_lit {
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
} arg_lit_t;
|
||||
|
||||
typedef struct arg_int {
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
int* ival; /* Array of parsed argument values */
|
||||
} arg_int_t;
|
||||
|
||||
typedef struct arg_dbl {
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
double* dval; /* Array of parsed argument values */
|
||||
} arg_dbl_t;
|
||||
|
||||
typedef struct arg_str {
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
const char** sval; /* Array of parsed argument values */
|
||||
} arg_str_t;
|
||||
|
||||
typedef struct arg_rex {
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
const char** sval; /* Array of parsed argument values */
|
||||
} arg_rex_t;
|
||||
|
||||
typedef struct arg_file {
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args*/
|
||||
const char** filename; /* Array of parsed filenames (eg: /home/foo.bar) */
|
||||
const char** basename; /* Array of parsed basenames (eg: foo.bar) */
|
||||
const char** extension; /* Array of parsed extensions (eg: .bar) */
|
||||
} arg_file_t;
|
||||
|
||||
typedef struct arg_date {
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
const char* format; /* strptime format string used to parse the date */
|
||||
int count; /* Number of matching command line args */
|
||||
struct tm* tmval; /* Array of parsed time values */
|
||||
} arg_date_t;
|
||||
|
||||
enum { ARG_ELIMIT = 1, ARG_EMALLOC, ARG_ENOMATCH, ARG_ELONGOPT, ARG_EMISSARG };
|
||||
typedef struct arg_end {
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of errors encountered */
|
||||
int* error; /* Array of error codes */
|
||||
void** parent; /* Array of pointers to offending arg_xxx struct */
|
||||
const char** argval; /* Array of pointers to offending argv[] string */
|
||||
} arg_end_t;
|
||||
|
||||
typedef struct arg_cmd_info {
|
||||
char name[ARG_CMD_NAME_LEN];
|
||||
char description[ARG_CMD_DESCRIPTION_LEN];
|
||||
arg_cmdfn* proc;
|
||||
} arg_cmd_info_t;
|
||||
|
||||
/**** arg_xxx constructor functions *********************************/
|
||||
|
||||
ARG_EXTERN struct arg_rem* arg_rem(const char* datatype, const char* glossary);
|
||||
|
||||
ARG_EXTERN struct arg_lit* arg_lit0(const char* shortopts, const char* longopts, const char* glossary);
|
||||
ARG_EXTERN struct arg_lit* arg_lit1(const char* shortopts, const char* longopts, const char* glossary);
|
||||
ARG_EXTERN struct arg_lit* arg_litn(const char* shortopts, const char* longopts, int mincount, int maxcount, const char* glossary);
|
||||
|
||||
ARG_EXTERN struct arg_int* arg_int0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||
ARG_EXTERN struct arg_int* arg_int1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||
ARG_EXTERN struct arg_int* arg_intn(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary);
|
||||
|
||||
ARG_EXTERN struct arg_dbl* arg_dbl0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||
ARG_EXTERN struct arg_dbl* arg_dbl1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||
ARG_EXTERN struct arg_dbl* arg_dbln(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary);
|
||||
|
||||
ARG_EXTERN struct arg_str* arg_str0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||
ARG_EXTERN struct arg_str* arg_str1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||
ARG_EXTERN struct arg_str* arg_strn(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary);
|
||||
|
||||
ARG_EXTERN struct arg_rex* arg_rex0(const char* shortopts, const char* longopts, const char* pattern, const char* datatype, int flags, const char* glossary);
|
||||
ARG_EXTERN struct arg_rex* arg_rex1(const char* shortopts, const char* longopts, const char* pattern, const char* datatype, int flags, const char* glossary);
|
||||
ARG_EXTERN struct arg_rex* arg_rexn(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* pattern,
|
||||
const char* datatype,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
int flags,
|
||||
const char* glossary);
|
||||
|
||||
ARG_EXTERN struct arg_file* arg_file0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||
ARG_EXTERN struct arg_file* arg_file1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary);
|
||||
ARG_EXTERN struct arg_file* arg_filen(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary);
|
||||
|
||||
ARG_EXTERN struct arg_date* arg_date0(const char* shortopts, const char* longopts, const char* format, const char* datatype, const char* glossary);
|
||||
ARG_EXTERN struct arg_date* arg_date1(const char* shortopts, const char* longopts, const char* format, const char* datatype, const char* glossary);
|
||||
ARG_EXTERN struct arg_date* arg_daten(const char* shortopts, const char* longopts, const char* format, const char* datatype, int mincount, int maxcount, const char* glossary);
|
||||
|
||||
ARG_EXTERN struct arg_end* arg_end(int maxerrors);
|
||||
|
||||
#define ARG_DSTR_STATIC ((arg_dstr_freefn*)0)
|
||||
#define ARG_DSTR_VOLATILE ((arg_dstr_freefn*)1)
|
||||
#define ARG_DSTR_DYNAMIC ((arg_dstr_freefn*)3)
|
||||
|
||||
/**** other functions *******************************************/
|
||||
ARG_EXTERN int arg_nullcheck(void** argtable);
|
||||
ARG_EXTERN int arg_parse(int argc, char** argv, void** argtable);
|
||||
ARG_EXTERN void arg_print_option(FILE* fp, const char* shortopts, const char* longopts, const char* datatype, const char* suffix);
|
||||
ARG_EXTERN void arg_print_syntax(FILE* fp, void** argtable, const char* suffix);
|
||||
ARG_EXTERN void arg_print_syntaxv(FILE* fp, void** argtable, const char* suffix);
|
||||
ARG_EXTERN void arg_print_glossary(FILE* fp, void** argtable, const char* format);
|
||||
ARG_EXTERN void arg_print_glossary_gnu(FILE* fp, void** argtable);
|
||||
ARG_EXTERN void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname);
|
||||
ARG_EXTERN void arg_print_option_ds(arg_dstr_t ds, const char* shortopts, const char* longopts, const char* datatype, const char* suffix);
|
||||
ARG_EXTERN void arg_print_syntax_ds(arg_dstr_t ds, void** argtable, const char* suffix);
|
||||
ARG_EXTERN void arg_print_syntaxv_ds(arg_dstr_t ds, void** argtable, const char* suffix);
|
||||
ARG_EXTERN void arg_print_glossary_ds(arg_dstr_t ds, void** argtable, const char* format);
|
||||
ARG_EXTERN void arg_print_glossary_gnu_ds(arg_dstr_t ds, void** argtable);
|
||||
ARG_EXTERN void arg_print_errors_ds(arg_dstr_t ds, struct arg_end* end, const char* progname);
|
||||
ARG_EXTERN void arg_freetable(void** argtable, size_t n);
|
||||
|
||||
ARG_EXTERN arg_dstr_t arg_dstr_create(void);
|
||||
ARG_EXTERN void arg_dstr_destroy(arg_dstr_t ds);
|
||||
ARG_EXTERN void arg_dstr_reset(arg_dstr_t ds);
|
||||
ARG_EXTERN void arg_dstr_free(arg_dstr_t ds);
|
||||
ARG_EXTERN void arg_dstr_set(arg_dstr_t ds, char* str, arg_dstr_freefn* free_proc);
|
||||
ARG_EXTERN void arg_dstr_cat(arg_dstr_t ds, const char* str);
|
||||
ARG_EXTERN void arg_dstr_catc(arg_dstr_t ds, char c);
|
||||
ARG_EXTERN void arg_dstr_catf(arg_dstr_t ds, const char* fmt, ...);
|
||||
ARG_EXTERN char* arg_dstr_cstr(arg_dstr_t ds);
|
||||
|
||||
ARG_EXTERN void arg_cmd_init(void);
|
||||
ARG_EXTERN void arg_cmd_uninit(void);
|
||||
ARG_EXTERN void arg_cmd_register(const char* name, arg_cmdfn* proc, const char* description);
|
||||
ARG_EXTERN void arg_cmd_unregister(const char* name);
|
||||
ARG_EXTERN int arg_cmd_dispatch(const char* name, int argc, char* argv[], arg_dstr_t res);
|
||||
ARG_EXTERN unsigned int arg_cmd_count(void);
|
||||
ARG_EXTERN arg_cmd_info_t* arg_cmd_info(const char* name);
|
||||
ARG_EXTERN arg_cmd_itr_t arg_cmd_itr_create(void);
|
||||
ARG_EXTERN void arg_cmd_itr_destroy(arg_cmd_itr_t itr);
|
||||
ARG_EXTERN int arg_cmd_itr_advance(arg_cmd_itr_t itr);
|
||||
ARG_EXTERN char* arg_cmd_itr_key(arg_cmd_itr_t itr);
|
||||
ARG_EXTERN arg_cmd_info_t* arg_cmd_itr_value(arg_cmd_itr_t itr);
|
||||
ARG_EXTERN int arg_cmd_itr_search(arg_cmd_itr_t itr, void* k);
|
||||
ARG_EXTERN void arg_mgsort(void* data, int size, int esize, int i, int k, arg_comparefn* comparefn);
|
||||
ARG_EXTERN void arg_make_get_help_msg(arg_dstr_t res);
|
||||
ARG_EXTERN void arg_make_help_msg(arg_dstr_t ds, char* cmd_name, void** argtable);
|
||||
ARG_EXTERN void arg_make_syntax_err_msg(arg_dstr_t ds, void** argtable, struct arg_end* end);
|
||||
ARG_EXTERN int arg_make_syntax_err_help_msg(arg_dstr_t ds, char* name, int help, int nerrors, void** argtable, struct arg_end* end, int* exitcode);
|
||||
ARG_EXTERN void arg_set_module_name(const char* name);
|
||||
ARG_EXTERN void arg_set_module_version(int major, int minor, int patch, const char* tag);
|
||||
|
||||
/**** deprecated functions, for back-compatibility only ********/
|
||||
ARG_EXTERN void arg_free(void** argtable);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -1,7 +1,7 @@
|
||||
# This is a basic configuration file that should provide
|
||||
# sensible values for "standard" setup.
|
||||
|
||||
verbose: false;
|
||||
verbose: 0;
|
||||
foreground: false;
|
||||
inetd: false;
|
||||
numeric: false;
|
||||
@@ -24,7 +24,7 @@ protocols:
|
||||
{ name: "openvpn"; host: "localhost"; port: "1194"; },
|
||||
{ name: "xmpp"; host: "localhost"; port: "5222"; },
|
||||
{ name: "http"; host: "localhost"; port: "80"; },
|
||||
{ name: "ssl"; host: "localhost"; port: "443"; log_level: 0; },
|
||||
{ name: "tls"; host: "localhost"; port: "443"; log_level: 0; },
|
||||
{ name: "anyprot"; host: "localhost"; port: "443"; }
|
||||
);
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
collection.c: management of a collection of connections, for sslh-select
|
||||
|
||||
# Copyright (C) 2021 Yves Rutschle
|
||||
#
|
||||
# This program is free software; you can redistribute it
|
||||
# and/or modify it under the terms of the GNU General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2 of the License, or (at your option) any later
|
||||
# version.
|
||||
#
|
||||
# This program 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 General Public License for more
|
||||
# details.
|
||||
#
|
||||
# The full text for the General Public License is here:
|
||||
# http://www.gnu.org/licenses/gpl.html
|
||||
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include "collection.h"
|
||||
#include "sslh-conf.h"
|
||||
#include "gap.h"
|
||||
|
||||
/* Info to keep track of all connections */
|
||||
struct cnx_collection {
|
||||
gap_array* fd2cnx; /* Array indexed by file descriptor to things in cnx[] */
|
||||
};
|
||||
|
||||
/* Allocates and initialises a new collection of connections with at least
|
||||
* `len` elements. */
|
||||
cnx_collection* collection_init(int len)
|
||||
{
|
||||
cnx_collection* collection;
|
||||
|
||||
collection = malloc(sizeof(*collection));
|
||||
CHECK_ALLOC(collection, "collection_init(collection)");
|
||||
|
||||
memset(collection, 0, sizeof(*collection));
|
||||
|
||||
collection->fd2cnx = gap_init(len);
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
/* Caveat: might not work, as has never been used */
|
||||
void collection_destroy(cnx_collection* collection)
|
||||
{
|
||||
/* Caveat 2: no code to free connections yet */
|
||||
gap_destroy(collection->fd2cnx);
|
||||
free(collection);
|
||||
}
|
||||
|
||||
/* Points the file descriptor to the specified connection index */
|
||||
int collection_add_fd(cnx_collection* collection, struct connection* cnx, int fd)
|
||||
{
|
||||
gap_set(collection->fd2cnx, fd, cnx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Allocates a connection and inits it with specified file descriptor */
|
||||
struct connection* collection_alloc_cnx_from_fd(struct cnx_collection* collection, int fd)
|
||||
{
|
||||
struct connection* cnx = malloc(sizeof(*cnx));
|
||||
|
||||
if (!cnx) return NULL;
|
||||
|
||||
init_cnx(cnx);
|
||||
cnx->type = SOCK_STREAM;
|
||||
cnx->q[0].fd = fd;
|
||||
cnx->state = ST_PROBING;
|
||||
cnx->probe_timeout = time(NULL) + cfg.timeout;
|
||||
|
||||
gap_set(collection->fd2cnx, fd, cnx);
|
||||
|
||||
return cnx;
|
||||
}
|
||||
|
||||
/* Remove a connection from the collection */
|
||||
int collection_remove_cnx(cnx_collection* collection, struct connection *cnx)
|
||||
{
|
||||
if (cnx->q[0].fd != -1)
|
||||
gap_set(collection->fd2cnx, cnx->q[0].fd, NULL);
|
||||
if (cnx->q[1].fd != -1)
|
||||
gap_set(collection->fd2cnx, cnx->q[1].fd, NULL);
|
||||
free(cnx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Returns the connection that contains the file descriptor */
|
||||
struct connection* collection_get_cnx_from_fd(struct cnx_collection* collection, int fd)
|
||||
{
|
||||
return gap_get(collection->fd2cnx, fd);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef COLLECTION_H
|
||||
#define COLLECTION_H
|
||||
|
||||
typedef struct cnx_collection cnx_collection;
|
||||
|
||||
|
||||
cnx_collection* collection_init(int len);
|
||||
void collection_destroy(cnx_collection* collection);
|
||||
|
||||
struct connection* collection_alloc_cnx_from_fd(cnx_collection* collection, int fd);
|
||||
int collection_add_fd(cnx_collection* collection, struct connection* cnx, int fd);
|
||||
|
||||
/* Remove a connection from the collection */
|
||||
int collection_remove_cnx(cnx_collection* collection, struct connection *cnx);
|
||||
|
||||
struct connection* collection_get_cnx_from_fd(struct cnx_collection* collection, int fd);
|
||||
|
||||
#endif
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
#include "common.h"
|
||||
#include "probe.h"
|
||||
#include "sslh-conf.h"
|
||||
|
||||
/* Added to make the code compilable under CYGWIN
|
||||
* */
|
||||
@@ -29,22 +30,20 @@
|
||||
#include <systemd/sd-daemon.h>
|
||||
#endif
|
||||
|
||||
#ifdef LIBBSD
|
||||
#include <bsd/unistd.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Settings that depend on the command line. They're set in main(), but also
|
||||
* used in other places in common.c, and it'd be heavy-handed to pass it all as
|
||||
* parameters
|
||||
* Settings that depend on the command line or the config file
|
||||
*/
|
||||
int verbose = 0;
|
||||
int probing_timeout = 2;
|
||||
int inetd = 0;
|
||||
int foreground = 0;
|
||||
int background = 0;
|
||||
int transparent = 0;
|
||||
int numeric = 0;
|
||||
const char *user_name, *pid_file, *chroot_path, *facility = "auth";
|
||||
struct sslhcfg_item cfg;
|
||||
|
||||
struct addrinfo *addr_listen = NULL; /* what addresses do we listen to? */
|
||||
|
||||
|
||||
static int do_syslog = 1; /* Should we syslog? controled by syslog_facility = "none" */
|
||||
|
||||
#ifdef LIBWRAP
|
||||
#include <tcpd.h>
|
||||
int allow_severity =0, deny_severity = 0;
|
||||
@@ -71,7 +70,7 @@ void check_res_dump(CR_ACTION act, int res, struct addrinfo *addr, char* syscall
|
||||
}
|
||||
}
|
||||
|
||||
int get_fd_sockets(int *sockfd[])
|
||||
int get_fd_sockets(struct listen_endpoint *sockfd[])
|
||||
{
|
||||
int sd = 0;
|
||||
|
||||
@@ -86,7 +85,8 @@ int get_fd_sockets(int *sockfd[])
|
||||
*sockfd = malloc(sd * sizeof(*sockfd[0]));
|
||||
CHECK_ALLOC(*sockfd, "malloc");
|
||||
for (i = 0; i < sd; i++) {
|
||||
(*sockfd)[i] = SD_LISTEN_FDS_START + i;
|
||||
(*sockfd)[i].socketfd = SD_LISTEN_FDS_START + i;
|
||||
(*sockfd)[i].type = SOCK_STREAM;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -94,78 +94,112 @@ int get_fd_sockets(int *sockfd[])
|
||||
return sd;
|
||||
}
|
||||
|
||||
/* Starts listening sockets on specified addresses.
|
||||
* IN: addr[], num_addr
|
||||
* OUT: *sockfd[] pointer to newly-allocated array of file descriptors
|
||||
* Returns number of addresses bound
|
||||
* Bound file descriptors are returned in newly-allocated *sockfd pointer
|
||||
*/
|
||||
int start_listen_sockets(int *sockfd[], struct addrinfo *addr_list)
|
||||
/* Set TCP_FASTOPEN on listening socket if all client protocols support it */
|
||||
int make_listen_tfo(int s)
|
||||
{
|
||||
struct sockaddr_storage *saddr;
|
||||
struct addrinfo *addr;
|
||||
int i, res, one;
|
||||
int num_addr = 0;
|
||||
int sd_socks = 0;
|
||||
int i, qlen = 5;
|
||||
|
||||
sd_socks = get_fd_sockets(sockfd);
|
||||
/* Don't do it if not supported */
|
||||
if (!TCP_FASTOPEN)
|
||||
return 0;
|
||||
|
||||
if (sd_socks > 0) {
|
||||
return sd_socks;
|
||||
}
|
||||
/* Don't do it if any protocol does not specify it */
|
||||
for (i = 0; i < cfg.protocols_len; i++) {
|
||||
if (! cfg.protocols[i].tfo_ok)
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (addr = addr_list; addr; addr = addr->ai_next)
|
||||
num_addr++;
|
||||
return setsockopt(s, SOL_SOCKET, TCP_FASTOPEN, (char*)&qlen, sizeof(qlen));
|
||||
}
|
||||
|
||||
if (num_addr == 0) {
|
||||
fprintf(stderr, "FATAL: No available addresses.\n");
|
||||
exit(1);
|
||||
}
|
||||
/* Starts listening on a single address
|
||||
* Returns a socket filehandle, or dies with message in case of major error */
|
||||
int listen_single_addr(struct addrinfo* addr, int keepalive, int udp)
|
||||
{
|
||||
struct sockaddr_storage *saddr;
|
||||
int sockfd, one, res;
|
||||
|
||||
if (verbose)
|
||||
fprintf(stderr, "listening to %d addresses\n", num_addr);
|
||||
saddr = (struct sockaddr_storage*)addr->ai_addr;
|
||||
|
||||
*sockfd = malloc(num_addr * sizeof(*sockfd[0]));
|
||||
CHECK_ALLOC(*sockfd, "malloc");
|
||||
sockfd = socket(saddr->ss_family, udp ? SOCK_DGRAM : SOCK_STREAM, 0);
|
||||
check_res_dump(CR_DIE, sockfd, addr, "socket");
|
||||
|
||||
for (i = 0, addr = addr_list; i < num_addr && addr; i++, addr = addr->ai_next) {
|
||||
if (!addr) {
|
||||
fprintf(stderr, "FATAL: Inconsistent listen number. This should not happen.\n");
|
||||
exit(1);
|
||||
}
|
||||
saddr = (struct sockaddr_storage*)addr->ai_addr;
|
||||
one = 1;
|
||||
res = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one));
|
||||
check_res_dump(CR_DIE, res, addr, "setsockopt(SO_REUSEADDR)");
|
||||
|
||||
(*sockfd)[i] = socket(saddr->ss_family, SOCK_STREAM, 0);
|
||||
check_res_dump(CR_DIE, (*sockfd)[i], addr, "socket");
|
||||
res = make_listen_tfo(sockfd);
|
||||
check_res_dump(CR_WARN, res, addr, "setsockopt(TCP_FASTOPEN)");
|
||||
|
||||
one = 1;
|
||||
res = setsockopt((*sockfd)[i], SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one));
|
||||
check_res_dump(CR_DIE, res, addr, "setsockopt(SO_REUSEADDR)");
|
||||
if (keepalive) {
|
||||
res = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (char*)&one, sizeof(one));
|
||||
check_res_dump(CR_DIE, res, addr, "setsockopt(SO_KEEPALIVE)");
|
||||
}
|
||||
|
||||
if (addr->ai_flags & SO_KEEPALIVE) {
|
||||
res = setsockopt((*sockfd)[i], SOL_SOCKET, SO_KEEPALIVE, (char*)&one, sizeof(one));
|
||||
check_res_dump(CR_DIE, res, addr, "setsockopt(SO_KEEPALIVE)");
|
||||
}
|
||||
if (IP_FREEBIND) {
|
||||
res = setsockopt(sockfd, IPPROTO_IP, IP_FREEBIND, (char*)&one, sizeof(one));
|
||||
check_res_dump(CR_WARN, res, addr, "setsockopt(IP_FREEBIND)");
|
||||
}
|
||||
|
||||
if (IP_FREEBIND) {
|
||||
res = setsockopt((*sockfd)[i], IPPROTO_IP, IP_FREEBIND, (char*)&one, sizeof(one));
|
||||
check_res_dump(CR_WARN, res, addr, "setsockopt(IP_FREEBIND)");
|
||||
}
|
||||
if (addr->ai_addr->sa_family == AF_INET6) {
|
||||
res = setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&one, sizeof(one));
|
||||
check_res_dump(CR_WARN, res, addr, "setsockopt(IPV6_V6ONLY)");
|
||||
}
|
||||
|
||||
if (addr->ai_addr->sa_family == AF_INET6) {
|
||||
res = setsockopt((*sockfd)[i], IPPROTO_IPV6, IPV6_V6ONLY, (char*)&one, sizeof(one));
|
||||
check_res_dump(CR_WARN, res, addr, "setsockopt(IPV6_V6ONLY)");
|
||||
}
|
||||
res = bind(sockfd, addr->ai_addr, addr->ai_addrlen);
|
||||
check_res_dump(CR_DIE, res, addr, "bind");
|
||||
|
||||
res = bind((*sockfd)[i], addr->ai_addr, addr->ai_addrlen);
|
||||
check_res_dump(CR_DIE, res, addr, "bind");
|
||||
if (!udp) {
|
||||
res = listen (sockfd, 50);
|
||||
check_res_dump(CR_DIE, res, addr, "listen");
|
||||
}
|
||||
|
||||
res = listen ((*sockfd)[i], 50);
|
||||
check_res_dump(CR_DIE, res, addr, "listen");
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
}
|
||||
/* Starts listening sockets on specified addresses.
|
||||
* OUT: *sockfd[] pointer to newly-allocated array of listen_endpoint objects
|
||||
* Returns number of addresses bound
|
||||
*/
|
||||
int start_listen_sockets(struct listen_endpoint *sockfd[])
|
||||
{
|
||||
struct addrinfo *addr, *start_addr;
|
||||
char buf[NI_MAXHOST];
|
||||
int i, res;
|
||||
int num_addr = 0, keepalive = 0, udp = 0;
|
||||
int sd_socks = 0;
|
||||
|
||||
return num_addr;
|
||||
sd_socks = get_fd_sockets(sockfd);
|
||||
|
||||
if (sd_socks > 0) {
|
||||
return sd_socks;
|
||||
}
|
||||
|
||||
*sockfd = NULL;
|
||||
|
||||
if (cfg.verbose) fprintf(stderr, "Listening to:\n");
|
||||
|
||||
for (i = 0; i < cfg.listen_len; i++) {
|
||||
keepalive = cfg.listen[i].keepalive;
|
||||
udp = cfg.listen[i].is_udp;
|
||||
|
||||
res = resolve_split_name(&start_addr, cfg.listen[i].host, cfg.listen[i].port);
|
||||
if (res) exit(4);
|
||||
|
||||
for (addr = start_addr; addr; addr = addr->ai_next) {
|
||||
num_addr++;
|
||||
*sockfd = realloc(*sockfd, num_addr * sizeof(*sockfd));
|
||||
(*sockfd)[num_addr-1].socketfd = listen_single_addr(addr, keepalive, udp);
|
||||
(*sockfd)[num_addr-1].type = udp ? SOCK_DGRAM : SOCK_STREAM;
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "%d:\t%s\t[%s] [%s]\n", (*sockfd)[num_addr-1].socketfd, sprintaddr(buf, sizeof(buf), addr),
|
||||
cfg.listen[i].keepalive ? "keepalive" : "",
|
||||
cfg.listen[i].is_udp ? "udp" : "");
|
||||
}
|
||||
freeaddrinfo(start_addr);
|
||||
}
|
||||
|
||||
return num_addr;
|
||||
}
|
||||
|
||||
|
||||
@@ -226,7 +260,7 @@ int bind_peer(int fd, int fd_from)
|
||||
/* getpeername can fail with ENOTCONN if connection was dropped before we
|
||||
* got here */
|
||||
res = getpeername(fd_from, from.ai_addr, &from.ai_addrlen);
|
||||
CHECK_RES_RETURN(res, "getpeername");
|
||||
CHECK_RES_RETURN(res, "getpeername", res);
|
||||
|
||||
/* if the destination is the same machine, there's no need to do bind */
|
||||
if (is_same_machine(&from))
|
||||
@@ -234,47 +268,65 @@ int bind_peer(int fd, int fd_from)
|
||||
|
||||
#ifndef IP_BINDANY /* use IP_TRANSPARENT */
|
||||
res = setsockopt(fd, IPPROTO_IP, IP_TRANSPARENT, &trans, sizeof(trans));
|
||||
CHECK_RES_DIE(res, "setsockopt");
|
||||
CHECK_RES_DIE(res, "setsockopt IP_TRANSPARENT");
|
||||
#else
|
||||
if (from.ai_addr->sa_family==AF_INET) { /* IPv4 */
|
||||
res = setsockopt(fd, IPPROTO_IP, IP_BINDANY, &trans, sizeof(trans));
|
||||
CHECK_RES_RETURN(res, "setsockopt IP_BINDANY");
|
||||
CHECK_RES_RETURN(res, "setsockopt IP_BINDANY", res);
|
||||
#ifdef IPV6_BINDANY
|
||||
} else { /* IPv6 */
|
||||
res = setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &trans, sizeof(trans));
|
||||
CHECK_RES_RETURN(res, "setsockopt IPV6_BINDANY");
|
||||
CHECK_RES_RETURN(res, "setsockopt IPV6_BINDANY", res);
|
||||
#endif /* IPV6_BINDANY */
|
||||
}
|
||||
#endif /* IP_TRANSPARENT / IP_BINDANY */
|
||||
res = bind(fd, from.ai_addr, from.ai_addrlen);
|
||||
CHECK_RES_RETURN(res, "bind");
|
||||
CHECK_RES_RETURN(res, "bind", res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Make the file descriptor non-block */
|
||||
int set_nonblock(int fd)
|
||||
{
|
||||
int flags;
|
||||
|
||||
flags = fcntl(fd, F_GETFL);
|
||||
CHECK_RES_RETURN(flags, "fcntl", -1);
|
||||
|
||||
flags |= O_NONBLOCK;
|
||||
|
||||
flags = fcntl(fd, F_SETFL, flags);
|
||||
CHECK_RES_RETURN(flags, "fcntl", -1);
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
|
||||
/* Connect to first address that works and returns a file descriptor, or -1 if
|
||||
* none work.
|
||||
* If transparent proxying is on, use fd_from peer address on external address
|
||||
* of new file descriptor. */
|
||||
int connect_addr(struct connection *cnx, int fd_from)
|
||||
int connect_addr(struct connection *cnx, int fd_from, connect_blocking blocking)
|
||||
{
|
||||
struct addrinfo *a, from;
|
||||
struct sockaddr_storage ss;
|
||||
char buf[NI_MAXHOST];
|
||||
int fd, res, one;
|
||||
int transparent = cnx->proto->transparent || cfg.transparent;
|
||||
|
||||
memset(&from, 0, sizeof(from));
|
||||
from.ai_addr = (struct sockaddr*)&ss;
|
||||
from.ai_addrlen = sizeof(ss);
|
||||
|
||||
res = getpeername(fd_from, from.ai_addr, &from.ai_addrlen);
|
||||
CHECK_RES_RETURN(res, "getpeername");
|
||||
CHECK_RES_RETURN(res, "getpeername", res);
|
||||
|
||||
for (a = cnx->proto->saddr; a; a = a->ai_next) {
|
||||
/* When transparent, make sure both connections use the same address family */
|
||||
if (transparent && a->ai_family != from.ai_addr->sa_family)
|
||||
continue;
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "connecting to %s family %d len %d\n",
|
||||
sprintaddr(buf, sizeof(buf), a),
|
||||
a->ai_addr->sa_family, a->ai_addrlen);
|
||||
@@ -283,25 +335,35 @@ int connect_addr(struct connection *cnx, int fd_from)
|
||||
fd = socket(a->ai_family, SOCK_STREAM, 0);
|
||||
if (fd == -1) {
|
||||
log_message(LOG_ERR, "forward to %s failed:socket: %s\n",
|
||||
cnx->proto->description, strerror(errno));
|
||||
cnx->proto->name, strerror(errno));
|
||||
} else {
|
||||
one = 1;
|
||||
setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof(one));
|
||||
/* no need to check return value; if it's not supported, that's okay */
|
||||
|
||||
if (blocking == NON_BLOCKING) {
|
||||
set_nonblock(fd);
|
||||
}
|
||||
|
||||
if (transparent) {
|
||||
res = bind_peer(fd, fd_from);
|
||||
CHECK_RES_RETURN(res, "bind_peer");
|
||||
CHECK_RES_RETURN(res, "bind_peer", res);
|
||||
}
|
||||
res = connect(fd, a->ai_addr, a->ai_addrlen);
|
||||
if (res == -1) {
|
||||
|
||||
/* EINPROGRESS indicates it might take time. If it eventually
|
||||
* fails, it'll be caught as a failed read */
|
||||
if ((res == -1) && (errno != EINPROGRESS)) {
|
||||
log_message(LOG_ERR, "forward to %s failed:connect: %s\n",
|
||||
cnx->proto->description, strerror(errno));
|
||||
cnx->proto->name, strerror(errno));
|
||||
close(fd);
|
||||
} else {
|
||||
if (cnx->proto->keepalive) {
|
||||
one = 1;
|
||||
res = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&one, sizeof(one));
|
||||
CHECK_RES_RETURN(res, "setsockopt(SO_KEEPALIVE)");
|
||||
}
|
||||
return fd;
|
||||
continue; /* Try the next address */
|
||||
}
|
||||
if (cnx->proto->keepalive) {
|
||||
res = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&one, sizeof(one));
|
||||
CHECK_RES_RETURN(res, "setsockopt(SO_KEEPALIVE)", res);
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
@@ -312,7 +374,7 @@ int defer_write(struct queue *q, void* data, int data_size)
|
||||
{
|
||||
char *p;
|
||||
ptrdiff_t data_offset = q->deferred_data - q->begin_deferred_data;
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "**** writing deferred on fd %d\n", q->fd);
|
||||
|
||||
p = realloc(q->begin_deferred_data, data_offset + q->deferred_data_size + data_size);
|
||||
@@ -335,7 +397,7 @@ int flush_deferred(struct queue *q)
|
||||
{
|
||||
int n;
|
||||
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "flushing deferred data to fd %d\n", q->fd);
|
||||
|
||||
n = write(q->fd, q->deferred_data, q->deferred_data_size);
|
||||
@@ -363,14 +425,16 @@ void init_cnx(struct connection *cnx)
|
||||
memset(cnx, 0, sizeof(*cnx));
|
||||
cnx->q[0].fd = -1;
|
||||
cnx->q[1].fd = -1;
|
||||
cnx->proto = get_first_protocol();
|
||||
cnx->proto = NULL;
|
||||
}
|
||||
|
||||
void dump_connection(struct connection *cnx)
|
||||
{
|
||||
printf("state: %d\n", cnx->state);
|
||||
printf("fd %d, %d deferred\n", cnx->q[0].fd, cnx->q[0].deferred_data_size);
|
||||
printf("fd %d, %d deferred\n", cnx->q[1].fd, cnx->q[1].deferred_data_size);
|
||||
printf("0: fd %d, %d deferred\n", cnx->q[0].fd, cnx->q[0].deferred_data_size);
|
||||
hexdump(cnx->q[0].deferred_data, cnx->q[0].deferred_data_size);
|
||||
printf("1: fd %d, %d deferred\n", cnx->q[1].fd, cnx->q[1].deferred_data_size);
|
||||
hexdump(cnx->q[1].deferred_data, cnx->q[1].deferred_data_size);
|
||||
}
|
||||
|
||||
|
||||
@@ -395,7 +459,7 @@ int fd2fd(struct queue *target_q, struct queue *from_q)
|
||||
if (size_r == -1) {
|
||||
switch (errno) {
|
||||
case EAGAIN:
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "reading 0 from %d\n", from);
|
||||
return FD_NODATA;
|
||||
|
||||
@@ -405,7 +469,7 @@ int fd2fd(struct queue *target_q, struct queue *from_q)
|
||||
}
|
||||
}
|
||||
|
||||
CHECK_RES_RETURN(size_r, "read");
|
||||
CHECK_RES_RETURN(size_r, "read",FD_CNXCLOSED);
|
||||
|
||||
if (size_r == 0)
|
||||
return FD_CNXCLOSED;
|
||||
@@ -421,7 +485,7 @@ int fd2fd(struct queue *target_q, struct queue *from_q)
|
||||
|
||||
case ECONNRESET:
|
||||
case EPIPE:
|
||||
/* remove end closed -- drop the connection */
|
||||
/* remote end closed -- drop the connection */
|
||||
return FD_CNXCLOSED;
|
||||
}
|
||||
} else if (size_w < size_r) {
|
||||
@@ -430,7 +494,7 @@ int fd2fd(struct queue *target_q, struct queue *from_q)
|
||||
return FD_STALLED;
|
||||
}
|
||||
|
||||
CHECK_RES_RETURN(size_w, "write");
|
||||
CHECK_RES_RETURN(size_w, "write", FD_CNXCLOSED);
|
||||
|
||||
return size_w;
|
||||
}
|
||||
@@ -444,7 +508,7 @@ char* sprintaddr(char* buf, size_t size, struct addrinfo *a)
|
||||
res = getnameinfo(a->ai_addr, a->ai_addrlen,
|
||||
host, sizeof(host),
|
||||
serv, sizeof(serv),
|
||||
numeric ? NI_NUMERICHOST | NI_NUMERICSERV : 0 );
|
||||
cfg.numeric ? NI_NUMERICHOST | NI_NUMERICSERV : 0 );
|
||||
|
||||
if (res) {
|
||||
log_message(LOG_ERR, "sprintaddr:getnameinfo: %s\n", gai_strerror(res));
|
||||
@@ -469,26 +533,16 @@ char* sprintaddr(char* buf, size_t size, struct addrinfo *a)
|
||||
/* Turns a hostname and port (or service) into a list of struct addrinfo
|
||||
* returns 0 on success, -1 otherwise and logs error
|
||||
*/
|
||||
int resolve_split_name(struct addrinfo **out, const char* ct_host, const char* serv)
|
||||
int resolve_split_name(struct addrinfo **out, char* host, char* serv)
|
||||
{
|
||||
struct addrinfo hint;
|
||||
char *end;
|
||||
int res;
|
||||
char* host, *host_base;
|
||||
|
||||
memset(&hint, 0, sizeof(hint));
|
||||
hint.ai_family = PF_UNSPEC;
|
||||
hint.ai_socktype = SOCK_STREAM;
|
||||
|
||||
/* Copy parameter so not to clobber data in libconfig */
|
||||
res = asprintf(&host_base, "%s", ct_host);
|
||||
if (res == -1) {
|
||||
log_message(LOG_ERR, "asprintf: cannot allocate memory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
host = host_base;
|
||||
|
||||
/* If it is a RFC-Compliant IPv6 address ("[1234::12]:443"), remove brackets
|
||||
* around IP address */
|
||||
if (host[0] == '[') {
|
||||
@@ -504,7 +558,6 @@ int resolve_split_name(struct addrinfo **out, const char* ct_host, const char* s
|
||||
res = getaddrinfo(host, serv, &hint, out);
|
||||
if (res)
|
||||
log_message(LOG_ERR, "%s `%s:%s'\n", gai_strerror(res), host, serv);
|
||||
free(host_base);
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -538,60 +591,97 @@ void resolve_name(struct addrinfo **out, char* fullname)
|
||||
}
|
||||
|
||||
/* Log to syslog or stderr if foreground */
|
||||
void log_message(int type, char* msg, ...)
|
||||
void log_message(int type, const char* msg, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, msg);
|
||||
if (foreground)
|
||||
if (cfg.foreground)
|
||||
vfprintf(stderr, msg, ap);
|
||||
else
|
||||
vsyslog(type, msg, ap);
|
||||
va_end(ap);
|
||||
|
||||
if (do_syslog) {
|
||||
va_start(ap, msg);
|
||||
vsyslog(type, msg, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
}
|
||||
|
||||
/* syslogs who connected to where */
|
||||
void log_connection(struct connection *cnx)
|
||||
|
||||
/* Fills a connection description; returns 0 on failure */
|
||||
int get_connection_desc(struct connection_desc* desc, const struct connection *cnx)
|
||||
{
|
||||
int res;
|
||||
struct addrinfo addr;
|
||||
struct sockaddr_storage ss;
|
||||
#define MAX_NAMELENGTH (NI_MAXHOST + NI_MAXSERV + 1)
|
||||
char peer[MAX_NAMELENGTH], service[MAX_NAMELENGTH],
|
||||
local[MAX_NAMELENGTH], target[MAX_NAMELENGTH];
|
||||
int res;
|
||||
|
||||
if (cnx->proto->log_level < 1)
|
||||
return;
|
||||
|
||||
addr.ai_addr = (struct sockaddr*)&ss;
|
||||
addr.ai_addrlen = sizeof(ss);
|
||||
|
||||
res = getpeername(cnx->q[0].fd, addr.ai_addr, &addr.ai_addrlen);
|
||||
if (res == -1) return; /* Can happen if connection drops before we get here.
|
||||
if (res == -1) return 0; /* Can happen if connection drops before we get here.
|
||||
In that case, don't log anything (there is no connection) */
|
||||
sprintaddr(peer, sizeof(peer), &addr);
|
||||
sprintaddr(desc->peer, sizeof(desc->peer), &addr);
|
||||
|
||||
addr.ai_addrlen = sizeof(ss);
|
||||
res = getsockname(cnx->q[0].fd, addr.ai_addr, &addr.ai_addrlen);
|
||||
if (res == -1) return;
|
||||
sprintaddr(service, sizeof(service), &addr);
|
||||
if (res == -1) return 0;
|
||||
sprintaddr(desc->service, sizeof(desc->service), &addr);
|
||||
|
||||
addr.ai_addrlen = sizeof(ss);
|
||||
res = getpeername(cnx->q[1].fd, addr.ai_addr, &addr.ai_addrlen);
|
||||
if (res == -1) return;
|
||||
sprintaddr(target, sizeof(target), &addr);
|
||||
if (res == -1) return 0;
|
||||
sprintaddr(desc->target, sizeof(desc->target), &addr);
|
||||
|
||||
addr.ai_addrlen = sizeof(ss);
|
||||
res = getsockname(cnx->q[1].fd, addr.ai_addr, &addr.ai_addrlen);
|
||||
if (res == -1) return;
|
||||
sprintaddr(local, sizeof(local), &addr);
|
||||
if (res == -1) return 0;
|
||||
sprintaddr(desc->local, sizeof(desc->local), &addr);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* syslogs who connected to where
|
||||
* desc: string description of the connection. if NULL, log_connection will
|
||||
* manage on its own
|
||||
* cnx: connection descriptor
|
||||
* */
|
||||
void log_connection(struct connection_desc* desc, const struct connection *cnx)
|
||||
{
|
||||
struct connection_desc d;
|
||||
|
||||
if (cnx->proto->log_level < 1)
|
||||
return;
|
||||
|
||||
if (!desc) {
|
||||
desc = &d;
|
||||
get_connection_desc(desc, cnx);
|
||||
}
|
||||
|
||||
log_message(LOG_INFO, "%s:connection from %s to %s forwarded from %s to %s\n",
|
||||
cnx->proto->description,
|
||||
peer,
|
||||
service,
|
||||
local,
|
||||
target);
|
||||
cnx->proto->name,
|
||||
desc->peer,
|
||||
desc->service,
|
||||
desc->local,
|
||||
desc->target);
|
||||
}
|
||||
|
||||
void set_proctitle_shovel(struct connection_desc* desc, const struct connection *cnx)
|
||||
{
|
||||
#ifdef LIBBSD
|
||||
struct connection_desc d;
|
||||
|
||||
if (!desc) {
|
||||
desc = &d;
|
||||
get_connection_desc(desc, cnx);
|
||||
}
|
||||
setproctitle("shovel %s %s->%s => %s->%s",
|
||||
cnx->proto->name,
|
||||
desc->peer,
|
||||
desc->service,
|
||||
desc->local,
|
||||
desc->target);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -613,27 +703,27 @@ int check_access_rights(int in_socket, const char* service)
|
||||
int res;
|
||||
|
||||
res = getpeername(in_socket, &peer.saddr, &size);
|
||||
CHECK_RES_RETURN(res, "getpeername");
|
||||
CHECK_RES_RETURN(res, "getpeername", res);
|
||||
|
||||
/* extract peer address */
|
||||
res = getnameinfo(&peer.saddr, size, addr_str, sizeof(addr_str), NULL, 0, NI_NUMERICHOST);
|
||||
if (res) {
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "getnameinfo(NI_NUMERICHOST):%s\n", gai_strerror(res));
|
||||
strcpy(addr_str, STRING_UNKNOWN);
|
||||
}
|
||||
/* extract peer name */
|
||||
strcpy(host, STRING_UNKNOWN);
|
||||
if (!numeric) {
|
||||
if (!cfg.numeric) {
|
||||
res = getnameinfo(&peer.saddr, size, host, sizeof(host), NULL, 0, NI_NAMEREQD);
|
||||
if (res) {
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "getnameinfo(NI_NAMEREQD):%s\n", gai_strerror(res));
|
||||
}
|
||||
}
|
||||
|
||||
if (!hosts_ctl(service, host, addr_str, STRING_UNKNOWN)) {
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "access denied\n");
|
||||
log_message(LOG_INFO, "connection from %s(%s): access denied", host, addr_str);
|
||||
close(in_socket);
|
||||
@@ -676,15 +766,20 @@ void setup_syslog(const char* bin_name) {
|
||||
char *name1, *name2;
|
||||
int res, fn;
|
||||
|
||||
if (!strcmp(cfg.syslog_facility, "none")) {
|
||||
do_syslog = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
name1 = strdup(bin_name);
|
||||
res = asprintf(&name2, "%s[%d]", basename(name1), getpid());
|
||||
CHECK_RES_DIE(res, "asprintf");
|
||||
|
||||
for (fn = 0; facilitynames[fn].c_val != -1; fn++)
|
||||
if (strcmp(facilitynames[fn].c_name, facility) == 0)
|
||||
if (strcmp(facilitynames[fn].c_name, cfg.syslog_facility) == 0)
|
||||
break;
|
||||
if (facilitynames[fn].c_val == -1) {
|
||||
fprintf(stderr, "Unknown facility %s\n", facility);
|
||||
fprintf(stderr, "Unknown facility %s\n", cfg.syslog_facility);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -707,16 +802,33 @@ void set_keepcaps(int val) {
|
||||
#endif
|
||||
}
|
||||
|
||||
/* set needed capabilities for effective and permitted, clear rest */
|
||||
void set_capabilities(void) {
|
||||
/* Returns true if anything requires transparent proxying. */
|
||||
static int use_transparent(void)
|
||||
{
|
||||
#ifdef LIBCAP
|
||||
if (cfg.transparent)
|
||||
return 1;
|
||||
|
||||
for (int i = 0; i < cfg.protocols_len; i++)
|
||||
if (cfg.protocols[i].transparent)
|
||||
return 1;
|
||||
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* set needed capabilities for effective and permitted, clear rest
|
||||
* IN: cap_net_admin: set to 1 to set CAP_NET_RAW
|
||||
* */
|
||||
void set_capabilities(int cap_net_admin) {
|
||||
#ifdef LIBCAP
|
||||
int res;
|
||||
cap_t caps;
|
||||
cap_value_t cap_list[10];
|
||||
int ncap = 0;
|
||||
|
||||
if (transparent)
|
||||
cap_list[ncap++] = CAP_NET_ADMIN;
|
||||
if (cap_net_admin)
|
||||
cap_list[ncap++] = CAP_NET_RAW;
|
||||
|
||||
caps = cap_init();
|
||||
|
||||
@@ -757,12 +869,12 @@ void drop_privileges(const char* user_name, const char* chroot_path)
|
||||
fprintf(stderr, "%s: not found\n", user_name);
|
||||
exit(2);
|
||||
}
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "turning into %s\n", user_name);
|
||||
}
|
||||
|
||||
if (chroot_path) {
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "chrooting into %s\n", chroot_path);
|
||||
|
||||
res = chroot(chroot_path);
|
||||
@@ -784,7 +896,7 @@ void drop_privileges(const char* user_name, const char* chroot_path)
|
||||
res = setuid(pw->pw_uid);
|
||||
CHECK_RES_DIE(res, "setuid");
|
||||
|
||||
set_capabilities();
|
||||
set_capabilities(use_transparent());
|
||||
set_keepcaps(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,9 @@
|
||||
|
||||
#include "version.h"
|
||||
|
||||
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
||||
|
||||
|
||||
#define CHECK_RES_DIE(res, str) \
|
||||
if (res == -1) { \
|
||||
fprintf(stderr, "%s:%d:", __FILE__, __LINE__); \
|
||||
@@ -42,10 +45,10 @@
|
||||
exit(1); \
|
||||
}
|
||||
|
||||
#define CHECK_RES_RETURN(res, str) \
|
||||
#define CHECK_RES_RETURN(res, str, ret) \
|
||||
if (res == -1) { \
|
||||
log_message(LOG_CRIT, "%s:%d:%s:%d:%s\n", __FILE__, __LINE__, str, errno, strerror(errno)); \
|
||||
return res; \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
#define CHECK_ALLOC(a, str) \
|
||||
@@ -67,6 +70,14 @@
|
||||
#define IP_FREEBIND 0
|
||||
#endif
|
||||
|
||||
#ifndef TCP_FASTOPEN
|
||||
#define TCP_FASTOPEN 0
|
||||
#endif
|
||||
|
||||
#ifndef TCP_FASTOPEN_CONNECT
|
||||
#define TCP_FASTOPEN_CONNECT 30 /* Attempt FastOpen with connect. */
|
||||
#endif
|
||||
|
||||
enum connection_state {
|
||||
ST_PROBING=1, /* Waiting for timeout to find where to forward */
|
||||
ST_SHOVELING /* Connexion is established */
|
||||
@@ -85,53 +96,86 @@ struct queue {
|
||||
};
|
||||
|
||||
struct connection {
|
||||
int type; /* SOCK_DGRAM | SOCK_STREAM */
|
||||
struct sslhcfg_protocols_item* proto; /* Where to connect to */
|
||||
|
||||
/* SOCK_STREAM */
|
||||
enum connection_state state;
|
||||
time_t probe_timeout;
|
||||
struct proto *proto;
|
||||
|
||||
/* q[0]: queue for external connection (client);
|
||||
* q[1]: queue for internal connection (httpd or sshd);
|
||||
* */
|
||||
struct queue q[2];
|
||||
|
||||
/* SOCK_DGRAM */
|
||||
struct sockaddr client_addr; /* Contains the remote client address */
|
||||
socklen_t addrlen;
|
||||
|
||||
int local_endpoint; /* Contains the local address */
|
||||
|
||||
time_t last_active;
|
||||
|
||||
/* We need one local socket for each target server, so we know where to
|
||||
* forward server responses */
|
||||
int target_sock;
|
||||
};
|
||||
|
||||
|
||||
struct listen_endpoint {
|
||||
int socketfd; /* file descriptor of listening socket */
|
||||
int type; /* SOCK_DGRAM | SOCK_STREAM */
|
||||
};
|
||||
|
||||
#define FD_CNXCLOSED 0
|
||||
#define FD_NODATA -1
|
||||
#define FD_STALLED -2
|
||||
|
||||
/* String description of a connection */
|
||||
#define MAX_NAMELENGTH (NI_MAXHOST + NI_MAXSERV + 1)
|
||||
struct connection_desc {
|
||||
char peer[MAX_NAMELENGTH], service[MAX_NAMELENGTH],
|
||||
local[MAX_NAMELENGTH], target[MAX_NAMELENGTH];
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
NON_BLOCKING = 0,
|
||||
BLOCKING = 1
|
||||
} connect_blocking;
|
||||
|
||||
|
||||
/* common.c */
|
||||
void init_cnx(struct connection *cnx);
|
||||
int connect_addr(struct connection *cnx, int fd_from);
|
||||
int set_nonblock(int fd);
|
||||
int connect_addr(struct connection *cnx, int fd_from, connect_blocking blocking);
|
||||
int fd2fd(struct queue *target, struct queue *from);
|
||||
char* sprintaddr(char* buf, size_t size, struct addrinfo *a);
|
||||
void resolve_name(struct addrinfo **out, char* fullname);
|
||||
void log_connection(struct connection *cnx);
|
||||
int get_connection_desc(struct connection_desc* desc, const struct connection *cnx);
|
||||
void log_connection(struct connection_desc* desc, const struct connection *cnx);
|
||||
void set_proctitle_shovel(struct connection_desc* desc, const struct connection *cnx);
|
||||
int check_access_rights(int in_socket, const char* service);
|
||||
void setup_signals(void);
|
||||
void setup_syslog(const char* bin_name);
|
||||
void drop_privileges(const char* user_name, const char* chroot_path);
|
||||
void set_capabilities(int cap_net_admin);
|
||||
void write_pid_file(const char* pidfile);
|
||||
void log_message(int type, char* msg, ...);
|
||||
void log_message(int type, const char* msg, ...);
|
||||
void dump_connection(struct connection *cnx);
|
||||
int resolve_split_name(struct addrinfo **out, const char* hostname, const char* port);
|
||||
int resolve_split_name(struct addrinfo **out, char* hostname, char* port);
|
||||
|
||||
int start_listen_sockets(int *sockfd[], struct addrinfo *addr_list);
|
||||
int start_listen_sockets(struct listen_endpoint *sockfd[]);
|
||||
|
||||
int defer_write(struct queue *q, void* data, int data_size);
|
||||
int flush_deferred(struct queue *q);
|
||||
|
||||
extern int probing_timeout, verbose, inetd, foreground,
|
||||
background, transparent, numeric;
|
||||
extern struct sockaddr_storage addr_ssl, addr_ssh, addr_openvpn;
|
||||
extern struct sslhcfg_item cfg;
|
||||
extern struct addrinfo *addr_listen;
|
||||
extern const char* USAGE_STRING;
|
||||
extern const char* user_name, *pid_file, *chroot_path, *facility;
|
||||
extern const char* server_type;
|
||||
|
||||
/* sslh-fork.c */
|
||||
void start_shoveler(int);
|
||||
|
||||
void main_loop(int *listen_sockets, int num_addr_listen);
|
||||
void main_loop(struct listen_endpoint *listen_sockets, int num_addr_listen);
|
||||
|
||||
#endif
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
Frequently Asked Questions
|
||||
==========================
|
||||
|
||||
When something doesn't work, look up here... and if it still
|
||||
doesn't work, report how what was suggested here went.
|
||||
|
||||
It's also worth reading [how to ask
|
||||
questions](http://www.catb.org/~esr/faqs/smart-questions.html)
|
||||
before posting on the mailing list or opening an issue in
|
||||
Github.
|
||||
|
||||
Getting more info
|
||||
=================
|
||||
|
||||
In general, if something doesn't work, you'll want to run
|
||||
`sslh` with lots of logging, and the logging directly in
|
||||
the terminal (Otherwise, logs are sent to `syslog`, and
|
||||
usually end up in `/var/log/auth.log`). You will achieve
|
||||
this by running `sslh` in foreground with verbose:
|
||||
|
||||
```
|
||||
sslh -v 1 -f -F myconfig.cfg
|
||||
```
|
||||
|
||||
Higher values of `verbose` produce more information. 1 is
|
||||
usually sufficient. 2 will also print incoming packets used
|
||||
for probing.
|
||||
|
||||
forward to [PROBE] failed:connect: Connection refused
|
||||
=====================================================
|
||||
|
||||
Usually this means `sslh` is configured to forward a
|
||||
protocol somewhere, but no service is listening on the
|
||||
target address. Check your `sslh` configuration, check the
|
||||
corresponding server really is listening and running.
|
||||
Finally, check the server is listening where you expect it
|
||||
to:
|
||||
|
||||
```
|
||||
netstat -lpt
|
||||
```
|
||||
|
||||
I get a segmentation fault!
|
||||
===========================
|
||||
|
||||
Well, it's not yours (fault): a segfault is always a bug in
|
||||
the programme. Usually standard use cases are well tested,
|
||||
so it may be related to something unusual in your
|
||||
configuration, or even something wrong, but it should still
|
||||
never result in a segfault.
|
||||
|
||||
Thankfully, when they are deterministic, segfaults are
|
||||
usually fairly easy to fix if you're willing to run a few
|
||||
diagnostics to help the developer.
|
||||
|
||||
First, make sure you have debug symbols:
|
||||
```
|
||||
$ file sslh-select
|
||||
sslh-select: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=a758ac75ff11f1ace577705b4d6627e301940b59, with debug_info, not stripped
|
||||
```
|
||||
|
||||
Note `with debug_info, not stripped` at the end. If you
|
||||
don't have that, your distribution stripped the binary: you
|
||||
will need to get the source code and compile it yourself
|
||||
(that way, you will also get the latest version).
|
||||
|
||||
Install `valgrind` and run `sslh` under it:
|
||||
|
||||
```
|
||||
valgrind --leak-check=full ./sslh-fork -v 2 -f -F yourconfig.cfg
|
||||
```
|
||||
|
||||
Report the full output to the mailing list or github.
|
||||
Valgrind is very powerful and gives precise hints of what is
|
||||
wrong and why. For example on `sslh` issue
|
||||
(#273)[https://github.com/yrutschle/sslh/issues/273]:
|
||||
|
||||
```
|
||||
sudo valgrind --leak-check=full ./sslh-fork -v 2 -f -F /etc/sslh.cfg
|
||||
==20037== Memcheck, a memory error detector
|
||||
==20037== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
|
||||
==20037== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
|
||||
==20037== Command: ./sslh-fork -v 2 -f -F /etc/sslh.cfg
|
||||
==20037==
|
||||
sslh-fork v1.21b-1-g2c93a01-dirty started
|
||||
--20037-- WARNING: unhandled arm-linux syscall: 403
|
||||
--20037-- You may be able to write your own handler.
|
||||
--20037-- Read the file README_MISSING_SYSCALL_OR_IOCTL.
|
||||
--20037-- Nevertheless we consider this a bug. Please report
|
||||
--20037-- it at http://valgrind.org/support/bug_reports.html.
|
||||
==20040== Conditional jump or move depends on uninitialised value(s)
|
||||
==20040== at 0x112A3C: parse_tls_header (tls.c:162)
|
||||
==20040== by 0x111CEF: is_tls_protocol (probe.c:214)
|
||||
==20040== by 0x11239F: probe_client_protocol (probe.c:366)
|
||||
==20040== by 0x10A8F7: start_shoveler (sslh-fork.c:98)
|
||||
==20040== by 0x10AE9B: main_loop (sslh-fork.c:200)
|
||||
==20040== by 0x1114FB: main (sslh-main.c:322)
|
||||
==20040==
|
||||
```
|
||||
|
||||
Here we see that something wrong is happening at `tls.c`
|
||||
line 162, and it's linked to an uninitialised value.
|
||||
|
||||
Using sslh for virtual hosting
|
||||
==============================
|
||||
|
||||
Virtual hosting refers to having several domain names behind
|
||||
a single IP address. All Web servers handle this, but
|
||||
sometimes it can be useful to do it with `sslh`.
|
||||
|
||||
TLS virtual hosting with SNI
|
||||
----------------------------
|
||||
|
||||
For TLS, this is done very simply using Server Name
|
||||
Indication, SNI for short, which is a TLS extension whereby
|
||||
the client indicates the name of the server it wishes to
|
||||
connect to. This can be a very powerful way to separate
|
||||
several TLS-based services hosted behind the same port:
|
||||
simply name each service with its own hostname. For example,
|
||||
we could define `mail.rutschle.net`, `im.rutschle.net`,
|
||||
`www.rutschle.net`, all of which point to the same IP
|
||||
address. `sslh` uses the `sni_hostnames` setting of the
|
||||
TLS probe to do this, e.g.:
|
||||
|
||||
```
|
||||
protocols: (
|
||||
{ name: "tls";
|
||||
host: "localhost";
|
||||
port: "993";
|
||||
sni_hostnames: [ "mail.rutschle.net" ];
|
||||
},
|
||||
{ name: "tls";
|
||||
host: "localhost";
|
||||
port: "xmpp-client";
|
||||
sni_hostnames: [ "im.rutschle.net" ];
|
||||
},
|
||||
{ name: "tls";
|
||||
host: "localhost";
|
||||
port: "4443";
|
||||
sni_hostnames: [ "www.rutschle.net" ];
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
HTTP virtual hosting with regex
|
||||
-------------------------------
|
||||
|
||||
If you wish to serve several Web domains over HTTP through
|
||||
`sslh`, you can do this simply by using regular expressions
|
||||
on the Host specification part of the HTTP query.
|
||||
|
||||
The following example forwards connections to `host_A.acme`
|
||||
to 192.168.0.2, and connections to `host_B.acme` to
|
||||
192.168.0.3.
|
||||
|
||||
```
|
||||
protocols: (
|
||||
{ name: "regex";
|
||||
host: "192.168.0.2";
|
||||
port: "80";
|
||||
regex_patterns:
|
||||
["^(GET|POST|PUT|OPTIONS|DELETE|HEADER) [^ ]* HTTP/[0-9.]*[\r\n]*Host: host_A.acme"] },
|
||||
{ name: "regex";
|
||||
host: "192.168.0.3";
|
||||
port: "80";
|
||||
regex_patterns:
|
||||
["^(GET|POST|PUT|OPTIONS|DELETE|HEADER) [^ ]* HTTP/[0-9.]*[\r\n]*Host: host_B.acme"] }
|
||||
);
|
||||
```
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
Compile and install
|
||||
===================
|
||||
|
||||
Dependencies
|
||||
------------
|
||||
|
||||
`sslh` uses:
|
||||
|
||||
* [libconfig](http://www.hyperrealm.com/libconfig/). For
|
||||
Debian this is contained in package `libconfig8-dev`. You
|
||||
can compile with or without it using USELIBCONFIG in the
|
||||
Makefile.
|
||||
|
||||
* [libwrap](http://packages.debian.org/source/unstable/tcp-wrappers).
|
||||
For Debian, this is contained in packages
|
||||
`libwrap0-dev`. You
|
||||
can compile with or without it using USELIBWRAP in the
|
||||
Makefile.
|
||||
|
||||
* [libsystemd](http://packages.debian.org/source/unstable/libsystemd-dev), in package `libsystemd-dev`. You
|
||||
can compile with or without it using USESYSTEMD in the
|
||||
Makefile.
|
||||
|
||||
* [libcap](http://packages.debian.org/source/unstable/libcap-dev), in package `libcap-dev`. You can compile with or without it using USELIBCAP in the Makefile
|
||||
|
||||
* libbsd, to enable to change the process name (as shown in
|
||||
`ps`, so each forked process shows what protocol and what
|
||||
connection it is serving),
|
||||
which requires `libbsd` at runtime, and `libbsd-dev` at
|
||||
compile-time.
|
||||
|
||||
|
||||
For OpenSUSE, these are contained in packages libconfig9 and
|
||||
libconfig-dev in repository
|
||||
<http://download.opensuse.org/repositories/multimedia:/libs/openSUSE_12.1/>
|
||||
|
||||
For Fedora, you'll need packages `libconfig` and
|
||||
`libconfig-devel`:
|
||||
|
||||
yum install libconfig libconfig-devel
|
||||
|
||||
If you want to rebuild `sslh-conf.c` (after a `make
|
||||
distclean` for example), you will also need to add
|
||||
[conf2struct](https://www.rutschle.net/tech/conf2struct/README.html)
|
||||
(v1.5) to your path.
|
||||
|
||||
Compilation
|
||||
-----------
|
||||
|
||||
After this, the Makefile should work:
|
||||
|
||||
make install
|
||||
|
||||
There are a couple of configuration options at the beginning
|
||||
of the Makefile:
|
||||
|
||||
* `USELIBWRAP` compiles support for host access control (see
|
||||
`hosts_access(3)`), you will need `libwrap` headers and
|
||||
library to compile (`libwrap0-dev` in Debian).
|
||||
|
||||
* `USELIBCONFIG` compiles support for the configuration
|
||||
file. You will need `libconfig` headers to compile
|
||||
(`libconfig8-dev` in Debian).
|
||||
|
||||
* `USESYSTEMD` compiles support for using systemd socket activation.
|
||||
You will need `systemd` headers to compile (`systemd-devel` in Fedora).
|
||||
|
||||
* `USELIBBSD` compiles support for updating the process name (as shown
|
||||
by `ps`).
|
||||
|
||||
Binaries
|
||||
--------
|
||||
|
||||
The Makefile produces two different executables: `sslh-fork`
|
||||
and `sslh-select`:
|
||||
|
||||
* `sslh-fork` forks a new process for each incoming connection.
|
||||
It is well-tested and very reliable, but incurs the overhead
|
||||
of many processes.
|
||||
If you are going to use `sslh` for a "small" setup (less than
|
||||
a dozen ssh connections and a low-traffic https server) then
|
||||
`sslh-fork` is probably more suited for you.
|
||||
|
||||
* `sslh-select` uses only one thread, which monitors all connections
|
||||
at once. It is more recent and less tested, but only incurs a 16
|
||||
byte overhead per connection. Also, if it stops, you'll lose all
|
||||
connections, which means you can't upgrade it remotely.
|
||||
If you are going to use `sslh` on a "medium" setup (a few thousand ssh
|
||||
connections, and another few thousand ssl connections),
|
||||
`sslh-select` will be better.
|
||||
|
||||
If you have a very large site (tens of thousands of connections),
|
||||
you'll need a vapourware version that would use libevent or
|
||||
something like that.
|
||||
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
* In general:
|
||||
|
||||
make
|
||||
cp sslh-fork /usr/local/sbin/sslh
|
||||
cp basic.cfg /etc/sslh.cfg
|
||||
vi /etc/sslh.cfg
|
||||
|
||||
* For Debian:
|
||||
|
||||
cp scripts/etc.init.d.sslh /etc/init.d/sslh
|
||||
|
||||
* For CentOS:
|
||||
|
||||
cp scripts/etc.rc.d.init.d.sslh.centos /etc/rc.d/init.d/sslh
|
||||
|
||||
|
||||
You might need to create links in /etc/rc<x>.d so that the server
|
||||
start automatically at boot-up, e.g. under Debian:
|
||||
|
||||
update-rc.d sslh defaults
|
||||
|
||||
File renamed without changes.
+347
@@ -0,0 +1,347 @@
|
||||
Configuration
|
||||
=============
|
||||
|
||||
If you use the scripts provided, sslh will get its
|
||||
configuration from /etc/sslh.cfg. Please refer to
|
||||
example.cfg for an overview of all the settings.
|
||||
|
||||
A good scheme is to use the external name of the machine in
|
||||
`listen`, and bind `httpd` to `localhost:443` (instead of all
|
||||
binding to all interfaces): that way, HTTPS connections
|
||||
coming from inside your network don't need to go through
|
||||
`sslh`, and `sslh` is only there as a frontal for connections
|
||||
coming from the internet.
|
||||
|
||||
Note that 'external name' in this context refers to the
|
||||
actual IP address of the machine as seen from your network,
|
||||
i.e. that that is not `127.0.0.1` in the output of
|
||||
`ifconfig(8)`.
|
||||
|
||||
Libwrap support
|
||||
---------------
|
||||
|
||||
Sslh can optionally perform `libwrap` checks for the sshd
|
||||
service: because the connection to `sshd` will be coming
|
||||
locally from `sslh`, `sshd` cannot determine the IP of the
|
||||
client.
|
||||
|
||||
OpenVPN support
|
||||
---------------
|
||||
|
||||
OpenVPN clients connecting to OpenVPN running with
|
||||
`-port-share` reportedly take more than one second between
|
||||
the time the TCP connection is established and the time they
|
||||
send the first data packet. This results in `sslh` with
|
||||
default settings timing out and assuming an SSH connection.
|
||||
To support OpenVPN connections reliably, it is necessary to
|
||||
increase `sslh`'s timeout to 5 seconds.
|
||||
|
||||
Instead of using OpenVPN's port sharing, it is more reliable
|
||||
to use `sslh`'s `--openvpn` option to get `sslh` to do the
|
||||
port sharing.
|
||||
|
||||
Using proxytunnel with sslh
|
||||
---------------------------
|
||||
|
||||
If you are connecting through a proxy that checks that the
|
||||
outgoing connection really is SSL and rejects SSH, you can
|
||||
encapsulate all your traffic in SSL using `proxytunnel` (this
|
||||
should work with `corkscrew` as well). On the server side you
|
||||
receive the traffic with `stunnel` to decapsulate SSL, then
|
||||
pipe through `sslh` to switch HTTP on one side and SSL on the
|
||||
other.
|
||||
|
||||
In that case, you end up with something like this:
|
||||
|
||||
ssh -> proxytunnel -e ----[ssh/ssl]---> stunnel ---[ssh]---> sslh --> sshd
|
||||
Web browser -------------[http/ssl]---> stunnel ---[http]--> sslh --> httpd
|
||||
|
||||
Configuration goes like this on the server side, using `stunnel3`:
|
||||
|
||||
stunnel -f -p mycert.pem -d thelonious:443 -l /usr/local/sbin/sslh -- \
|
||||
sslh -i --http localhost:80 --ssh localhost:22
|
||||
|
||||
* stunnel options:
|
||||
* `-f` for foreground/debugging
|
||||
* `-p` for specifying the key and certificate
|
||||
* `-d` for specifying which interface and port
|
||||
we're listening to for incoming connexions
|
||||
* `-l` summons `sslh` in inetd mode.
|
||||
|
||||
* sslh options:
|
||||
* `-i` for inetd mode
|
||||
* `--http` to forward HTTP connexions to port 80,
|
||||
and SSH connexions to port 22.
|
||||
|
||||
Capabilities support
|
||||
--------------------
|
||||
|
||||
On Linux (only?), you can compile sslh with `USELIBCAP=1` to
|
||||
make use of POSIX capabilities; this will save the required
|
||||
capabilities needed for transparent proxying for unprivileged
|
||||
processes.
|
||||
|
||||
Alternatively, you may use filesystem capabilities instead
|
||||
of starting sslh as root and asking it to drop privileges.
|
||||
You will need `CAP_NET_BIND_SERVICE` for listening on port 443
|
||||
and `CAP_NET_RAW` for transparent proxying (see
|
||||
`capabilities(7)`).
|
||||
|
||||
You can use the `setcap(8)` utility to give these capabilities
|
||||
to the executable:
|
||||
|
||||
sudo setcap cap_net_bind_service,cap_net_raw+pe sslh-select
|
||||
|
||||
Then you can run sslh-select as an unpriviledged user, e.g.:
|
||||
|
||||
sslh-select -p myname:443 --ssh localhost:22 --tls localhost:443
|
||||
|
||||
Transparent proxy support
|
||||
-------------------------
|
||||
|
||||
On Linux and FreeBSD you can use the `--transparent` option to
|
||||
request transparent proxying. This means services behind `sslh`
|
||||
(Apache, `sshd` and so on) will see the external IP and ports
|
||||
as if the external world connected directly to them. This
|
||||
simplifies IP-based access control (or makes it possible at
|
||||
all).
|
||||
|
||||
You can refer to Sean Warn'ѕ [tutorial](tproxy.md) for a
|
||||
different set-up which enables transparent proxying between
|
||||
two different machines. The following may only work if
|
||||
`sslh` and the final servers are on the same machine.
|
||||
|
||||
Note that getting this to work is very tricky and
|
||||
detail-dependant: depending on whether the target server and
|
||||
sslh are on the same machine, different machines, or
|
||||
different dockers, and tool versions, all seem to change the
|
||||
required network configuration somewhat. If it doesn't work,
|
||||
it's almost certain that the problem is not linked to `sslh`
|
||||
but to the network setup that surrounds it.
|
||||
|
||||
Linux:
|
||||
|
||||
`sslh` needs extended rights to perform this: you'll need to
|
||||
give it `CAP_NET_RAW` capabilities (see appropriate chapter)
|
||||
or run it as root (but don't do that).
|
||||
|
||||
The firewalling tables also need to be adjusted as follows.
|
||||
I don't think it is possible to have `httpd` and `sslh` both listen to 443 in
|
||||
this scheme -- let me know if you manage that:
|
||||
|
||||
# Set route_localnet = 1 on all interfaces so that ssl can use "localhost" as destination
|
||||
sysctl -w net.ipv4.conf.default.route_localnet=1
|
||||
sysctl -w net.ipv4.conf.all.route_localnet=1
|
||||
|
||||
# DROP martian packets as they would have been if route_localnet was zero
|
||||
# Note: packets not leaving the server aren't affected by this, thus sslh will still work
|
||||
iptables -t raw -A PREROUTING ! -i lo -d 127.0.0.0/8 -j DROP
|
||||
iptables -t mangle -A POSTROUTING ! -o lo -s 127.0.0.0/8 -j DROP
|
||||
|
||||
# Mark all connections made by ssl for special treatment (here sslh is run as user "sslh")
|
||||
iptables -t nat -A OUTPUT -m owner --uid-owner sslh -p tcp --tcp-flags FIN,SYN,RST,ACK SYN -j CONNMARK --set-xmark 0x01/0x0f
|
||||
|
||||
# Outgoing packets that should go to sslh instead have to be rerouted, so mark them accordingly (copying over the connection mark)
|
||||
iptables -t mangle -A OUTPUT ! -o lo -p tcp -m connmark --mark 0x01/0x0f -j CONNMARK --restore-mark --mask 0x0f
|
||||
|
||||
# Configure routing for those marked packets
|
||||
ip rule add fwmark 0x1 lookup 100
|
||||
ip route add local 0.0.0.0/0 dev lo table 100
|
||||
|
||||
Tranparent proxying with IPv6 is similarly set up as follows:
|
||||
|
||||
# Set route_localnet = 1 on all interfaces so that ssl can use "localhost" as destination
|
||||
# Not sure if this is needed for ipv6 though
|
||||
sysctl -w net.ipv4.conf.default.route_localnet=1
|
||||
sysctl -w net.ipv4.conf.all.route_localnet=1
|
||||
|
||||
# DROP martian packets as they would have been if route_localnet was zero
|
||||
# Note: packets not leaving the server aren't affected by this, thus sslh will still work
|
||||
ip6tables -t raw -A PREROUTING ! -i lo -d ::1/128 -j DROP
|
||||
ip6tables -t mangle -A POSTROUTING ! -o lo -s ::1/128 -j DROP
|
||||
|
||||
# Mark all connections made by ssl for special treatment (here sslh is run as user "sslh")
|
||||
ip6tables -t nat -A OUTPUT -m owner --uid-owner sslh -p tcp --tcp-flags FIN,SYN,RST,ACK SYN -j CONNMARK --set-xmark 0x01/0x0f
|
||||
|
||||
# Outgoing packets that should go to sslh instead have to be rerouted, so mark them accordingly (copying over the connection mark)
|
||||
ip6tables -t mangle -A OUTPUT ! -o lo -p tcp -m connmark --mark 0x01/0x0f -j CONNMARK --restore-mark --mask 0x0f
|
||||
|
||||
# Configure routing for those marked packets
|
||||
ip -6 rule add fwmark 0x1 lookup 100
|
||||
ip -6 route add local ::/0 dev lo table 100
|
||||
|
||||
Explanation:
|
||||
To be able to use `localhost` as destination in your sslh config along with transparent proxying
|
||||
you have to allow routing of loopback addresses as done above.
|
||||
This is something you usually should not do (see [this stackoverflow post](https://serverfault.com/questions/656279/how-to-force-linux-to-accept-packet-with-loopback-ip/656484#656484))
|
||||
The two `DROP` iptables rules emulate the behaviour of `route_localnet` set to off (with one small difference:
|
||||
allowing the reroute-check to happen after the fwmark is set on packets destined for sslh).
|
||||
See [this diagram](https://upload.wikimedia.org/wikipedia/commons/3/37/Netfilter-packet-flow.svg) for a good visualisation
|
||||
showing how packets will traverse the iptables chains.
|
||||
|
||||
Note:
|
||||
You have to run `sslh` as dedicated user (in this example the user is also named `sslh`), to not mess up with your normal networking.
|
||||
These rules will allow you to connect directly to ssh on port
|
||||
22 (or to any other service behind sslh) as well as through sslh on port 443.
|
||||
|
||||
Also remember that iptables configuration and ip routes and
|
||||
rules won't be necessarily persisted after you reboot. Make
|
||||
sure to save them properly. For example in CentOS7, you would
|
||||
do `iptables-save > /etc/sysconfig/iptables`, and add both
|
||||
`ip` commands to your `/etc/rc.local`.
|
||||
|
||||
FreeBSD:
|
||||
|
||||
Given you have no firewall defined yet, you can use the following configuration
|
||||
to have ipfw properly redirect traffic back to sslh
|
||||
|
||||
/etc/rc.conf
|
||||
firewall_enable="YES"
|
||||
firewall_type="open"
|
||||
firewall_logif="YES"
|
||||
firewall_coscripts="/etc/ipfw/sslh.rules"
|
||||
|
||||
|
||||
/etc/ipfw/sslh.rules
|
||||
|
||||
#! /bin/sh
|
||||
|
||||
# ssl
|
||||
ipfw add 20000 fwd 192.0.2.1,443 log tcp from 192.0.2.1 8443 to any out
|
||||
ipfw add 20010 fwd 2001:db8::1,443 log tcp from 2001:db8::1 8443 to any out
|
||||
|
||||
# ssh
|
||||
ipfw add 20100 fwd 192.0.2.1,443 log tcp from 192.0.2.1 8022 to any out
|
||||
ipfw add 20110 fwd 2001:db8::1,443 log tcp from 2001:db8::1 8022 to any out
|
||||
|
||||
# xmpp
|
||||
ipfw add 20200 fwd 192.0.2.1,443 log tcp from 192.0.2.1 5222 to any out
|
||||
ipfw add 20210 fwd 2001:db8::1,443 log tcp from 2001:db8::1 5222 to any out
|
||||
|
||||
# openvpn (running on other internal system)
|
||||
ipfw add 20300 fwd 192.0.2.1,443 log tcp from 198.51.100.7 1194 to any out
|
||||
ipfw add 20310 fwd 2001:db8::1,443 log tcp from 2001:db8:1::7 1194 to any out
|
||||
|
||||
General notes:
|
||||
|
||||
|
||||
This will only work if `sslh` does not use any loopback
|
||||
addresses (no `127.0.0.1` or `localhost`), you'll need to use
|
||||
explicit IP addresses (or names):
|
||||
|
||||
sslh --listen 192.168.0.1:443 --ssh 192.168.0.1:22 --tls 192.168.0.1:4443
|
||||
|
||||
This will not work:
|
||||
|
||||
sslh --listen 192.168.0.1:443 --ssh 127.0.0.1:22 --tls 127.0.0.1:4443
|
||||
|
||||
Transparent proxying means the target server sees the real
|
||||
origin address, so it means if the client connects using
|
||||
IPv6, the server must also support IPv6. It is easy to
|
||||
support both IPv4 and IPv6 by configuring the server
|
||||
accordingly, and setting `sslh` to connect to a name that
|
||||
resolves to both IPv4 and IPv6, e.g.:
|
||||
|
||||
sslh --transparent --listen <extaddr>:443 --ssh insideaddr:22
|
||||
|
||||
/etc/hosts:
|
||||
192.168.0.1 insideaddr
|
||||
201::::2 insideaddr
|
||||
|
||||
Upon incoming IPv6 connection, `sslh` will first try to
|
||||
connect to the IPv4 address (which will fail), then connect
|
||||
to the IPv6 address.
|
||||
|
||||
Systemd Socket Activation
|
||||
-------------------------
|
||||
If compiled with `USESYSTEMD` then it is possible to activate
|
||||
the service on demand and avoid running any code as root.
|
||||
|
||||
In this mode any listen configuration options are ignored and
|
||||
the sockets are passed by systemd to the service.
|
||||
|
||||
Example socket unit:
|
||||
|
||||
[Unit]
|
||||
Before=sslh.service
|
||||
|
||||
[Socket]
|
||||
ListenStream=1.2.3.4:443
|
||||
ListenStream=5.6.7.8:444
|
||||
ListenStream=9.10.11.12:445
|
||||
FreeBind=true
|
||||
|
||||
[Install]
|
||||
WantedBy=sockets.target
|
||||
|
||||
Example service unit:
|
||||
|
||||
[Unit]
|
||||
PartOf=sslh.socket
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/sbin/sslh -v -f --ssh 127.0.0.1:22 --tls 127.0.0.1:443
|
||||
KillMode=process
|
||||
CapabilityBoundingSet=CAP_NET_BIND_SERVICE CAP_NET_RAW
|
||||
PrivateTmp=true
|
||||
PrivateDevices=true
|
||||
ProtectSystem=full
|
||||
ProtectHome=true
|
||||
User=sslh
|
||||
|
||||
|
||||
With this setup only the socket needs to be enabled. The sslh service
|
||||
will be started on demand and does not need to run as root to bind the
|
||||
sockets as systemd has already bound and passed them over. If the sslh
|
||||
service is started on its own without the sockets being passed by systemd
|
||||
then it will look to use those defined on the command line or config
|
||||
file as usual. Any number of ListenStreams can be defined in the socket
|
||||
file and systemd will pass them all over to sslh to use as usual.
|
||||
|
||||
To avoid inconsistency between starting via socket and starting directly
|
||||
via the service Requires=sslh.socket can be added to the service unit to
|
||||
mandate the use of the socket configuration.
|
||||
|
||||
Rather than overwriting the entire socket file drop in values can be placed
|
||||
in /etc/systemd/system/sslh.socket.d/<name>.conf with additional ListenStream
|
||||
values that will be merged.
|
||||
|
||||
In addition to the above with manual .socket file configuration there is an
|
||||
optional systemd generator which can be compiled - systemd-sslh-generator
|
||||
|
||||
This parses the /etc/sslh.cfg (or /etc/sslh/sslh.cfg file if that exists
|
||||
instead) configuration file and dynamically generates a socket file to use.
|
||||
|
||||
This will also merge with any sslh.socket.d drop in configuration but will be
|
||||
overriden by a /etc/systemd/system/sslh.socket file.
|
||||
|
||||
To use the generator place it in /usr/lib/systemd/system-generators and then
|
||||
call systemctl daemon-reload after any changes to /etc/sslh.cfg to generate
|
||||
the new dynamic socket unit.
|
||||
|
||||
Fail2ban
|
||||
--------
|
||||
|
||||
If using transparent proxying, just use the standard ssh
|
||||
rules. If you can't or don't want to use transparent
|
||||
proxying, you can set `fail2ban` rules to block repeated ssh
|
||||
connections from an IP address (obviously this depends
|
||||
on the site, there might be legitimate reasons you would get
|
||||
many connections to ssh from the same IP address...)
|
||||
|
||||
See example files in scripts/fail2ban.
|
||||
|
||||
UDP
|
||||
---
|
||||
|
||||
`sslh` can perform demultiplexing on UDP packets as well.
|
||||
This only works with `sslh-select` (it is not possible to
|
||||
support UDP with a forking model). Specify a listening
|
||||
address and target protocols with `is_udp: true`. `sslh`
|
||||
will wait for incoming UDP packets, run the probes in the
|
||||
usual fashion, and forward packets to the appropriate
|
||||
target. `sslh` will then remember the association between
|
||||
remote host to target server for 60 seconds by default,
|
||||
which can be overriden with `udp_timeout`. This allows to
|
||||
process both single-datagram protocols such as DNS, and
|
||||
connection-based protocols such as QUIC.
|
||||
+272
@@ -0,0 +1,272 @@
|
||||
Transparent Proxy to Two Hosts
|
||||
==============================
|
||||
|
||||
Tutorial by Sean Warner. 19 June 2019 20:35
|
||||
|
||||
Aim
|
||||
---
|
||||
|
||||
* Show that `sslh` can transparently proxy requests from the internet to services on two separate hosts that are both on the same LAN.
|
||||
* The IP address of the client initiating the request is what the destination should see… and not the IP address of the host that `sslh` is running on, which is what happens when `sslh` is not running in transparent mode.
|
||||
* The solution here only works for my very specific use-case but hopefully others can adapt it to suits their needs.
|
||||
|
||||
Overview of my Network
|
||||
----------------------
|
||||
|
||||
Two Raspberry Pis on my home LAN:
|
||||
* Pi A: 192.168.1.124 – `sslh` (Port 4433), Apache2 web server for https (port 443), `stunnel` (port 4480) to decrypt ssh traffic and forward to SSH server (also on Pi A at Port 1022)
|
||||
* Pi B: 192.168.1.123 - HTTP server (port 8000), SSH server (port 1022 on PiB).
|
||||
* I send traffic from the internet to my router's external port 443 then use a port forward rule in my router to map that to internal port 4433 where sslh is listening.
|
||||
|
||||

|
||||
|
||||
`sslh` build
|
||||
------------
|
||||
|
||||
`sslh` Version: sslh v1.19c-2-gf451cc8-dirty.
|
||||
|
||||
I compiled sslh from sources giving the binary pretty much all possible options such as Posix capabilities and systemd support.. here are the first few lines of the makefile:
|
||||
|
||||
```
|
||||
# Configuration
|
||||
|
||||
VERSION=$(shell ./genver.sh -r)
|
||||
ENABLE_REGEX=1 # Enable regex probes
|
||||
USELIBCONFIG=1 # Use libconfig? (necessary to use configuration files)
|
||||
USELIBPCRE=1 # Use libpcre? (needed for regex on musl)
|
||||
USELIBWRAP=1 # Use libwrap?
|
||||
USELIBCAP=1 # Use libcap?
|
||||
USESYSTEMD=1 # Make use of systemd socket activation
|
||||
COV_TEST= # Perform test coverage?
|
||||
PREFIX=/usr/local
|
||||
BINDIR=$(PREFIX)/sbin
|
||||
MANDIR=$(PREFIX)/share/man/man8
|
||||
MAN=sslh.8.gz # man page name
|
||||
|
||||
# End of configuration -- the rest should take care of
|
||||
# itself
|
||||
```
|
||||
|
||||
systemd setup
|
||||
-------------
|
||||
|
||||
Create an sslh systemd service file...
|
||||
```
|
||||
# nano /lib/systemd/system/sslh.service
|
||||
```
|
||||
|
||||
Paste in this contents…
|
||||
|
||||
```
|
||||
[Unit]
|
||||
Description=SSL/SSH multiplexer
|
||||
After=network.target
|
||||
Documentation=man:sslh(8)
|
||||
|
||||
[Service]
|
||||
#EnvironmentFile=/etc/default/sslh
|
||||
#ExecStart=/usr/local/sbin/sslh $DAEMON_OPTS
|
||||
ExecStart=/usr/local/sbin/sslh -F /etc/sslh/sslh.cfg
|
||||
KillMode=process
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
Save it and then…
|
||||
```
|
||||
# systemctl daemon-reload
|
||||
```
|
||||
|
||||
Start it again to test…
|
||||
```
|
||||
# systemctl start sslh
|
||||
```
|
||||
|
||||
Configure `sslh`
|
||||
----------------
|
||||
|
||||
First stop `sslh` then open the config file and replace with below, save and start `sslh` again
|
||||
```
|
||||
# systemctl stop sslh
|
||||
# nano /etc/sslh/sslh.cfg
|
||||
# systemctl start sslh
|
||||
```
|
||||
|
||||
```
|
||||
verbose: true;
|
||||
foreground: true;
|
||||
inetd: false;
|
||||
numeric: true;
|
||||
transparent: true;
|
||||
timeout: 2;
|
||||
user: "sslh";
|
||||
pidfile: "/var/run/sslh.pid";
|
||||
chroot: "/var/empty";
|
||||
|
||||
# You must have a port forward rule in the router: external port 443 <-> internal port 4433
|
||||
# Local ip address of PiA is: 192.168.1.124, sslh and stunnel4 are running on this Pi
|
||||
# Local ip address of PiB is: 192.168.1.123, http server and ssh server on this Pi
|
||||
listen:
|
||||
(
|
||||
{ host: "192.168.1.124"; port: "4433"; }
|
||||
);
|
||||
|
||||
# sslh demultiplexes based on the Protocol and Hostname
|
||||
protocols:
|
||||
(
|
||||
{ name: "tls"; sni_hostnames: [ "www.example.com" ]; host: "192.168.1.124"; port: "443"; log_level: 1; },
|
||||
# This probe is for tls encrypted ssh. SSLH forwards it to stunnel on port 4480 which decrypts it and sends it to the ssh server on PiA port 1022
|
||||
{ name: "tls"; sni_hostnames: [ "ssh.example.com" ]; host: "192.168.1.124"; port: "4480"; log_level: 1; },
|
||||
{ name: "http"; host: "192.168.1.123"; port: "8000"; log_level: 1; },
|
||||
{ name: "ssh"; host: "192.168.1.123"; port: "1022"; log_level: 1; }
|
||||
);
|
||||
```
|
||||
|
||||
Configure `stunnel`
|
||||
-------------------
|
||||
|
||||
First stop `stunnel` then open the config file and replace with below, save and start `stunnel` again
|
||||
```
|
||||
# systemctl stop stunnel4
|
||||
# nano /etc/stunnel/stunnel.conf
|
||||
# systemctl start stunnel4
|
||||
```
|
||||
|
||||
```
|
||||
# Debugging stuff (may be useful for troubleshooting)
|
||||
foreground = yes
|
||||
#debug = 5 # this is the default
|
||||
debug = 7
|
||||
output = /var/log/stunnel4/stunnel.log
|
||||
pid = /var/run/stunnel4/stunnel.pid
|
||||
fips = no
|
||||
|
||||
cert = /etc/letsencrypt/live/example.com/fullchain.pem
|
||||
key = /etc/letsencrypt/live/example.com/privkey.pem
|
||||
|
||||
[ssh]
|
||||
accept = 192.168.1.124:4480
|
||||
connect = 192.168.1.124:1022
|
||||
TIMEOUTclose = 0
|
||||
```
|
||||
|
||||
Configure iptables for Pi A
|
||||
--------------------------
|
||||
|
||||
The `_add.sh` script creates the rules, the `_rm.sh` script removes the rules.
|
||||
They will be lost if you reboot but there are ways to make them load again on start-up..
|
||||
```
|
||||
# nano /usr/local/sbin/piA_tproxy_add.sh
|
||||
```
|
||||
``` piA_tproxy_add.sh
|
||||
iptables -t mangle -N SSLH
|
||||
iptables -t mangle -A PREROUTING -p tcp -m socket --transparent -j SSLH
|
||||
iptables -t mangle -A OUTPUT --protocol tcp --out-interface eth0 -m multiport --sport 443,4480 --jump SSLH
|
||||
iptables -t mangle -A SSLH --jump MARK --set-mark 0x1
|
||||
iptables -t mangle -A SSLH --jump ACCEPT
|
||||
ip rule add fwmark 0x1 lookup 100
|
||||
ip route add local 0.0.0.0/0 dev lo table 100
|
||||
```
|
||||
|
||||
```
|
||||
# nano /usr/local/sbin/piA_tproxy_rm.sh
|
||||
```
|
||||
``` piA_tproxy_rm.sh
|
||||
iptables -t mangle -D PREROUTING -p tcp -m socket --transparent -j SSLH
|
||||
iptables -t mangle -D OUTPUT --protocol tcp --out-interface eth0 -m multiport --sport 443,4480 --jump SSLH
|
||||
iptables -t mangle -D SSLH --jump MARK --set-mark 0x1
|
||||
iptables -t mangle -D SSLH --jump ACCEPT
|
||||
iptables -t mangle -X SSLH
|
||||
ip rule del fwmark 0x1 lookup 100
|
||||
ip route del local 0.0.0.0/0 dev lo table 100
|
||||
```
|
||||
|
||||
Make them executable..
|
||||
```
|
||||
# chmod +rx piA_tproxy_add.sh
|
||||
# chmod +rx piA_tproxy_rm.sh
|
||||
```
|
||||
|
||||
Now run the "add" script on Pi A!
|
||||
```
|
||||
# piA_tproxy_add.sh
|
||||
# piA_tproxy_rm.sh
|
||||
```
|
||||
|
||||
Configure iptables for Pi B
|
||||
--------------------------
|
||||
|
||||
```
|
||||
# nano /usr/local/sbin/piB_tproxy_add.sh
|
||||
```
|
||||
``` piB_tproxy_add.sh
|
||||
iptables -t mangle -N SSLHSSL
|
||||
iptables -t mangle -A OUTPUT -o eth0 -p tcp -m multiport --sport 1022,8000 -j SSLHSSL
|
||||
iptables -t mangle -A SSLHSSL --jump MARK --set-mark 0x1
|
||||
iptables -t mangle -A SSLHSSL --jump ACCEPT
|
||||
ip rule add fwmark 0x1 lookup 100
|
||||
ip route add default via 192.168.1.124 table 100
|
||||
ip route flush cache
|
||||
```
|
||||
|
||||
```
|
||||
# nano /usr/local/sbin/piB_tproxy_rm.sh
|
||||
```
|
||||
```
|
||||
iptables -t mangle -D OUTPUT -o eth0 -p tcp -m multiport --sport 1022,8000 -j SSLHSSL
|
||||
iptables -t mangle -D SSLHSSL --jump MARK --set-mark 0x1
|
||||
iptables -t mangle -D SSLHSSL --jump ACCEPT
|
||||
iptables -t mangle -X SSLHSSL
|
||||
ip rule del fwmark 0x1 lookup 100
|
||||
ip route del default via 192.168.1.124 table 100
|
||||
ip route flush cache
|
||||
```
|
||||
|
||||
Make them executable..
|
||||
```
|
||||
# chmod +rx piB_tproxy_add.sh
|
||||
# chmod +rx piB_tproxy_rm.sh
|
||||
```
|
||||
|
||||
Now run the "add" script on Pi B!
|
||||
```
|
||||
# piB_tproxy_add.sh
|
||||
# piB_tproxy_rm.sh
|
||||
```
|
||||
|
||||
Testing
|
||||
-------
|
||||
* Getting to sshd on PiA
|
||||
|
||||
I did this test using 4G from my phone (outside the LAN)
|
||||
|
||||
To simulate this I use `proxytunnel`. External port 443 is forwarded by my router to 4433. I need to arrive at `sslh` (port 4433) with ssh encrypted as TLS (hence I use the -e switch) and the `sni_hostname` set to ssh.example.com so that `sslh` will demultiplex to `stunnel` (port 4480) which will decrypt and forward to ssh server on PiA… see `sslh.cfg` and `stunnel.conf`.
|
||||
|
||||
The first IP:port is just a free HTTPS proxy I found on https://free-proxy-list.net
|
||||
I execute this command from a terminal window..
|
||||
|
||||
```
|
||||
# proxytunnel -v -e -C root.pem -p 78.141.192.198:8080 -d ssh.example.com:443
|
||||
```
|
||||
* Getting to sshd on PiB
|
||||
|
||||
I did this test using 4G from my phone (outside the LAN)
|
||||
|
||||
My smartphone telecom provider blocks ssh over port 443 so I need to use `proxytunnel` to encrypt.
|
||||
|
||||
Use the Proxytunnel `-X` switch to encrypt from local proxy to destination only so by the time we get to the destination it is unencrypted and `sslh` will see the ssh protocol and demultiplex to PiB as per `sslh.cfg`.
|
||||
|
||||
```
|
||||
# proxytunnel -v -X -C root.pem -p 78.141.192.198:8080 -d ssh.example.com:443
|
||||
```
|
||||
|
||||
Now when you test it all look at the output in daemon.log like this:
|
||||
```
|
||||
# grep -i 'ssl' /var/log/daemon.log
|
||||
```
|
||||
You should see that the IP address and port from the “connection from” and “forwarded from” fields are the same.
|
||||
|
||||
Special thanks and appreciation to Michael Yelsukov without whom I would never have got this working.
|
||||
|
||||
Any feedback or corrections very welcome!
|
||||
+635
@@ -0,0 +1,635 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<!-- Created with Inkscape (http://www.inkscape.org/) -->
|
||||
|
||||
<svg
|
||||
xmlns:osb="http://www.openswatchbook.org/uri/2009/osb"
|
||||
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
||||
xmlns:cc="http://creativecommons.org/ns#"
|
||||
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
||||
xmlns:svg="http://www.w3.org/2000/svg"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
|
||||
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
|
||||
width="297mm"
|
||||
height="210mm"
|
||||
viewBox="0 0 297 210"
|
||||
version="1.1"
|
||||
id="svg288"
|
||||
inkscape:version="0.92.4 (5da689c313, 2019-01-14)"
|
||||
sodipodi:docname="Transparent Proxy to Two Hosts-new.svg">
|
||||
<defs
|
||||
id="defs282">
|
||||
<linearGradient
|
||||
id="linearGradient4840"
|
||||
osb:paint="solid">
|
||||
<stop
|
||||
style="stop-color:#000000;stop-opacity:1;"
|
||||
offset="0"
|
||||
id="stop4838" />
|
||||
</linearGradient>
|
||||
<linearGradient
|
||||
id="linearGradient4802"
|
||||
osb:paint="solid">
|
||||
<stop
|
||||
style="stop-color:#000000;stop-opacity:1;"
|
||||
offset="0"
|
||||
id="stop4800" />
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<sodipodi:namedview
|
||||
id="base"
|
||||
pagecolor="#ffffff"
|
||||
bordercolor="#666666"
|
||||
borderopacity="1.0"
|
||||
inkscape:pageopacity="0.0"
|
||||
inkscape:pageshadow="2"
|
||||
inkscape:zoom="1.01"
|
||||
inkscape:cx="511.12411"
|
||||
inkscape:cy="388.13656"
|
||||
inkscape:document-units="mm"
|
||||
inkscape:current-layer="layer1"
|
||||
showgrid="false"
|
||||
inkscape:snap-page="true"
|
||||
inkscape:window-width="1920"
|
||||
inkscape:window-height="1005"
|
||||
inkscape:window-x="-9"
|
||||
inkscape:window-y="-9"
|
||||
inkscape:window-maximized="1"
|
||||
showguides="false" />
|
||||
<metadata
|
||||
id="metadata285">
|
||||
<rdf:RDF>
|
||||
<cc:Work
|
||||
rdf:about="">
|
||||
<dc:format>image/svg+xml</dc:format>
|
||||
<dc:type
|
||||
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
|
||||
<dc:title />
|
||||
</cc:Work>
|
||||
</rdf:RDF>
|
||||
</metadata>
|
||||
<g
|
||||
inkscape:label="Layer 1"
|
||||
inkscape:groupmode="layer"
|
||||
id="layer1"
|
||||
transform="translate(0,-87)">
|
||||
<rect
|
||||
style="fill:#000000;fill-opacity:0;stroke:#fa9600;stroke-width:0.68479234;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
|
||||
id="rect4977-0"
|
||||
width="37.333149"
|
||||
height="17.888765"
|
||||
x="234.24397"
|
||||
y="232.81883" />
|
||||
<ellipse
|
||||
id="path290"
|
||||
cx="38.405174"
|
||||
cy="133.55173"
|
||||
rx="21.853449"
|
||||
ry="28.965515"
|
||||
style="stroke-width:0.26458332;fill-opacity:1;fill:none" />
|
||||
<ellipse
|
||||
style="fill:none;stroke-width:0.26458332"
|
||||
id="path4806"
|
||||
cx="44.482758"
|
||||
cy="135.49138"
|
||||
rx="28.965515"
|
||||
ry="27.025862" />
|
||||
<ellipse
|
||||
style="fill:none;stroke-width:0.26458332"
|
||||
id="path4808"
|
||||
cx="124.26724"
|
||||
cy="151.26724"
|
||||
rx="29.612068"
|
||||
ry="24.698277" />
|
||||
<ellipse
|
||||
style="fill:none;fill-opacity:1;stroke:#000000;stroke-width:0.76499999;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
|
||||
id="path4812"
|
||||
ry="30.796005"
|
||||
rx="24.16864"
|
||||
cy="140.54999"
|
||||
cx="43.31974" />
|
||||
<rect
|
||||
style="fill:#000000;fill-opacity:0;stroke:#000000;stroke-width:0.59869254;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
|
||||
id="rect4852"
|
||||
width="27.302826"
|
||||
height="13.554869"
|
||||
x="29.797318"
|
||||
y="133.28833" />
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:6.6431241px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.31139645"
|
||||
x="38.145908"
|
||||
y="130.0871"
|
||||
id="text138"
|
||||
transform="scale(0.9172844,1.0901744)"><tspan
|
||||
sodipodi:role="line"
|
||||
x="38.145908"
|
||||
y="130.0871"
|
||||
id="tspan136"
|
||||
style="stroke-width:0.31139645"><tspan
|
||||
x="38.145908"
|
||||
y="130.0871"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:4.93888903px;font-family:'Courier New';-inkscape-font-specification:'Courier New';stroke-width:0.31139645"
|
||||
id="tspan134">Client</tspan></tspan></text>
|
||||
<rect
|
||||
style="fill:#000000;fill-opacity:0;stroke:#000000;stroke-width:0.66499996;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
|
||||
id="rect4862"
|
||||
width="20.232847"
|
||||
height="37.612331"
|
||||
x="101.42361"
|
||||
y="121.64869" />
|
||||
<rect
|
||||
style="fill:#000000;fill-opacity:0;stroke:#000000;stroke-width:0.67072004;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
|
||||
id="rect4864"
|
||||
width="121.91585"
|
||||
height="91.301468"
|
||||
x="156.41544"
|
||||
y="116.72592" />
|
||||
<rect
|
||||
style="fill:#000000;fill-opacity:0;stroke:#000000;stroke-width:0.66499996;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
|
||||
id="rect4866"
|
||||
width="122.95342"
|
||||
height="37.871742"
|
||||
x="155.89668"
|
||||
y="223.04117" />
|
||||
<rect
|
||||
style="fill:#000000;fill-opacity:0;stroke:#f40000;stroke-width:0.701;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
|
||||
id="rect4868"
|
||||
width="82.747147"
|
||||
height="23.645355"
|
||||
x="175.0919"
|
||||
y="128.09322" />
|
||||
<rect
|
||||
style="fill:#000000;fill-opacity:0;stroke:#009600;stroke-width:0.66499996;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
|
||||
id="rect4870-7"
|
||||
width="26.977121"
|
||||
height="14.785542"
|
||||
x="220.4861"
|
||||
y="185.84906" />
|
||||
<rect
|
||||
style="fill:#000000;fill-opacity:0;stroke:#009600;stroke-width:0.66499996;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
|
||||
id="rect4870-7-1"
|
||||
width="26.977123"
|
||||
height="14.785542"
|
||||
x="184.17078"
|
||||
y="185.84906" />
|
||||
<rect
|
||||
style="fill:#000000;fill-opacity:0;stroke:#009600;stroke-width:0.66499996;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
|
||||
id="rect4919"
|
||||
width="27.495911"
|
||||
height="11.413391"
|
||||
x="219.96732"
|
||||
y="164.44894" />
|
||||
<rect
|
||||
style="fill:none;fill-opacity:0;stroke:#800080;stroke-width:0.85740757;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:0.96517412"
|
||||
id="rect4921"
|
||||
width="139.8217"
|
||||
height="161.06387"
|
||||
x="148.11099"
|
||||
y="108.15634" />
|
||||
<path
|
||||
style="fill:#000000;stroke:#c98f26;stroke-width:0.665;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:0.88627452;stroke-miterlimit:4;stroke-dasharray:none;fill-opacity:0"
|
||||
d="m 57.326389,140.58456 c 43.837821,-0.25939 44.097221,-0.25939 44.097221,-0.25939"
|
||||
id="path4925"
|
||||
inkscape:connector-curvature="0" />
|
||||
<path
|
||||
style="fill:#000000;fill-opacity:0;stroke:#c98f26;stroke-width:0.665;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:0.88627451"
|
||||
d="m 122.01937,140.18462 c 53.1252,-0.25845 53.43955,-0.25845 53.43955,-0.25845"
|
||||
id="path4925-7"
|
||||
inkscape:connector-curvature="0" />
|
||||
<path
|
||||
style="fill:#000000;fill-opacity:0;stroke:#c98f26;stroke-width:0.58399999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:1.75200005, 0.58400002;stroke-dashoffset:0;stroke-opacity:0.88627451"
|
||||
d="m 101.35344,140.71552 c 19.77892,-0.26191 19.89596,-0.26191 19.89596,-0.26191"
|
||||
id="path4925-4"
|
||||
inkscape:connector-curvature="0" />
|
||||
<path
|
||||
style="fill:none;stroke:#009600;stroke-width:0.665;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;stroke-miterlimit:4;stroke-dasharray:none"
|
||||
d="m 198.1781,185.71937 c -0.25939,-33.9808 -0.25939,-33.9808 -0.25939,-33.9808"
|
||||
id="path4971"
|
||||
inkscape:connector-curvature="0" />
|
||||
<path
|
||||
style="fill:none;stroke:#009600;stroke-width:0.665;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;stroke-miterlimit:4;stroke-dasharray:none"
|
||||
d="m 233.45588,164.18955 c 0,-12.45098 0,-12.45098 0,-12.45098"
|
||||
id="path4973"
|
||||
inkscape:connector-curvature="0" />
|
||||
<path
|
||||
style="fill:none;stroke:#009600;stroke-width:0.665;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;stroke-miterlimit:4;stroke-dasharray:none"
|
||||
d="m 233.19648,185.45997 c 0,-9.59763 0,-9.59763 0,-9.59763"
|
||||
id="path4975"
|
||||
inkscape:connector-curvature="0" />
|
||||
<rect
|
||||
style="fill:none;fill-opacity:0;stroke:#fa9600;stroke-width:0.665;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
|
||||
id="rect4977"
|
||||
width="37.35294"
|
||||
height="16.860703"
|
||||
x="161.2299"
|
||||
y="233.1991" />
|
||||
<path
|
||||
style="fill:none;stroke:#fa9600;stroke-width:0.665;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:0.60784316;stroke-miterlimit:4;stroke-dasharray:none"
|
||||
d="m 178.79609,151.73857 c 0.25941,81.45016 0.25941,81.45016 0.25941,81.45016"
|
||||
id="path4994"
|
||||
inkscape:connector-curvature="0" />
|
||||
<path
|
||||
style="fill:none;stroke:#fa9600;stroke-width:0.66078299;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:0.60784316"
|
||||
d="m 253.45564,151.97084 c 0.25959,80.36694 0.25959,80.36694 0.25959,80.36694"
|
||||
id="path4994-0"
|
||||
inkscape:connector-curvature="0" />
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:10.58333302px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="32.424427"
|
||||
y="103.23161"
|
||||
id="text5013"><tspan
|
||||
sodipodi:role="line"
|
||||
id="tspan5011"
|
||||
x="32.424427"
|
||||
y="103.23161"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:5.29166698px;line-height:0;font-family:'Courier New';-inkscape-font-specification:'Courier New';stroke-width:0.26458332">Internet</tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:10.58333302px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="102.3172"
|
||||
y="116.44047"
|
||||
id="text5013-6"><tspan
|
||||
sodipodi:role="line"
|
||||
id="tspan5011-2"
|
||||
x="102.3172"
|
||||
y="116.44047"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:5.29166698px;line-height:0;font-family:'Courier New';-inkscape-font-specification:'Courier New';stroke-width:0.26458332">Router</tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:10.58333302px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#800080;fill-opacity:0.96470588;stroke:none;stroke-width:0.26458332"
|
||||
x="188.76727"
|
||||
y="105.30679"
|
||||
id="text5053"><tspan
|
||||
sodipodi:role="line"
|
||||
id="tspan5051"
|
||||
x="188.76727"
|
||||
y="105.30679"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:4.93888903px;font-family:'Courier New';-inkscape-font-specification:'Courier New';fill:#800080;fill-opacity:0.96470588;stroke:none;stroke-width:0.26458332;stroke-opacity:0.96517412">Local Area Network</tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:10.58333302px;line-height:1.25;font-family:'Courier New';-inkscape-font-specification:'Courier New';letter-spacing:0px;word-spacing:0px;fill:#c98f26;fill-opacity:0.88627451;stroke:none;stroke-width:0.26458332"
|
||||
x="92.344772"
|
||||
y="138.5094"
|
||||
id="text5057"><tspan
|
||||
sodipodi:role="line"
|
||||
id="tspan5055"
|
||||
x="92.344772"
|
||||
y="138.5094"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:4.23333311px;font-family:'Courier New';-inkscape-font-specification:'Courier New';fill:#c98f26;fill-opacity:0.88627451;stroke-width:0.26458332">443</tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:10.58333302px;line-height:1.25;font-family:'Courier New';-inkscape-font-specification:'Courier New';letter-spacing:0px;word-spacing:0px;fill:#c98f26;fill-opacity:0.88627451;stroke:none;stroke-width:0.26458332"
|
||||
x="92.414749"
|
||||
y="138.5529"
|
||||
id="text5057-0"><tspan
|
||||
sodipodi:role="line"
|
||||
id="tspan5055-6"
|
||||
x="92.414749"
|
||||
y="138.5529"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:4.23333311px;font-family:'Courier New';-inkscape-font-specification:'Courier New';fill:#c98f26;fill-opacity:0.88627451;stroke-width:0.26458332">443</tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:10.58333302px;line-height:1.25;font-family:'Courier New';-inkscape-font-specification:'Courier New';letter-spacing:0px;word-spacing:0px;fill:#c98f26;fill-opacity:0.88627451;stroke:none;stroke-width:0.26458332"
|
||||
x="92.414749"
|
||||
y="138.5529"
|
||||
id="text5057-0-8"><tspan
|
||||
sodipodi:role="line"
|
||||
id="tspan5055-6-2"
|
||||
x="92.414749"
|
||||
y="138.5529"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:4.23333333px;font-family:'Courier New';-inkscape-font-specification:'Courier New';fill:#c98f26;fill-opacity:0.88627451;stroke-width:0.26458332">443</tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:11.3396225px;line-height:1.25;font-family:'Courier New';-inkscape-font-specification:'Courier New';letter-spacing:0px;word-spacing:0px;fill:#c98f26;fill-opacity:0.88627451;stroke:none;stroke-width:0.28349054"
|
||||
x="151.81532"
|
||||
y="147.89809"
|
||||
id="text5057-0-8-6"
|
||||
transform="scale(1.0714604,0.93330561)"><tspan
|
||||
sodipodi:role="line"
|
||||
id="tspan5055-6-2-7"
|
||||
x="151.81532"
|
||||
y="147.89809"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:4.23333333px;font-family:'Courier New';-inkscape-font-specification:'Courier New';fill:#c98f26;fill-opacity:0.88627451;stroke-width:0.28349054">4433</tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:11.33962345px;line-height:1.25;font-family:'Courier New';-inkscape-font-specification:'Courier New';letter-spacing:0px;word-spacing:0px;fill:#c98f26;fill-opacity:0.88627451;stroke:none;stroke-width:0.28349057"
|
||||
x="114.9891"
|
||||
y="148.08023"
|
||||
id="text5057-0-8-6-4"
|
||||
transform="scale(1.0714604,0.93330561)"><tspan
|
||||
sodipodi:role="line"
|
||||
id="tspan5055-6-2-7-3"
|
||||
x="114.9891"
|
||||
y="148.08023"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:4.23333311px;font-family:'Courier New';-inkscape-font-specification:'Courier New';fill:#c98f26;fill-opacity:0.88627451;stroke-width:0.28349057">4433</tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:10.58333302px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="208.55414"
|
||||
y="135.19525"
|
||||
id="text144"><tspan
|
||||
sodipodi:role="line"
|
||||
x="208.55414"
|
||||
y="135.19525"
|
||||
id="tspan142"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="208.55414"
|
||||
y="135.19525"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:5.64444447px;font-family:'Courier New';-inkscape-font-specification:'Courier New';fill:#f40000;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan140">SSLH</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="158.75"
|
||||
y="121.48432"
|
||||
id="text5159"><tspan
|
||||
sodipodi:role="line"
|
||||
id="tspan5157"
|
||||
x="158.75"
|
||||
y="121.48432"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';stroke-width:0.26458332;font-size:3.88055556px">192.168.1.124 (Pi A)</tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="157.30107"
|
||||
y="258.37653"
|
||||
id="text5159-0"><tspan
|
||||
sodipodi:role="line"
|
||||
id="tspan5157-1"
|
||||
x="157.30107"
|
||||
y="258.37653"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';stroke-width:0.26458332">192.168.1.123 (Pi B)</tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#fa9600;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="167.25511"
|
||||
y="242.15076"
|
||||
id="text168"><tspan
|
||||
sodipodi:role="line"
|
||||
x="167.25511"
|
||||
y="242.15076"
|
||||
id="tspan166"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="167.25511"
|
||||
y="242.15076"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:4.23333311px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#fa9600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan164">Web Server</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#fa9600;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="239.92085"
|
||||
y="241.98419"
|
||||
id="text174"><tspan
|
||||
sodipodi:role="line"
|
||||
x="239.92085"
|
||||
y="241.98419"
|
||||
id="tspan172"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="239.92085"
|
||||
y="241.98419"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:4.23333311px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#fa9600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan170">SSH Server</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="167.9187"
|
||||
y="190.6398"
|
||||
id="text186"><tspan
|
||||
sodipodi:role="line"
|
||||
x="167.9187"
|
||||
y="190.6398"
|
||||
id="tspan184"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="167.9187"
|
||||
y="190.6398"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#fa9600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan182">http</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="167.48399"
|
||||
y="157.93468"
|
||||
id="text180"><tspan
|
||||
sodipodi:role="line"
|
||||
x="167.48399"
|
||||
y="157.93468"
|
||||
id="tspan178"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="167.48399"
|
||||
y="157.93468"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#fa9600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan176">8000</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="167.74596"
|
||||
y="229.97469"
|
||||
id="text192"><tspan
|
||||
sodipodi:role="line"
|
||||
x="167.74596"
|
||||
y="229.97469"
|
||||
id="tspan190"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="167.74596"
|
||||
y="229.97469"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#fa9600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan188">8000</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="255.3237"
|
||||
y="190.91431"
|
||||
id="text246"><tspan
|
||||
sodipodi:role="line"
|
||||
x="255.3237"
|
||||
y="190.91431"
|
||||
id="tspan244"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="255.3237"
|
||||
y="190.91431"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#fa9600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan242">ssh</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="254.88899"
|
||||
y="158.20918"
|
||||
id="text240"><tspan
|
||||
sodipodi:role="line"
|
||||
x="254.88899"
|
||||
y="158.20918"
|
||||
id="tspan238"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="254.88899"
|
||||
y="158.20918"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#fa9600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan236">1022</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="255.15096"
|
||||
y="230.24921"
|
||||
id="text252"><tspan
|
||||
sodipodi:role="line"
|
||||
x="255.15096"
|
||||
y="230.24921"
|
||||
id="tspan250"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="255.15096"
|
||||
y="230.24921"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#fa9600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan248">1022</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#009600;fill-opacity:1;stroke:none;stroke-width:0.26458332;stroke-opacity:1"
|
||||
x="186.26637"
|
||||
y="193.70544"
|
||||
id="text156"><tspan
|
||||
sodipodi:role="line"
|
||||
x="186.26637"
|
||||
y="193.70544"
|
||||
id="tspan154"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="186.26637"
|
||||
y="193.70544"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#009600;fill-opacity:1;stroke:none;stroke-width:0.26458332;stroke-opacity:1"
|
||||
id="tspan152">Web Server</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#009600;fill-opacity:1;stroke:none;stroke-width:0.26458332;stroke-opacity:1"
|
||||
x="225.25404"
|
||||
y="170.88316"
|
||||
id="text150"><tspan
|
||||
sodipodi:role="line"
|
||||
x="225.25404"
|
||||
y="170.88316"
|
||||
id="tspan148"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="225.25404"
|
||||
y="170.88316"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#009600;fill-opacity:1;stroke:none;stroke-width:0.26458332;stroke-opacity:1"
|
||||
id="tspan146">STUNNEL</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#009600;fill-opacity:1;stroke:none;stroke-width:0.26458332;stroke-opacity:1"
|
||||
x="222.6344"
|
||||
y="194.19792"
|
||||
id="text162"><tspan
|
||||
sodipodi:role="line"
|
||||
x="222.6344"
|
||||
y="194.19792"
|
||||
id="tspan160"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="222.6344"
|
||||
y="194.19792"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#009600;fill-opacity:1;stroke:none;stroke-width:0.26458332;stroke-opacity:1"
|
||||
id="tspan158">SSH Server</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#009600;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="199.15128"
|
||||
y="157.29231"
|
||||
id="text198"><tspan
|
||||
sodipodi:role="line"
|
||||
x="199.15128"
|
||||
y="157.29231"
|
||||
id="tspan196"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="199.15128"
|
||||
y="157.29231"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#009600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan194">443</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#009600;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="199.41324"
|
||||
y="182.17886"
|
||||
id="text210"><tspan
|
||||
sodipodi:role="line"
|
||||
x="199.41324"
|
||||
y="182.17886"
|
||||
id="tspan208"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="199.41324"
|
||||
y="182.17886"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#009600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan206">443</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#009600;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="199.15128"
|
||||
y="170.65247"
|
||||
id="text204"><tspan
|
||||
sodipodi:role="line"
|
||||
x="199.15128"
|
||||
y="170.65247"
|
||||
id="tspan202"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="199.15128"
|
||||
y="170.65247"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#009600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan200">tls</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#009600;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="223.09605"
|
||||
y="158.60213"
|
||||
id="text216"><tspan
|
||||
sodipodi:role="line"
|
||||
x="223.09605"
|
||||
y="158.60213"
|
||||
id="tspan214"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="223.09605"
|
||||
y="158.60213"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#009600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan212">4480</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#009600;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="235.14638"
|
||||
y="158.60213"
|
||||
id="text222"><tspan
|
||||
sodipodi:role="line"
|
||||
x="235.14638"
|
||||
y="158.60213"
|
||||
id="tspan220"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="235.14638"
|
||||
y="158.60213"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#009600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan218">tls</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#009600;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="222.57213"
|
||||
y="182.44083"
|
||||
id="text228"><tspan
|
||||
sodipodi:role="line"
|
||||
x="222.57213"
|
||||
y="182.44083"
|
||||
id="tspan226"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="222.57213"
|
||||
y="182.44083"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#009600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan224">1022</tspan></tspan></text>
|
||||
<text
|
||||
xml:space="preserve"
|
||||
style="font-style:normal;font-weight:normal;font-size:3.52777767px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#009600;fill-opacity:1;stroke:none;stroke-width:0.26458332"
|
||||
x="234.88441"
|
||||
y="182.44083"
|
||||
id="text234"><tspan
|
||||
sodipodi:role="line"
|
||||
x="234.88441"
|
||||
y="182.44083"
|
||||
id="tspan232"
|
||||
style="stroke-width:0.26458332"><tspan
|
||||
x="234.88441"
|
||||
y="182.44083"
|
||||
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.88055563px;font-family:'Courier New';-inkscape-font-specification:'Courier New Bold';fill:#009600;fill-opacity:1;stroke-width:0.26458332"
|
||||
id="tspan230">ssh</tspan></tspan></text>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 32 KiB |
+1307
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,66 @@
|
||||
/* Generated by conf2struct (https://www.rutschle.net/tech/conf2struct/README)
|
||||
* on Fri Aug 13 18:03:20 2021.
|
||||
|
||||
# conf2struct: generate libconf parsers that read to structs
|
||||
# Copyright (C) 2018-2021 Yves Rutschle
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef C2S_ECHOCFG_H
|
||||
#define C2S_ECHOCFG_H
|
||||
#ifdef LIBCONFIG
|
||||
# include <libconfig.h>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
struct echocfg_listen_item {
|
||||
char* host;
|
||||
char* port;
|
||||
};
|
||||
|
||||
struct echocfg_item {
|
||||
int udp;
|
||||
char* prefix;
|
||||
size_t listen_len;
|
||||
struct echocfg_listen_item* listen;
|
||||
};
|
||||
|
||||
int echocfg_parse_file(
|
||||
const char* filename,
|
||||
struct echocfg_item* echocfg,
|
||||
const char** errmsg);
|
||||
|
||||
void echocfg_fprint(
|
||||
FILE* out,
|
||||
struct echocfg_item *echocfg,
|
||||
int depth);
|
||||
|
||||
int echocfg_cl_parse(
|
||||
int argc,
|
||||
char* argv[],
|
||||
struct echocfg_item *echocfg);
|
||||
|
||||
#endif
|
||||
@@ -27,8 +27,13 @@
|
||||
#include <syslog.h>
|
||||
#include <libgen.h>
|
||||
#include <getopt.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define cfg sslhcfg
|
||||
#include "common.h"
|
||||
#undef cfg
|
||||
|
||||
#include "echosrv-conf.h"
|
||||
|
||||
/* Added to make the code compilable under CYGWIN
|
||||
* */
|
||||
@@ -36,58 +41,21 @@
|
||||
#define SA_NOCLDWAIT 0
|
||||
#endif
|
||||
|
||||
const char* USAGE_STRING =
|
||||
"echosrv\n" \
|
||||
"usage:\n" \
|
||||
"\techosrv [-v] --listen <address:port> [--prefix <prefix>]\n"
|
||||
"-v: verbose\n" \
|
||||
"--listen: address to listen on. Can be specified multiple times.\n" \
|
||||
"--prefix: add specified prefix before every line echoed.\n"
|
||||
"";
|
||||
struct echocfg_item cfg;
|
||||
|
||||
const char* server_type = "echsrv"; /* keep setup_syslog happy */
|
||||
|
||||
/*
|
||||
* Settings that depend on the command line.
|
||||
*/
|
||||
char* prefix = "";
|
||||
int port;
|
||||
|
||||
void parse_cmdline(int argc, char* argv[])
|
||||
void check_res_dump(int res, struct addrinfo *addr, char* syscall)
|
||||
{
|
||||
int c;
|
||||
struct option options[] = {
|
||||
{ "verbose", no_argument, &verbose, 1 },
|
||||
{ "numeric", no_argument, &numeric, 1 },
|
||||
{ "listen", required_argument, 0, 'l' },
|
||||
{ "prefix", required_argument, 0, 'p' },
|
||||
};
|
||||
struct addrinfo **a;
|
||||
char buf[NI_MAXHOST];
|
||||
|
||||
while ((c = getopt_long_only(argc, argv, "l:p:", options, NULL)) != -1) {
|
||||
if (c == 0) continue;
|
||||
if (res == -1) {
|
||||
if (addr)
|
||||
fprintf(stderr, "error %s:%s: %s\n",
|
||||
sprintaddr(buf, sizeof(buf), addr),
|
||||
syscall,
|
||||
strerror(errno));
|
||||
else
|
||||
fprintf(stderr, "Dying just because\n");
|
||||
|
||||
switch (c) {
|
||||
|
||||
case 'l':
|
||||
/* find the end of the listen list */
|
||||
for (a = &addr_listen; *a; a = &((*a)->ai_next));
|
||||
/* append the specified addresses */
|
||||
resolve_name(a, optarg);
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
prefix = optarg;
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "%s", USAGE_STRING);
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
if (!addr_listen) {
|
||||
fprintf(stderr, "No listening port specified\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -97,19 +65,28 @@ void start_echo(int fd)
|
||||
int res;
|
||||
char buffer[1 << 20];
|
||||
int ret, prefix_len;
|
||||
int first = 1;
|
||||
|
||||
prefix_len = strlen(prefix);
|
||||
prefix_len = strlen(cfg.prefix);
|
||||
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
strcpy(buffer, prefix);
|
||||
strcpy(buffer, cfg.prefix);
|
||||
|
||||
while (1) {
|
||||
ret = read(fd, buffer + prefix_len, sizeof(buffer));
|
||||
if (ret == -1) {
|
||||
ret = read(fd, buffer + prefix_len, sizeof(buffer) - prefix_len);
|
||||
if (ret <= 0) {
|
||||
fprintf(stderr, "%s", strerror(errno));
|
||||
return;
|
||||
}
|
||||
res = write(fd, buffer, ret + prefix_len);
|
||||
if (first) {
|
||||
res = write(fd, buffer, ret + prefix_len);
|
||||
first = 0;
|
||||
if (write(1, buffer, ret + prefix_len) < 0) {
|
||||
fprintf(stderr, "%s", strerror(errno));
|
||||
}
|
||||
} else {
|
||||
res = write(fd, buffer + prefix_len, ret);
|
||||
}
|
||||
if (res < 0) {
|
||||
fprintf(stderr, "%s", strerror(errno));
|
||||
return;
|
||||
@@ -117,30 +94,228 @@ void start_echo(int fd)
|
||||
}
|
||||
}
|
||||
|
||||
void main_loop(int listen_sockets[], int num_addr_listen)
|
||||
/* TCP echo server: accepts connections to an endpoint, forks an echo for each
|
||||
* connection, forever. Prefix is added at start of response stream */
|
||||
void tcp_echo(struct listen_endpoint* listen_socket)
|
||||
{
|
||||
int in_socket, i;
|
||||
while (1) {
|
||||
int in_socket = accept(listen_socket->socketfd, 0, 0);
|
||||
CHECK_RES_DIE(in_socket, "accept");
|
||||
|
||||
if (!fork())
|
||||
{
|
||||
close(listen_socket->socketfd);
|
||||
start_echo(in_socket);
|
||||
exit(0);
|
||||
}
|
||||
close(in_socket);
|
||||
}
|
||||
}
|
||||
|
||||
void print_udp_xchange(int sockfd, struct sockaddr* addr, socklen_t addrlen)
|
||||
{
|
||||
struct addrinfo src_addrinfo, to_addrinfo;
|
||||
char str_addr[NI_MAXHOST+1+NI_MAXSERV+1];
|
||||
char str_addr2[NI_MAXHOST+1+NI_MAXSERV+1];
|
||||
struct sockaddr_storage ss;
|
||||
|
||||
src_addrinfo.ai_addr = (struct sockaddr*)&ss;
|
||||
src_addrinfo.ai_addrlen = sizeof(ss);
|
||||
getsockname(sockfd, src_addrinfo.ai_addr, &src_addrinfo.ai_addrlen);
|
||||
|
||||
to_addrinfo.ai_addr = addr;
|
||||
to_addrinfo.ai_addrlen = sizeof(*addr);
|
||||
|
||||
fprintf(stderr, "UDP local %s remote %s\n",
|
||||
sprintaddr(str_addr, sizeof(str_addr), &src_addrinfo),
|
||||
sprintaddr(str_addr2, sizeof(str_addr2), &to_addrinfo)
|
||||
);
|
||||
}
|
||||
|
||||
/* UDP echo server: receive packets, return them, forever.
|
||||
* Prefix is added at each packet */
|
||||
void udp_echo(struct listen_endpoint* listen_socket)
|
||||
{
|
||||
char data[65536];
|
||||
struct sockaddr src_addr;
|
||||
socklen_t addrlen;
|
||||
|
||||
memset(data, 0, sizeof(data));
|
||||
|
||||
size_t prefix_len = strlen(cfg.prefix);
|
||||
memcpy(data, cfg.prefix, prefix_len);
|
||||
|
||||
while (1) {
|
||||
addrlen = sizeof(src_addr);
|
||||
size_t len = recvfrom(listen_socket->socketfd,
|
||||
data + prefix_len,
|
||||
sizeof(data) - prefix_len,
|
||||
0,
|
||||
&src_addr,
|
||||
&addrlen);
|
||||
|
||||
if (len < 0) {
|
||||
perror("recvfrom");
|
||||
}
|
||||
*(data + prefix_len + len) = 0;
|
||||
fprintf(stderr, "%ld: %s\n", len, data + prefix_len);
|
||||
|
||||
print_udp_xchange(listen_socket->socketfd, &src_addr, addrlen);
|
||||
|
||||
int res = sendto(listen_socket->socketfd,
|
||||
data,
|
||||
len + prefix_len,
|
||||
0,
|
||||
&src_addr,
|
||||
addrlen);
|
||||
if (res < 0) {
|
||||
perror("sendto");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main_loop(struct listen_endpoint listen_sockets[], int num_addr_listen)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < num_addr_listen; i++) {
|
||||
if (!fork()) {
|
||||
while (1)
|
||||
{
|
||||
in_socket = accept(listen_sockets[i], 0, 0);
|
||||
if (verbose) fprintf(stderr, "accepted fd %d\n", in_socket);
|
||||
|
||||
if (!fork())
|
||||
{
|
||||
close(listen_sockets[i]);
|
||||
start_echo(in_socket);
|
||||
exit(0);
|
||||
}
|
||||
close(in_socket);
|
||||
if (cfg.udp) {
|
||||
udp_echo(&listen_sockets[i]);
|
||||
} else {
|
||||
tcp_echo(&listen_sockets[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
wait(NULL);
|
||||
}
|
||||
|
||||
/* Following is a number of utility functions copied from common.c: linking
|
||||
* against common.o directly means echosrv has to work with sslh config struct,
|
||||
* which makes it all too awkward */
|
||||
|
||||
/* simplified from common.c */
|
||||
char* sprintaddr(char* buf, size_t size, struct addrinfo *a)
|
||||
{
|
||||
char host[NI_MAXHOST], serv[NI_MAXSERV];
|
||||
int res;
|
||||
|
||||
res = getnameinfo(a->ai_addr, a->ai_addrlen,
|
||||
host, sizeof(host),
|
||||
serv, sizeof(serv),
|
||||
0 );
|
||||
|
||||
if (res) {
|
||||
/* Name resolution failed: do it numerically instead */
|
||||
res = getnameinfo(a->ai_addr, a->ai_addrlen,
|
||||
host, sizeof(host),
|
||||
serv, sizeof(serv),
|
||||
NI_NUMERICHOST | NI_NUMERICSERV);
|
||||
/* should not fail but... */
|
||||
if (res) {
|
||||
strcpy(host, "?");
|
||||
strcpy(serv, "?");
|
||||
}
|
||||
}
|
||||
|
||||
snprintf(buf, size, "%s:%s", host, serv);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
/* simplified from common.c */
|
||||
int listen_single_addr(struct addrinfo* addr, int keepalive, int udp)
|
||||
{
|
||||
struct sockaddr_storage *saddr;
|
||||
int sockfd, one, res;
|
||||
|
||||
saddr = (struct sockaddr_storage*)addr->ai_addr;
|
||||
|
||||
sockfd = socket(saddr->ss_family, udp ? SOCK_DGRAM : SOCK_STREAM, 0);
|
||||
check_res_dump(sockfd, addr, "socket");
|
||||
|
||||
one = 1;
|
||||
res = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one));
|
||||
check_res_dump(res, addr, "setsockopt(SO_REUSEADDR)");
|
||||
|
||||
if (addr->ai_addr->sa_family == AF_INET6) {
|
||||
res = setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&one, sizeof(one));
|
||||
check_res_dump(res, addr, "setsockopt(IPV6_V6ONLY)");
|
||||
}
|
||||
|
||||
res = bind(sockfd, addr->ai_addr, addr->ai_addrlen);
|
||||
check_res_dump(res, addr, "bind");
|
||||
|
||||
if (!udp) {
|
||||
res = listen (sockfd, 50);
|
||||
check_res_dump(res, addr, "listen");
|
||||
}
|
||||
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
/* simplified from common.c */
|
||||
int resolve_split_name(struct addrinfo **out, char* host, char* serv)
|
||||
{
|
||||
struct addrinfo hint;
|
||||
char *end;
|
||||
int res;
|
||||
|
||||
memset(&hint, 0, sizeof(hint));
|
||||
hint.ai_family = PF_UNSPEC;
|
||||
hint.ai_socktype = SOCK_STREAM;
|
||||
|
||||
/* If it is a RFC-Compliant IPv6 address ("[1234::12]:443"), remove brackets
|
||||
* around IP address */
|
||||
if (host[0] == '[') {
|
||||
end = strrchr(host, ']');
|
||||
if (!end) {
|
||||
fprintf(stderr, "%s: no closing bracket in IPv6 address?\n", host);
|
||||
return -1;
|
||||
}
|
||||
host++; /* skip first bracket */
|
||||
*end = 0; /* remove last bracket */
|
||||
}
|
||||
|
||||
res = getaddrinfo(host, serv, &hint, out);
|
||||
|
||||
if (res)
|
||||
fprintf(stderr, "%s `%s:%s'\n", gai_strerror(res), host, serv);
|
||||
return res;
|
||||
}
|
||||
|
||||
int start_listen_sockets(struct listen_endpoint *sockfd[])
|
||||
{
|
||||
struct addrinfo *addr, *start_addr;
|
||||
char buf[NI_MAXHOST];
|
||||
int i, res;
|
||||
int num_addr = 0, keepalive = 0, udp = 0;
|
||||
|
||||
*sockfd = NULL;
|
||||
|
||||
fprintf(stderr, "Listening to:\n");
|
||||
|
||||
for (i = 0; i < cfg.listen_len; i++) {
|
||||
udp = cfg.udp;
|
||||
|
||||
|
||||
res = resolve_split_name(&start_addr, cfg.listen[i].host, cfg.listen[i].port);
|
||||
if (res) exit(4);
|
||||
|
||||
for (addr = start_addr; addr; addr = addr->ai_next) {
|
||||
num_addr++;
|
||||
*sockfd = realloc(*sockfd, num_addr * sizeof(*sockfd));
|
||||
(*sockfd)[num_addr-1].socketfd = listen_single_addr(addr, keepalive, udp);
|
||||
(*sockfd)[num_addr-1].type = udp ? SOCK_DGRAM : SOCK_STREAM;
|
||||
fprintf(stderr, "%d:\t%s\n", (*sockfd)[num_addr-1].socketfd, sprintaddr(buf, sizeof(buf), addr));
|
||||
}
|
||||
freeaddrinfo(start_addr);
|
||||
}
|
||||
|
||||
return num_addr;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
@@ -148,11 +323,15 @@ int main(int argc, char *argv[])
|
||||
extern int optind;
|
||||
int num_addr_listen;
|
||||
|
||||
int *listen_sockets;
|
||||
struct listen_endpoint *listen_sockets;
|
||||
|
||||
parse_cmdline(argc, argv);
|
||||
memset(&cfg, 0, sizeof(cfg));
|
||||
if (echocfg_cl_parse(argc, argv, &cfg))
|
||||
exit(1);
|
||||
|
||||
num_addr_listen = start_listen_sockets(&listen_sockets, addr_listen);
|
||||
echocfg_fprint(stdout, &cfg, 0);
|
||||
|
||||
num_addr_listen = start_listen_sockets(&listen_sockets);
|
||||
|
||||
main_loop(listen_sockets, num_addr_listen);
|
||||
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
# conf2struct for echosrv
|
||||
|
||||
header: "echosrv-conf.h";
|
||||
parser: "echosrv-conf.c";
|
||||
|
||||
printer: true;
|
||||
|
||||
conffile_option: ("F", "config");
|
||||
|
||||
config: {
|
||||
name: "echocfg",
|
||||
type: "list",
|
||||
items: (
|
||||
{name: "udp", type: "bool"; default: false; },
|
||||
{name: "prefix", type: "string"; },
|
||||
{ name: "listen",
|
||||
type: "list",
|
||||
items: (
|
||||
{ name: "host"; type: "string"; var: true; },
|
||||
{ name: "port"; type: "string"; var: true; }
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
cl_groups: (
|
||||
{ name: "listen"; pattern: "(.+):(\w+)"; description: "Listen on host:port";
|
||||
short: "p"; argdesc: "<host:port>";
|
||||
list: "listen";
|
||||
# no override, this just adds to the list (and thus can be specified several times)
|
||||
targets: (
|
||||
{ path: "host"; value: "$1" },
|
||||
{ path: "port"; value: "$2" }
|
||||
);
|
||||
}
|
||||
)
|
||||
+32
-13
@@ -3,7 +3,7 @@
|
||||
# not be used as a starting point for a working
|
||||
# configuration. Instead use basic.cfg.
|
||||
|
||||
verbose: true;
|
||||
verbose: 0;
|
||||
foreground: true;
|
||||
inetd: false;
|
||||
numeric: false;
|
||||
@@ -17,6 +17,7 @@ chroot: "/var/empty";
|
||||
# system are usually defined in /usr/include/*/sys/syslog.h
|
||||
# or equivalent)
|
||||
# Default is "auth"
|
||||
# "none" disables use of syslog
|
||||
syslog_facility: "auth";
|
||||
|
||||
# List of interfaces on which we should listen
|
||||
@@ -24,7 +25,8 @@ syslog_facility: "auth";
|
||||
listen:
|
||||
(
|
||||
{ host: "thelonious"; port: "443"; },
|
||||
{ host: "thelonious"; port: "8080"; keepalive: true; }
|
||||
{ host: "thelonious"; port: "8080"; keepalive: true; },
|
||||
{ host: "thelonious"; is_udp: true; port: "443" }
|
||||
);
|
||||
|
||||
# List of protocols
|
||||
@@ -41,6 +43,10 @@ listen:
|
||||
# connection (default is off)
|
||||
# fork: Should a new process be forked for this protocol?
|
||||
# (only useful for sslh-select)
|
||||
# tfo_ok: Set to true if the server supports TCP FAST OPEN
|
||||
# transparent: Set to true to proxy this protocol
|
||||
# transparently (server sees the remote client IP
|
||||
# address). Same as the global option, but per-protocol
|
||||
#
|
||||
# Probe-specific options:
|
||||
# (sslh will try each probe in order they are declared, and
|
||||
@@ -66,33 +72,46 @@ listen:
|
||||
|
||||
protocols:
|
||||
(
|
||||
{ name: "ssh"; service: "ssh"; host: "localhost"; port: "22"; keepalive: true; fork: true; },
|
||||
{ name: "ssh"; service: "ssh"; host: "localhost"; port: "22";
|
||||
keepalive: true; fork: true; tfo_ok: true },
|
||||
{ name: "http"; host: "localhost"; port: "80"; },
|
||||
|
||||
# match BOTH ALPN/SNI
|
||||
{ name: "tls"; host: "localhost"; port: "5223"; alpn_protocols: [ "xmpp-client" ]; sni_hostnames: [ "im.somethingelse.net" ]; log_level: 0;},
|
||||
{ name: "tls"; host: "localhost"; port: "5223"; alpn_protocols: [ "xmpp-client" ]; sni_hostnames: [ "im.somethingelse.net" ]; log_level: 0; tfo_ok: true },
|
||||
|
||||
# just match ALPN
|
||||
{ name: "tls"; host: "localhost"; port: "443"; alpn_protocols: [ "h2", "http/1.1", "spdy/1", "spdy/2", "spdy/3" ]; log_level: 0; },
|
||||
{ name: "tls"; host: "localhost"; port: "xmpp-client"; alpn_protocols: [ "xmpp-client" ]; log_level: 0;},
|
||||
{ name: "tls"; host: "localhost"; port: "443"; alpn_protocols: [ "h2", "http/1.1", "spdy/1", "spdy/2", "spdy/3" ]; log_level: 0; tfo_ok: true },
|
||||
{ name: "tls"; host: "localhost"; port: "xmpp-client"; alpn_protocols: [ "xmpp-client" ]; log_level: 0; tfo_ok: true },
|
||||
|
||||
# just match SNI
|
||||
{ name: "tls"; host: "localhost"; port: "993"; sni_hostnames: [ "mail.rutschle.net", "mail.englishintoulouse.com" ]; log_level: 0; },
|
||||
{ name: "tls"; host: "localhost"; port: "xmpp-client"; sni_hostnames: [ "im.rutschle.net", "im.englishintoulouse.com" ]; log_level: 0;},
|
||||
{ name: "tls"; host: "localhost"; port: "993"; sni_hostnames: [ "mail.rutschle.net", "mail.englishintoulouse.com" ]; log_level: 0; tfo_ok: true },
|
||||
{ name: "tls"; host: "localhost"; port: "xmpp-client"; sni_hostnames: [ "im.rutschle.net", "im.englishintoulouse.com" ]; log_level: 0; tfo_ok: true },
|
||||
|
||||
# Let's Encrypt (tls-sni-* challenges)
|
||||
{ name: "tls"; host: "localhost"; port: "letsencrypt-client"; sni_hostnames: [ "*.*.acme.invalid" ]; log_level: 0;},
|
||||
# Let's Encrypt (tls-alpn-* challenges)
|
||||
{ name: "tls"; host: "localhost"; port: "letsencrypt-client"; alpn_protocols: [ "acme-tls/1" ]; log_level: 0;},
|
||||
|
||||
# catch anything else TLS
|
||||
{ name: "tls"; host: "localhost"; port: "443"; },
|
||||
{ name: "tls"; host: "localhost"; port: "443"; tfo_ok: true },
|
||||
|
||||
# Forward UDP
|
||||
{ name: "regex"; host: "localhost"; is_udp: true; port: "123";
|
||||
udp_timeout: 20; # Time after which the "connection" is forgotten
|
||||
regex_patterns: [ "hello" ]; },
|
||||
# Forward Teamspeak3 (Voice only)
|
||||
{ name: "regex"; host: "localhost"; is_udp: true; port: "9987"; regex_patterns: [ "TS3INIT1" ]; },
|
||||
# Forward IETF QUIC-50 ("Q050" -> "\x51\x30\x35\x30")
|
||||
# Remember that the regex needs to be adjusted for every supported QUIC version.
|
||||
{ name: "regex"; host: "localhost"; is_udp: true; port: "4433"; regex_patterns: [ "\x51\x30\x35\x30" ]; },
|
||||
|
||||
# Regex examples -- better use the built-in probes for real-world use!
|
||||
# OpenVPN
|
||||
{ name: "regex"; host: "localhost"; port: "1194"; regex_patterns: [ "^\x00[\x0D-\xFF]$", "^\x00[\x0D-\xFF]\x38" ]; },
|
||||
# Jabber
|
||||
{ name: "regex"; host: "localhost"; port: "5222"; regex_patterns: [ "jabber" ]; },
|
||||
{ name: "regex"; host: "localhost"; port: "5222"; regex_patterns: [ "jabber" ];
|
||||
minlength: 60; # Won't even try to match the regex if we don't have that many bytes
|
||||
},
|
||||
|
||||
# Catch-all
|
||||
# Catch-all (but better use 'anyprot')
|
||||
{ name: "regex"; host: "localhost"; port: "443"; regex_patterns: [ "" ]; },
|
||||
|
||||
# Where to connect in case of timeout (defaults to ssh)
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
gap.c: gap, a simple, dynamically-growing array
|
||||
of pointers that never shrinks
|
||||
|
||||
# Copyright (C) 2021 Yves Rutschle
|
||||
#
|
||||
# This program is free software; you can redistribute it
|
||||
# and/or modify it under the terms of the GNU General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2 of the License, or (at your option) any later
|
||||
# version.
|
||||
#
|
||||
# This program 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 General Public License for more
|
||||
# details.
|
||||
#
|
||||
# The full text for the General Public License is here:
|
||||
# http://www.gnu.org/licenses/gpl.html
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "sslh-conf.h"
|
||||
#include "gap.h"
|
||||
|
||||
|
||||
typedef struct gap_array {
|
||||
int len; /* Number of elements in array */
|
||||
void** array;
|
||||
} gap_array;
|
||||
|
||||
/* Allocate one page-worth of elements */
|
||||
static int gap_len_alloc(int elem_size)
|
||||
{
|
||||
return getpagesize() / elem_size;
|
||||
}
|
||||
|
||||
/* Creates a new gap at least `len` big, all pointers are initialised at NULL */
|
||||
gap_array* gap_init(int len)
|
||||
{
|
||||
gap_array* gap = malloc(sizeof(*gap));
|
||||
if (!gap) return NULL;
|
||||
memset(gap, 0, sizeof(*gap));
|
||||
|
||||
int elem_size = sizeof(gap->array[0]);
|
||||
gap->len = gap_len_alloc(elem_size);
|
||||
if (gap->len < len) gap->len = len;
|
||||
gap->array = malloc(gap->len * elem_size);
|
||||
if (!gap->array) return NULL;
|
||||
|
||||
for (int i = 0; i < gap->len; i++)
|
||||
gap->array[i] = NULL;
|
||||
|
||||
return gap;
|
||||
}
|
||||
|
||||
void* gap_get(gap_array* gap, int index)
|
||||
{
|
||||
return gap->array[index];
|
||||
}
|
||||
|
||||
static int gap_extend(gap_array* gap)
|
||||
{
|
||||
int elem_size = sizeof(gap->array[0]);
|
||||
int new_length = gap->len + gap_len_alloc(elem_size);
|
||||
void** new = realloc(gap->array, new_length * elem_size);
|
||||
if (!new) return -1;
|
||||
|
||||
gap->array = new;
|
||||
|
||||
for (int i = gap->len; i < new_length; i++) {
|
||||
gap->array[i] = NULL;
|
||||
}
|
||||
|
||||
gap->len = new_length;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gap_set(gap_array* gap, int index, void* ptr)
|
||||
{
|
||||
while (index >= gap->len) {
|
||||
int res = gap_extend(gap);
|
||||
if (res == -1) return -1;
|
||||
}
|
||||
|
||||
gap->array[index] = ptr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void gap_destroy(gap_array* gap)
|
||||
{
|
||||
free(gap->array);
|
||||
free(gap);
|
||||
}
|
||||
|
||||
|
||||
/* In gap, find element pointing to ptr, then shift the rest of the array that
|
||||
* is considered len elements long.
|
||||
* A poor man's list, if you will. Currently only used to remove probing
|
||||
* connections, so it only copies a few pointers at most.
|
||||
* Returns -1 if ptr was not found */
|
||||
int gap_remove_ptr(gap_array* gap, void* ptr, int len)
|
||||
{
|
||||
int start, i;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
if (gap->array[i] == ptr)
|
||||
break;
|
||||
|
||||
if (i < len)
|
||||
start = i;
|
||||
else
|
||||
return -1;
|
||||
|
||||
for (i = start; i < len; i++) {
|
||||
gap->array[i] = gap->array[i+1];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef GAP_H
|
||||
#define GAP_H
|
||||
|
||||
typedef struct gap_array gap_array;
|
||||
|
||||
gap_array* gap_init(int len);
|
||||
void* gap_get(gap_array* gap, int index);
|
||||
int gap_set(gap_array* gap, int index, void* ptr);
|
||||
void gap_destroy(gap_array* gap);
|
||||
|
||||
int gap_remove_ptr(gap_array* gap, void* ptr, int len);
|
||||
|
||||
#endif
|
||||
@@ -7,7 +7,7 @@ else
|
||||
QUIET=0
|
||||
fi
|
||||
|
||||
if ! `(git status | grep -q "On branch") 2> /dev/null`; then
|
||||
if [ ! -d .git ] || ! `(git status | grep -q "On branch") 2> /dev/null`; then
|
||||
# If we don't have git, we can't work out what
|
||||
# version this is. It must have been downloaded as a
|
||||
# zip file.
|
||||
@@ -25,7 +25,7 @@ if ! `(git status | grep -q "On branch") 2> /dev/null`; then
|
||||
fi
|
||||
fi
|
||||
|
||||
if head=`git rev-parse --verify HEAD 2>/dev/null`; then
|
||||
if [ -d .git ] && head=`git rev-parse --verify HEAD 2>/dev/null`; then
|
||||
# generate the version info based on the tag
|
||||
release=`(git describe --tags || git --describe || git describe --all --long) \
|
||||
2>/dev/null | tr -d '\n'`
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
# probe.c: Code for probing protocols
|
||||
#
|
||||
# Copyright (C) 2007-2015 Yves Rutschle
|
||||
# Copyright (C) 2007-2019 Yves Rutschle
|
||||
#
|
||||
# This program is free software; you can redistribute it
|
||||
# and/or modify it under the terms of the GNU General Public
|
||||
@@ -22,46 +22,43 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#ifdef ENABLE_REGEX
|
||||
#ifdef LIBPCRE
|
||||
#include <pcreposix.h>
|
||||
#else
|
||||
#include <regex.h>
|
||||
#endif
|
||||
#define PCRE2_CODE_UNIT_WIDTH 8
|
||||
#include <pcre2.h>
|
||||
#endif
|
||||
#include <ctype.h>
|
||||
#include "probe.h"
|
||||
|
||||
|
||||
|
||||
static int is_ssh_protocol(const char *p, int len, struct proto*);
|
||||
static int is_openvpn_protocol(const char *p, int len, struct proto*);
|
||||
static int is_tinc_protocol(const char *p, int len, struct proto*);
|
||||
static int is_xmpp_protocol(const char *p, int len, struct proto*);
|
||||
static int is_http_protocol(const char *p, int len, struct proto*);
|
||||
static int is_tls_protocol(const char *p, int len, struct proto*);
|
||||
static int is_adb_protocol(const char *p, int len, struct proto*);
|
||||
static int is_socks5_protocol(const char *p, int len, struct proto*);
|
||||
static int is_true(const char *p, int len, struct proto* proto) { return 1; }
|
||||
static int is_ssh_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item*);
|
||||
static int is_openvpn_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item*);
|
||||
static int is_tinc_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item*);
|
||||
static int is_xmpp_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item*);
|
||||
static int is_http_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item*);
|
||||
static int is_tls_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item*);
|
||||
static int is_adb_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item*);
|
||||
static int is_socks5_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item*);
|
||||
static int is_syslog_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item*);
|
||||
static int is_true(const char *p, ssize_t len, struct sslhcfg_protocols_item* proto) { return 1; }
|
||||
|
||||
/* Table of protocols that have a built-in probe
|
||||
*/
|
||||
static struct proto builtins[] = {
|
||||
/* description service saddr log_level keepalive fork probe */
|
||||
{ "ssh", "sshd", NULL, 1, 0, 1, is_ssh_protocol},
|
||||
{ "openvpn", NULL, NULL, 1, 0, 1, is_openvpn_protocol },
|
||||
{ "tinc", NULL, NULL, 1, 0, 1, is_tinc_protocol },
|
||||
{ "xmpp", NULL, NULL, 1, 0, 0, is_xmpp_protocol },
|
||||
{ "http", NULL, NULL, 1, 0, 0, is_http_protocol },
|
||||
{ "tls", NULL, NULL, 1, 0, 0, is_tls_protocol },
|
||||
{ "adb", NULL, NULL, 1, 0, 0, is_adb_protocol },
|
||||
{ "socks5", NULL, NULL, 1, 0, 0, is_socks5_protocol },
|
||||
{ "anyprot", NULL, NULL, 1, 0, 0, is_true }
|
||||
static struct protocol_probe_desc builtins[] = {
|
||||
/* description probe */
|
||||
{ "ssh", is_ssh_protocol},
|
||||
{ "openvpn", is_openvpn_protocol },
|
||||
{ "tinc", is_tinc_protocol },
|
||||
{ "xmpp", is_xmpp_protocol },
|
||||
{ "http", is_http_protocol },
|
||||
{ "tls", is_tls_protocol },
|
||||
{ "adb", is_adb_protocol },
|
||||
{ "socks5", is_socks5_protocol },
|
||||
{ "syslog", is_syslog_protocol },
|
||||
{ "anyprot", is_true }
|
||||
};
|
||||
|
||||
static struct proto *protocols;
|
||||
static char* on_timeout = "ssh";
|
||||
|
||||
struct proto* get_builtins(void) {
|
||||
/* TODO I think this has to go */
|
||||
struct protocol_probe_desc* get_builtins(void) {
|
||||
return builtins;
|
||||
}
|
||||
|
||||
@@ -69,34 +66,18 @@ int get_num_builtins(void) {
|
||||
return ARRAY_SIZE(builtins);
|
||||
}
|
||||
|
||||
/* Sets the protocol name to connect to in case of timeout */
|
||||
void set_ontimeout(const char* name)
|
||||
{
|
||||
int res = asprintf(&on_timeout, "%s", name);
|
||||
CHECK_RES_DIE(res, "asprintf");
|
||||
}
|
||||
|
||||
/* Returns the protocol to connect to in case of timeout;
|
||||
* if not found, return the first protocol specified
|
||||
*/
|
||||
struct proto* timeout_protocol(void)
|
||||
struct sslhcfg_protocols_item* timeout_protocol(void)
|
||||
{
|
||||
struct proto* p = get_first_protocol();
|
||||
for (; p && strcmp(p->description, on_timeout); p = p->next);
|
||||
if (p) return p;
|
||||
return get_first_protocol();
|
||||
int i;
|
||||
for (i = 0; i < cfg.protocols_len; i++) {
|
||||
if (!strcmp(cfg.protocols[i].name, cfg.on_timeout)) return &cfg.protocols[i];
|
||||
}
|
||||
return &cfg.protocols[0];
|
||||
}
|
||||
|
||||
/* returns the first protocol (caller can then follow the *next pointers) */
|
||||
struct proto* get_first_protocol(void)
|
||||
{
|
||||
return protocols;
|
||||
}
|
||||
|
||||
void set_protocol_list(struct proto* prots)
|
||||
{
|
||||
protocols = prots;
|
||||
}
|
||||
|
||||
/* From http://grapsus.net/blog/post/Hexadecimal-dump-in-C */
|
||||
#define HEXDUMP_COLS 16
|
||||
@@ -132,7 +113,7 @@ void hexdump(const char *mem, unsigned int len)
|
||||
}
|
||||
|
||||
/* Is the buffer the beginning of an SSH connection? */
|
||||
static int is_ssh_protocol(const char *p, int len, struct proto *proto)
|
||||
static int is_ssh_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item* proto)
|
||||
{
|
||||
if (len < 4)
|
||||
return PROBE_AGAIN;
|
||||
@@ -150,7 +131,7 @@ static int is_ssh_protocol(const char *p, int len, struct proto *proto)
|
||||
* http://www.fengnet.com/book/vpns%20illustrated%20tunnels%20%20vpnsand%20ipsec/ch08lev1sec5.html
|
||||
* and OpenVPN ssl.c, ssl.h and options.c
|
||||
*/
|
||||
static int is_openvpn_protocol (const char*p,int len, struct proto *proto)
|
||||
static int is_openvpn_protocol (const char*p,ssize_t len, struct sslhcfg_protocols_item* proto)
|
||||
{
|
||||
int packet_len;
|
||||
|
||||
@@ -165,7 +146,7 @@ static int is_openvpn_protocol (const char*p,int len, struct proto *proto)
|
||||
* Protocol is documented here: http://www.tinc-vpn.org/documentation/tinc.pdf
|
||||
* First connection starts with "0 " in 1.0.15)
|
||||
* */
|
||||
static int is_tinc_protocol( const char *p, int len, struct proto *proto)
|
||||
static int is_tinc_protocol( const char *p, ssize_t len, struct sslhcfg_protocols_item* proto)
|
||||
{
|
||||
if (len < 2)
|
||||
return PROBE_AGAIN;
|
||||
@@ -177,7 +158,7 @@ static int is_tinc_protocol( const char *p, int len, struct proto *proto)
|
||||
* (Protocol is documented (http://tools.ietf.org/html/rfc6120) but for lazy
|
||||
* clients, just checking first frame containing "jabber" in xml entity)
|
||||
* */
|
||||
static int is_xmpp_protocol( const char *p, int len, struct proto *proto)
|
||||
static int is_xmpp_protocol( const char *p, ssize_t len, struct sslhcfg_protocols_item* proto)
|
||||
{
|
||||
if (memmem(p, len, "jabber", 6))
|
||||
return PROBE_MATCH;
|
||||
@@ -200,7 +181,7 @@ static int probe_http_method(const char *p, int len, const char *opt)
|
||||
}
|
||||
|
||||
/* Is the buffer the beginning of an HTTP connection? */
|
||||
static int is_http_protocol(const char *p, int len, struct proto *proto)
|
||||
static int is_http_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item* proto)
|
||||
{
|
||||
int res;
|
||||
/* If it's got HTTP in the request (HTTP/1.1) then it's HTTP */
|
||||
@@ -226,7 +207,7 @@ static int is_http_protocol(const char *p, int len, struct proto *proto)
|
||||
}
|
||||
|
||||
/* Says if it's TLS, optionally with SNI and ALPN lists in proto->data */
|
||||
static int is_tls_protocol(const char *p, int len, struct proto *proto)
|
||||
static int is_tls_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item* proto)
|
||||
{
|
||||
switch (parse_tls_header(proto->data, p, len)) {
|
||||
case TLS_MATCH: return PROBE_MATCH;
|
||||
@@ -246,7 +227,7 @@ static int probe_adb_cnxn_message(const char *p)
|
||||
return !memcmp(&p[0], "CNXN", 4) && !memcmp(&p[24], "host:", 5);
|
||||
}
|
||||
|
||||
static int is_adb_protocol(const char *p, int len, struct proto *proto)
|
||||
static int is_adb_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item* proto)
|
||||
{
|
||||
/* amessage.data_length is not being checked, under the assumption that
|
||||
* a packet >= 30 bytes will have "something" in the payload field.
|
||||
@@ -285,7 +266,7 @@ static int is_adb_protocol(const char *p, int len, struct proto *proto)
|
||||
return probe_adb_cnxn_message(&p[sizeof(empty_message)]);
|
||||
}
|
||||
|
||||
static int is_socks5_protocol(const char *p_in, int len, struct proto *proto)
|
||||
static int is_socks5_protocol(const char *p_in, ssize_t len, struct sslhcfg_protocols_item* proto)
|
||||
{
|
||||
unsigned char* p = (unsigned char*)p_in;
|
||||
int i;
|
||||
@@ -318,16 +299,33 @@ static int is_socks5_protocol(const char *p_in, int len, struct proto *proto)
|
||||
return PROBE_MATCH;
|
||||
}
|
||||
|
||||
static int regex_probe(const char *p, int len, struct proto *proto)
|
||||
static int is_syslog_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item* proto)
|
||||
{
|
||||
int res, i, j;
|
||||
|
||||
res = sscanf(p, "<%d>", &i);
|
||||
if (res == 1) return 1;
|
||||
|
||||
res = sscanf(p, "%d <%d>", &i, &j);
|
||||
if (res == 2) return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int regex_probe(const char *p, ssize_t len, struct sslhcfg_protocols_item* proto)
|
||||
{
|
||||
#ifdef ENABLE_REGEX
|
||||
regex_t **probe = proto->data;
|
||||
regmatch_t pos = { 0, len };
|
||||
pcre2_code**probe = (pcre2_code**)proto->data;
|
||||
pcre2_match_data* matches;
|
||||
|
||||
for (; *probe && regexec(*probe, p, 0, &pos, REG_STARTEND); probe++)
|
||||
/* try them all */;
|
||||
matches = pcre2_match_data_create(1, NULL);
|
||||
|
||||
return (*probe != NULL);
|
||||
for (; *probe; probe++) {
|
||||
int res = pcre2_match(*probe, (PCRE2_SPTR8)p, len, 0, 0, matches, NULL);
|
||||
if (res >= 0) return 1;
|
||||
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
/* Should never happen as we check when loading config file */
|
||||
fprintf(stderr, "FATAL: regex probe called but not built in\n");
|
||||
@@ -335,17 +333,69 @@ static int regex_probe(const char *p, int len, struct proto *proto)
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
/* Run all the probes on a buffer
|
||||
* Returns
|
||||
* PROBE_AGAIN if not enough data, and set *proto to NULL
|
||||
* PROBE_MATCH if protocol is identified, in which case *proto is set to
|
||||
* point to the appropriate protocol
|
||||
* */
|
||||
int probe_buffer(char* buf, int len, struct sslhcfg_protocols_item** proto)
|
||||
{
|
||||
struct sslhcfg_protocols_item* p;
|
||||
int i, res, again = 0;
|
||||
|
||||
if (cfg.verbose > 1) {
|
||||
fprintf(stderr, "hexdump of incoming packet:\n");
|
||||
hexdump(buf, len);
|
||||
}
|
||||
|
||||
*proto = NULL;
|
||||
for (i = 0; i < cfg.protocols_len; i++) {
|
||||
char* probe_str[3] = {"PROBE_NEXT", "PROBE_MATCH", "PROBE_AGAIN"};
|
||||
p = &cfg.protocols[i];
|
||||
|
||||
if (! p->probe) continue;
|
||||
|
||||
if (cfg.verbose) fprintf(stderr, "probing for %s\n", p->name);
|
||||
|
||||
/* Don't probe last protocol if it is anyprot (and store last protocol) */
|
||||
if ((i == cfg.protocols_len - 1) && (!strcmp(p->name, "anyprot")))
|
||||
break;
|
||||
|
||||
if (p->minlength_is_present && (len < p->minlength )) {
|
||||
fprintf(stderr, "input too short, %d bytes but need %d\n", len , p->minlength);
|
||||
again++;
|
||||
continue;
|
||||
}
|
||||
|
||||
res = p->probe(buf, len, p);
|
||||
if (cfg.verbose) fprintf(stderr, "probed for %s: %s\n", p->name, probe_str[res]);
|
||||
|
||||
if (res == PROBE_MATCH) {
|
||||
*proto = p;
|
||||
return PROBE_MATCH;
|
||||
}
|
||||
if (res == PROBE_AGAIN)
|
||||
again++;
|
||||
}
|
||||
if (again)
|
||||
return PROBE_AGAIN;
|
||||
|
||||
/* Everything failed: match the last one */
|
||||
*proto = &cfg.protocols[cfg.protocols_len-1];
|
||||
return PROBE_MATCH;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the beginning of data coming from the client connection and check if
|
||||
* it's a known protocol.
|
||||
* it's a known protocol.
|
||||
* Return PROBE_AGAIN if not enough data, or PROBE_MATCH if it succeeded in
|
||||
* which case cnx->proto is set to the appropriate protocol.
|
||||
*/
|
||||
int probe_client_protocol(struct connection *cnx)
|
||||
{
|
||||
char buffer[BUFSIZ];
|
||||
struct proto *p, *last_p = cnx->proto;
|
||||
int n, res, again = 0;
|
||||
ssize_t n;
|
||||
|
||||
n = read(cnx->q[0].fd, buffer, sizeof(buffer));
|
||||
/* It's possible that read() returns an error, e.g. if the client
|
||||
@@ -355,63 +405,28 @@ int probe_client_protocol(struct connection *cnx)
|
||||
* connection will just fail later normally). */
|
||||
|
||||
if (n > 0) {
|
||||
if (verbose > 1) {
|
||||
fprintf(stderr, "hexdump of incoming packet:\n");
|
||||
hexdump(buffer, n);
|
||||
}
|
||||
defer_write(&cnx->q[1], buffer, n);
|
||||
return probe_buffer(cnx->q[1].begin_deferred_data,
|
||||
cnx->q[1].deferred_data_size,
|
||||
&cnx->proto);
|
||||
}
|
||||
|
||||
for (p = cnx->proto; p; p = p->next) {
|
||||
char* probe_str[3] = {"PROBE_NEXT", "PROBE_MATCH", "PROBE_AGAIN"};
|
||||
if (! p->probe) continue;
|
||||
|
||||
/* Don't probe last protocol if it is anyprot (and store last protocol) */
|
||||
if (! p->next) {
|
||||
last_p = p;
|
||||
if (!strcmp(p->description, "anyprot"))
|
||||
break;
|
||||
}
|
||||
|
||||
res = p->probe(cnx->q[1].begin_deferred_data, cnx->q[1].deferred_data_size, p);
|
||||
if (verbose) fprintf(stderr, "probing for %s: %s\n", p->description, probe_str[res]);
|
||||
|
||||
if (res == PROBE_MATCH) {
|
||||
cnx->proto = p;
|
||||
return PROBE_MATCH;
|
||||
}
|
||||
if (res == PROBE_AGAIN)
|
||||
again++;
|
||||
}
|
||||
if ((again && (n > 0)) || ((n == -1) && (errno == EAGAIN)))
|
||||
return PROBE_AGAIN;
|
||||
|
||||
/* Everything failed: match the last one */
|
||||
cnx->proto = last_p;
|
||||
/* read() returned an error, so just connect to the last protocol to die */
|
||||
cnx->proto = &cfg.protocols[cfg.protocols_len-1];
|
||||
return PROBE_MATCH;
|
||||
}
|
||||
|
||||
/* Returns the structure for specified protocol or NULL if not found */
|
||||
static struct proto* get_protocol(const char* description)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(builtins); i++) {
|
||||
if (!strcmp(builtins[i].description, description)) {
|
||||
return &builtins[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Returns the probe for specified protocol:
|
||||
* parameter is the description in builtins[], or "regex"
|
||||
* */
|
||||
T_PROBE* get_probe(const char* description) {
|
||||
struct proto* p = get_protocol(description);
|
||||
int i;
|
||||
|
||||
if (p)
|
||||
return p->probe;
|
||||
for (i = 0; i < ARRAY_SIZE(builtins); i++) {
|
||||
if (!strcmp(builtins[i].name, description)) {
|
||||
return builtins[i].probe;
|
||||
}
|
||||
}
|
||||
|
||||
/* Special case of "regex" probe (we don't want to set it in builtins
|
||||
* because builtins is also used to build the command-line options and
|
||||
|
||||
@@ -12,30 +12,19 @@ typedef enum {
|
||||
PROBE_AGAIN, /* Not enough data for this probe, try again with more data */
|
||||
} probe_result;
|
||||
|
||||
struct proto;
|
||||
typedef int T_PROBE(const char*, int, struct proto*);
|
||||
struct sslhcfg_protocols_item;
|
||||
typedef int T_PROBE(const char*, ssize_t, struct sslhcfg_protocols_item*);
|
||||
|
||||
/* For each protocol we need: */
|
||||
struct proto {
|
||||
const char* description; /* a string that says what it is (for logging and command-line parsing) */
|
||||
const char* service; /* service name to do libwrap checks */
|
||||
struct addrinfo *saddr; /* list of addresses to try and switch that protocol */
|
||||
int log_level; /* 0: No logging of connection
|
||||
* 1: Log incoming connection
|
||||
*/
|
||||
int keepalive; /* 0: No keepalive ; 1: Set Keepalive for this connection */
|
||||
int fork; /* 0: Connection can run within shared process ; 1: Separate process required for this connection */
|
||||
|
||||
/* function to probe that protocol; parameters are buffer and length
|
||||
* containing the data to probe, and a pointer to the protocol structure */
|
||||
struct protocol_probe_desc {
|
||||
const char* name;
|
||||
T_PROBE* probe;
|
||||
/* opaque pointer ; used to pass list of regex to regex probe, or TLSProtocol struct to sni/alpn probe */
|
||||
void* data;
|
||||
struct proto *next; /* pointer to next protocol in list, NULL if last */
|
||||
};
|
||||
|
||||
|
||||
#include "sslh-conf.h"
|
||||
|
||||
/* Returns a pointer to the array of builtin protocols */
|
||||
struct proto * get_builtins(void);
|
||||
struct protocol_probe_desc* get_builtins(void);
|
||||
|
||||
/* Returns the number of builtin protocols */
|
||||
int get_num_builtins(void);
|
||||
@@ -44,10 +33,10 @@ int get_num_builtins(void);
|
||||
T_PROBE* get_probe(const char* description);
|
||||
|
||||
/* Returns the head of the configured protocols */
|
||||
struct proto* get_first_protocol(void);
|
||||
struct sslhcfg_protocols_item* get_first_protocol(void);
|
||||
|
||||
/* Set the list of configured protocols */
|
||||
void set_protocol_list(struct proto*);
|
||||
void set_protocol_list(struct sslhcfg_protocols_item*);
|
||||
|
||||
/* probe_client_protocol
|
||||
*
|
||||
@@ -58,6 +47,9 @@ void set_protocol_list(struct proto*);
|
||||
*/
|
||||
int probe_client_protocol(struct connection *cnx);
|
||||
|
||||
/* Probe, but on a buffer */
|
||||
int probe_buffer(char* buf, int len, struct sslhcfg_protocols_item** proto);
|
||||
|
||||
/* set the protocol to connect to in case of timeout */
|
||||
void set_ontimeout(const char* name);
|
||||
|
||||
@@ -65,7 +57,7 @@ void set_ontimeout(const char* name);
|
||||
*
|
||||
* Returns the protocol to connect to in case of timeout
|
||||
*/
|
||||
struct proto* timeout_protocol(void);
|
||||
struct sslhcfg_protocols_item* timeout_protocol(void);
|
||||
|
||||
void hexdump(const char*, unsigned int);
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
# is needed in order to run as sslh user
|
||||
#
|
||||
#SSLH_USER=sslh
|
||||
#setcap cap_net_bind_service,cap_net_admin=+ep $SSLH
|
||||
#setcap cap_net_bind_service,cap_net_raw=+ep $SSLH
|
||||
|
||||
#
|
||||
# Configuration file for sslh
|
||||
|
||||
@@ -8,7 +8,7 @@ ExecStart=/usr/sbin/sslh --foreground $DAEMON_OPTS
|
||||
KillMode=process
|
||||
#Hardening
|
||||
PrivateTmp=true
|
||||
CapabilityBoundingSet=CAP_SETGID CAP_SETUID CAP_NET_BIND_SERVICE
|
||||
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
|
||||
AmbientCapabilities=CAP_NET_BIND_SERVICE
|
||||
SecureBits=noroot-locked
|
||||
ProtectSystem=strict
|
||||
|
||||
+2136
File diff suppressed because it is too large.
Load diff
+114
@@ -0,0 +1,114 @@
|
||||
/* Generated by conf2struct (https://www.rutschle.net/tech/conf2struct/README)
|
||||
* on Tue Aug 24 13:53:04 2021.
|
||||
|
||||
# conf2struct: generate libconf parsers that read to structs
|
||||
# Copyright (C) 2018-2021 Yves Rutschle
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef C2S_SSLHCFG_H
|
||||
#define C2S_SSLHCFG_H
|
||||
#ifdef LIBCONFIG
|
||||
# include <libconfig.h>
|
||||
#endif
|
||||
|
||||
|
||||
#include "probe.h"
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
struct sslhcfg_listen_item {
|
||||
char* host;
|
||||
char* port;
|
||||
int is_udp;
|
||||
int keepalive;
|
||||
};
|
||||
|
||||
struct sslhcfg_protocols_item {
|
||||
char* name;
|
||||
char* host;
|
||||
char* port;
|
||||
int service_is_present;
|
||||
char* service;
|
||||
int is_udp;
|
||||
int udp_timeout;
|
||||
int fork;
|
||||
int tfo_ok;
|
||||
int transparent;
|
||||
int log_level;
|
||||
int keepalive;
|
||||
size_t sni_hostnames_len;
|
||||
char** sni_hostnames;
|
||||
size_t alpn_protocols_len;
|
||||
char** alpn_protocols;
|
||||
size_t regex_patterns_len;
|
||||
char** regex_patterns;
|
||||
int minlength_is_present;
|
||||
int minlength;
|
||||
T_PROBE* probe;
|
||||
struct addrinfo* saddr;
|
||||
void* data;
|
||||
};
|
||||
|
||||
struct sslhcfg_item {
|
||||
int verbose;
|
||||
int version;
|
||||
int foreground;
|
||||
int inetd;
|
||||
int numeric;
|
||||
int transparent;
|
||||
int timeout;
|
||||
int user_is_present;
|
||||
char* user;
|
||||
int pidfile_is_present;
|
||||
char* pidfile;
|
||||
int chroot_is_present;
|
||||
char* chroot;
|
||||
char* syslog_facility;
|
||||
char* on_timeout;
|
||||
char* prefix;
|
||||
size_t listen_len;
|
||||
struct sslhcfg_listen_item* listen;
|
||||
size_t protocols_len;
|
||||
struct sslhcfg_protocols_item* protocols;
|
||||
};
|
||||
|
||||
int sslhcfg_parse_file(
|
||||
const char* filename,
|
||||
struct sslhcfg_item* sslhcfg,
|
||||
const char** errmsg);
|
||||
|
||||
void sslhcfg_fprint(
|
||||
FILE* out,
|
||||
struct sslhcfg_item *sslhcfg,
|
||||
int depth);
|
||||
|
||||
int sslhcfg_cl_parse(
|
||||
int argc,
|
||||
char* argv[],
|
||||
struct sslhcfg_item *sslhcfg);
|
||||
|
||||
#endif
|
||||
+86
-40
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
sslh-fork: forking server
|
||||
|
||||
# Copyright (C) 2007-2012 Yves Rutschle
|
||||
# Copyright (C) 2007-2021 Yves Rutschle
|
||||
#
|
||||
# This program is free software; you can redistribute it
|
||||
# and/or modify it under the terms of the GNU General Public
|
||||
@@ -22,11 +22,15 @@
|
||||
|
||||
#include "common.h"
|
||||
#include "probe.h"
|
||||
#include "sslh-conf.h"
|
||||
#include "udp-listener.h"
|
||||
|
||||
#ifdef LIBBSD
|
||||
#include <bsd/unistd.h>
|
||||
#endif
|
||||
|
||||
const char* server_type = "sslh-fork";
|
||||
|
||||
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
||||
|
||||
/* shovels data from one fd to the other and vice-versa
|
||||
returns after one socket closed
|
||||
*/
|
||||
@@ -53,8 +57,8 @@ int shovel(struct connection *cnx)
|
||||
for (i = 0; i < 2; i++) {
|
||||
if (FD_ISSET(cnx->q[i].fd, &fds)) {
|
||||
res = fd2fd(&cnx->q[1-i], &cnx->q[i]);
|
||||
if (!res) {
|
||||
if (verbose)
|
||||
if (res == FD_CNXCLOSED) {
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "%s %s", i ? "client" : "server", "socket closed\n");
|
||||
return res;
|
||||
}
|
||||
@@ -72,6 +76,7 @@ void start_shoveler(int in_socket)
|
||||
int res = PROBE_AGAIN;
|
||||
int out_socket;
|
||||
struct connection cnx;
|
||||
struct connection_desc desc;
|
||||
|
||||
init_cnx(&cnx);
|
||||
cnx.q[0].fd = in_socket;
|
||||
@@ -79,7 +84,7 @@ void start_shoveler(int in_socket)
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(in_socket, &fds);
|
||||
memset(&tv, 0, sizeof(tv));
|
||||
tv.tv_sec = probing_timeout;
|
||||
tv.tv_sec = cfg.timeout;
|
||||
|
||||
while (res == PROBE_AGAIN) {
|
||||
/* POSIX does not guarantee that tv will be updated, but the client can
|
||||
@@ -94,8 +99,8 @@ void start_shoveler(int in_socket)
|
||||
} else {
|
||||
/* Timed out: it's necessarily SSH */
|
||||
cnx.proto = timeout_protocol();
|
||||
if (verbose)
|
||||
log_message(LOG_INFO, "timed out, connect to %s\n", cnx.proto->description);
|
||||
if (cfg.verbose)
|
||||
log_message(LOG_INFO, "timed out, connect to %s\n", cnx.proto->name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -106,12 +111,16 @@ void start_shoveler(int in_socket)
|
||||
}
|
||||
|
||||
/* Connect the target socket */
|
||||
out_socket = connect_addr(&cnx, in_socket);
|
||||
out_socket = connect_addr(&cnx, in_socket, BLOCKING);
|
||||
CHECK_RES_DIE(out_socket, "connect");
|
||||
|
||||
set_capabilities(0);
|
||||
|
||||
cnx.q[1].fd = out_socket;
|
||||
|
||||
log_connection(&cnx);
|
||||
get_connection_desc(&desc, &cnx);
|
||||
log_connection(&desc, &cnx);
|
||||
set_proctitle_shovel(&desc, &cnx);
|
||||
|
||||
flush_deferred(&cnx.q[1]);
|
||||
|
||||
@@ -120,13 +129,13 @@ void start_shoveler(int in_socket)
|
||||
close(in_socket);
|
||||
close(out_socket);
|
||||
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "connection closed down\n");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
static int *listener_pid;
|
||||
static pid_t *listener_pid;
|
||||
static int listener_pid_number = 0;
|
||||
|
||||
void stop_listeners(int sig)
|
||||
@@ -138,9 +147,62 @@ void stop_listeners(int sig)
|
||||
}
|
||||
}
|
||||
|
||||
void main_loop(int listen_sockets[], int num_addr_listen)
|
||||
void set_listen_procname(struct listen_endpoint *listen_socket)
|
||||
{
|
||||
int in_socket, i, res;
|
||||
#ifdef LIBBSD
|
||||
int res;
|
||||
struct addrinfo addr;
|
||||
struct sockaddr_storage ss;
|
||||
char listen_addr[NI_MAXHOST+1+NI_MAXSERV+1];
|
||||
|
||||
addr.ai_addr = (struct sockaddr*)&ss;
|
||||
addr.ai_addrlen = sizeof(ss);
|
||||
res = getsockname(listen_socket->socketfd, addr.ai_addr, &addr.ai_addrlen);
|
||||
if (res != -1) {
|
||||
sprintaddr(listen_addr, sizeof(listen_addr), &addr);
|
||||
setproctitle("listener %s", listen_addr);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/* TCP listener: connections, fork a child for each new connection
|
||||
* IN:
|
||||
* endpoint: array of listening endpoint objects
|
||||
* num_endpoints: size of endpoint array
|
||||
* active_endpoint: which endpoint is this listener working on
|
||||
* Does not return
|
||||
* */
|
||||
void tcp_listener(struct listen_endpoint* endpoint, int num_endpoints, int active_endpoint)
|
||||
{
|
||||
int i, in_socket;
|
||||
|
||||
while (1) {
|
||||
in_socket = accept(endpoint[active_endpoint].socketfd, 0, 0);
|
||||
if (cfg.verbose) fprintf(stderr, "accepted fd %d\n", in_socket);
|
||||
|
||||
switch(fork()) {
|
||||
case -1: log_message(LOG_ERR, "fork failed: err %d: %s\n", errno, strerror(errno));
|
||||
break;
|
||||
|
||||
case 0: /* In child process */
|
||||
/* Shoveler processes don't need to hog file descriptors */
|
||||
for (i = 0; i < num_endpoints; i++)
|
||||
close(endpoint[i].socketfd);
|
||||
start_shoveler(in_socket);
|
||||
exit(0);
|
||||
|
||||
default: /* In parent process */
|
||||
break;
|
||||
}
|
||||
close(in_socket);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void main_loop(struct listen_endpoint listen_sockets[], int num_addr_listen)
|
||||
{
|
||||
int i, res;
|
||||
struct sigaction action;
|
||||
|
||||
listener_pid_number = num_addr_listen;
|
||||
@@ -151,36 +213,19 @@ void main_loop(int listen_sockets[], int num_addr_listen)
|
||||
for (i = 0; i < num_addr_listen; i++) {
|
||||
listener_pid[i] = fork();
|
||||
switch(listener_pid[i]) {
|
||||
// Log if fork() fails for some reason
|
||||
/* Log if fork() fails for some reason */
|
||||
case -1: log_message(LOG_ERR, "fork failed: err %d: %s\n", errno, strerror(errno));
|
||||
break;
|
||||
// We're in the child, we have work to do
|
||||
/* We're in the child, we have work to do */
|
||||
case 0:
|
||||
/* Listening process just accepts a connection, forks, and goes
|
||||
* back to listening */
|
||||
while (1)
|
||||
{
|
||||
in_socket = accept(listen_sockets[i], 0, 0);
|
||||
if (verbose) fprintf(stderr, "accepted fd %d\n", in_socket);
|
||||
|
||||
switch(fork()) {
|
||||
case -1: log_message(LOG_ERR, "fork failed: err %d: %s\n", errno, strerror(errno));
|
||||
break;
|
||||
|
||||
/* In child process */
|
||||
case 0:
|
||||
for (i = 0; i < num_addr_listen; ++i)
|
||||
close(listen_sockets[i]);
|
||||
start_shoveler(in_socket);
|
||||
exit(0);
|
||||
|
||||
/* In parent process */
|
||||
default: break;
|
||||
}
|
||||
close(in_socket);
|
||||
}
|
||||
set_listen_procname(&listen_sockets[i]);
|
||||
if (listen_sockets[i].type == SOCK_DGRAM)
|
||||
log_message(LOG_ERR, "UDP not (yet?) supported in sslh-fork\n");
|
||||
else
|
||||
tcp_listener(listen_sockets, num_addr_listen, i);
|
||||
break;
|
||||
// We're in the parent, we don't need to do anything
|
||||
|
||||
/* We're in the parent, we don't need to do anything */
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -194,6 +239,7 @@ void main_loop(int listen_sockets[], int num_addr_listen)
|
||||
res = sigaction(SIGTERM, &action, NULL);
|
||||
CHECK_RES_DIE(res, "sigaction");
|
||||
|
||||
|
||||
wait(NULL);
|
||||
}
|
||||
|
||||
|
||||
+101
-576
@@ -2,7 +2,7 @@
|
||||
# main: processing of config file, command line options and start the main
|
||||
# loop.
|
||||
#
|
||||
# Copyright (C) 2007-2016 Yves Rutschle
|
||||
# Copyright (C) 2007-2018 Yves Rutschle
|
||||
#
|
||||
# This program is free software; you can redistribute it
|
||||
# and/or modify it under the terms of the GNU General Public
|
||||
@@ -26,80 +26,20 @@
|
||||
#include <libconfig.h>
|
||||
#endif
|
||||
#ifdef ENABLE_REGEX
|
||||
#ifdef LIBPCRE
|
||||
#include <pcreposix.h>
|
||||
#else
|
||||
#include <regex.h>
|
||||
#define PCRE2_CODE_UNIT_WIDTH 8
|
||||
#include <pcre2.h>
|
||||
#endif
|
||||
|
||||
#ifdef LIBBSD
|
||||
#include <bsd/unistd.h>
|
||||
#endif
|
||||
|
||||
#include "common.h"
|
||||
#include "probe.h"
|
||||
|
||||
const char* USAGE_STRING =
|
||||
"sslh " VERSION "\n" \
|
||||
"usage:\n" \
|
||||
"\tsslh [-v] [-i] [-V] [-f] [-n] [--transparent] [-F<file>]\n"
|
||||
"\t[-t <timeout>] [-P <pidfile>] [-u <username>] [-C <chroot>] -p <addr> [-p <addr> ...] \n" \
|
||||
"%s\n\n" /* Dynamically built list of builtin protocols */ \
|
||||
"\t[--on-timeout <addr>]\n" \
|
||||
"-v: verbose\n" \
|
||||
"-V: version\n" \
|
||||
"-f: foreground\n" \
|
||||
"-n: numeric output\n" \
|
||||
"-u: specify under which user to run\n" \
|
||||
"-C: specify under which chroot path to run\n" \
|
||||
"--transparent: behave as a transparent proxy\n" \
|
||||
"-F: use configuration file (warning: no space between -F and file name!)\n" \
|
||||
"--on-timeout: connect to specified address upon timeout (default: ssh address)\n" \
|
||||
"-t: seconds to wait before connecting to --on-timeout address.\n" \
|
||||
"-p: address and port to listen on.\n Can be used several times to bind to several addresses.\n" \
|
||||
"--[ssh,ssl,...]: where to connect connections from corresponding protocol.\n" \
|
||||
"-P: PID file.\n" \
|
||||
"-i: Run as a inetd service.\n" \
|
||||
"";
|
||||
|
||||
/* Constants for options that have no one-character shorthand */
|
||||
#define OPT_ONTIMEOUT 257
|
||||
|
||||
static struct option const_options[] = {
|
||||
{ "inetd", no_argument, &inetd, 1 },
|
||||
{ "foreground", no_argument, &foreground, 1 },
|
||||
{ "background", no_argument, &background, 1 },
|
||||
{ "transparent", no_argument, &transparent, 1 },
|
||||
{ "numeric", no_argument, &numeric, 1 },
|
||||
{ "verbose", no_argument, &verbose, 1 },
|
||||
{ "user", required_argument, 0, 'u' },
|
||||
{ "config", optional_argument, 0, 'F' },
|
||||
{ "pidfile", required_argument, 0, 'P' },
|
||||
{ "chroot", required_argument, 0, 'C' },
|
||||
{ "timeout", required_argument, 0, 't' },
|
||||
{ "on-timeout", required_argument, 0, OPT_ONTIMEOUT },
|
||||
{ "listen", required_argument, 0, 'p' },
|
||||
{}
|
||||
};
|
||||
static struct option* all_options;
|
||||
static struct proto* builtins;
|
||||
static const char *optstr = "vt:T:p:VP:C:F::";
|
||||
|
||||
|
||||
|
||||
static void print_usage(void)
|
||||
{
|
||||
struct proto *p;
|
||||
int i;
|
||||
int res;
|
||||
char *prots = "";
|
||||
|
||||
p = get_builtins();
|
||||
for (i = 0; i < get_num_builtins(); i++) {
|
||||
res = asprintf(&prots, "%s\t[--%s <addr>]\n", prots, p[i].description);
|
||||
CHECK_RES_DIE(res, "asprintf");
|
||||
}
|
||||
|
||||
fprintf(stderr, USAGE_STRING, prots);
|
||||
}
|
||||
|
||||
static void printcaps(void) {
|
||||
#ifdef LIBCAP
|
||||
cap_t caps;
|
||||
@@ -120,567 +60,152 @@ static void printcaps(void) {
|
||||
static void printsettings(void)
|
||||
{
|
||||
char buf[NI_MAXHOST];
|
||||
struct addrinfo *a;
|
||||
struct proto *p;
|
||||
int i;
|
||||
struct sslhcfg_protocols_item *p;
|
||||
|
||||
for (p = get_first_protocol(); p; p = p->next) {
|
||||
for (i = 0; i < cfg.protocols_len; i++ ) {
|
||||
p = &cfg.protocols[i];
|
||||
fprintf(stderr,
|
||||
"%s addr: %s. libwrap service: %s log_level: %d family %d %d [%s] [%s]\n",
|
||||
p->description,
|
||||
"%s addr: %s. libwrap service: %s log_level: %d family %d %d [%s] [%s] [%s]\n",
|
||||
p->name,
|
||||
sprintaddr(buf, sizeof(buf), p->saddr),
|
||||
p->service,
|
||||
p->log_level,
|
||||
p->saddr->ai_family,
|
||||
p->saddr->ai_addr->sa_family,
|
||||
p->keepalive ? "keepalive" : "",
|
||||
p->fork ? "fork" : "");
|
||||
p->fork ? "fork" : "",
|
||||
p->transparent ? "transparent" : ""
|
||||
);
|
||||
}
|
||||
fprintf(stderr, "listening on:\n");
|
||||
for (a = addr_listen; a; a = a->ai_next) {
|
||||
fprintf(stderr,
|
||||
"\t%s\t[%s]\n",
|
||||
sprintaddr(buf, sizeof(buf), a),
|
||||
a->ai_flags & SO_KEEPALIVE ? "keepalive" : "");
|
||||
}
|
||||
fprintf(stderr, "timeout: %d\non-timeout: %s\n", probing_timeout,
|
||||
timeout_protocol()->description);
|
||||
fprintf(stderr, "timeout: %d\non-timeout: %s\n", cfg.timeout,
|
||||
timeout_protocol()->name);
|
||||
}
|
||||
|
||||
|
||||
/* To removed in v1.21 */
|
||||
const char* ssl_err_msg = "Usage of 'ssl' setting is deprecated and will be removed in v1.21. Please use 'tls' instead\n";
|
||||
void ssl_to_tls(char* setting)
|
||||
static void setup_regex_probe(struct sslhcfg_protocols_item *p)
|
||||
#ifdef ENABLE_REGEX
|
||||
{
|
||||
if (!strcmp(setting, "ssl")) {
|
||||
strcpy(setting, "tls"); /* legacy configuration */
|
||||
log_message(LOG_INFO, ssl_err_msg);
|
||||
int num_patterns, i, error;
|
||||
pcre2_code** pattern_list;
|
||||
PCRE2_SIZE error_offset;
|
||||
PCRE2_UCHAR8 err_str[120];
|
||||
|
||||
num_patterns = p->regex_patterns_len;
|
||||
|
||||
pattern_list = calloc(num_patterns + 1, sizeof(*pattern_list));
|
||||
CHECK_ALLOC(pattern_list, "calloc");
|
||||
p->data = (void*)pattern_list;
|
||||
|
||||
for (i = 0; i < num_patterns; i++) {
|
||||
pattern_list[i] = pcre2_compile((PCRE2_SPTR8)p->regex_patterns[i],
|
||||
PCRE2_ZERO_TERMINATED, 0,
|
||||
&error, &error_offset, NULL);
|
||||
if (!pattern_list[i]) {
|
||||
pcre2_get_error_message(error, err_str, sizeof(err_str));
|
||||
fprintf(stderr, "compiling pattern /%s/:%d:%s at offset %ld\n",
|
||||
p->regex_patterns[i], error, err_str, error_offset);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
{
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/* Turn 'ssl' command line option to 'tls'. To removed in v1.21 */
|
||||
void cmd_ssl_to_tls(int argc, char* argv[])
|
||||
/* For each protocol in the configuration, resolve address and set up protocol
|
||||
* options if required
|
||||
*/
|
||||
static void config_protocols()
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < argc; i++) {
|
||||
if (!strcmp(argv[i], "--ssl")) {
|
||||
strcpy(argv[i], "--tls");
|
||||
/* foreground option not parsed yet, syslog not open, just print on
|
||||
* stderr and hope for the best */
|
||||
fprintf(stderr, ssl_err_msg);
|
||||
for (i = 0; i < cfg.protocols_len; i++) {
|
||||
struct sslhcfg_protocols_item* p = &(cfg.protocols[i]);
|
||||
if (resolve_split_name(&(p->saddr), p->host, p->port)) {
|
||||
fprintf(stderr, "cannot resolve %s:%s\n", p->host, p->port);
|
||||
exit(4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Extract configuration on addresses and ports on which to listen.
|
||||
* out: newly allocated list of addrinfo to listen to
|
||||
*/
|
||||
#ifdef LIBCONFIG
|
||||
static int config_listen(config_t *config, struct addrinfo **listen)
|
||||
{
|
||||
config_setting_t *setting, *addr;
|
||||
int len, i, keepalive;
|
||||
const char *hostname, *port;
|
||||
|
||||
setting = config_lookup(config, "listen");
|
||||
if (setting) {
|
||||
len = config_setting_length(setting);
|
||||
for (i = 0; i < len; i++) {
|
||||
addr = config_setting_get_elem(setting, i);
|
||||
if (! (config_setting_lookup_string(addr, "host", &hostname) &&
|
||||
config_setting_lookup_string(addr, "port", &port))) {
|
||||
fprintf(stderr,
|
||||
"line %d:Incomplete specification (hostname and port required)\n",
|
||||
config_setting_source_line(addr));
|
||||
return -1;
|
||||
}
|
||||
|
||||
keepalive = 0;
|
||||
config_setting_lookup_bool(addr, "keepalive", &keepalive);
|
||||
|
||||
resolve_split_name(listen, hostname, port);
|
||||
|
||||
/* getaddrinfo returned a list of addresses corresponding to the
|
||||
* specification; move the pointer to the end of that list before
|
||||
* processing the next specification, while setting flags for
|
||||
* start_listen_sockets() through ai_flags (which is not meant for
|
||||
* that, but is only used as hint in getaddrinfo, so it's OK) */
|
||||
for (; *listen; listen = &((*listen)->ai_next)) {
|
||||
if (keepalive)
|
||||
(*listen)->ai_flags = SO_KEEPALIVE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef LIBCONFIG
|
||||
static void setup_regex_probe(struct proto *p, config_setting_t* probes)
|
||||
{
|
||||
#ifdef ENABLE_REGEX
|
||||
int num_probes, errsize, i, res;
|
||||
char *err;
|
||||
const char * expr;
|
||||
regex_t** probe_list;
|
||||
|
||||
num_probes = config_setting_length(probes);
|
||||
if (!num_probes) {
|
||||
fprintf(stderr, "%s: no probes specified\n", p->description);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
p->probe = get_probe("regex");
|
||||
probe_list = calloc(num_probes + 1, sizeof(*probe_list));
|
||||
CHECK_ALLOC(probe_list, "calloc");
|
||||
p->data = (void*)probe_list;
|
||||
|
||||
for (i = 0; i < num_probes; i++) {
|
||||
probe_list[i] = malloc(sizeof(*(probe_list[i])));
|
||||
CHECK_ALLOC(probe_list[i], "malloc");
|
||||
expr = config_setting_get_string_elem(probes, i);
|
||||
if (expr == NULL) {
|
||||
fprintf(stderr, "%s: invalid probe specified\n", p->description);
|
||||
p->probe = get_probe(p->name);
|
||||
if (!p->probe) {
|
||||
fprintf(stderr, "%s: probe unknown\n", p->name);
|
||||
exit(1);
|
||||
}
|
||||
res = regcomp(probe_list[i], expr, REG_EXTENDED);
|
||||
if (res) {
|
||||
err = malloc(errsize = regerror(res, probe_list[i], NULL, 0));
|
||||
CHECK_ALLOC(err, "malloc");
|
||||
regerror(res, probe_list[i], err, errsize);
|
||||
fprintf(stderr, "%s:%s\n", expr, err);
|
||||
free(err);
|
||||
exit(1);
|
||||
|
||||
if (!strcmp(cfg.protocols[i].name, "regex")) {
|
||||
setup_regex_probe(&cfg.protocols[i]);
|
||||
}
|
||||
}
|
||||
#else
|
||||
fprintf(stderr, "line %d: regex probe specified but not compiled in\n", config_setting_source_line(probes));
|
||||
exit(5);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef LIBCONFIG
|
||||
static void setup_sni_alpn_list(struct proto *p, config_setting_t* config_items, const char* name, int alpn)
|
||||
{
|
||||
int num_probes, i, max_server_name_len, server_name_len;
|
||||
const char * config_item, *server_name;
|
||||
char** sni_hostname_list;
|
||||
|
||||
num_probes = config_setting_length(config_items);
|
||||
if (!num_probes) {
|
||||
fprintf(stderr, "%s: no %s specified\n", p->description, name);
|
||||
return;
|
||||
}
|
||||
|
||||
max_server_name_len = 0;
|
||||
for (i = 0; i < num_probes; i++) {
|
||||
server_name = config_setting_get_string_elem(config_items, i);
|
||||
if (server_name == NULL) {
|
||||
fprintf(stderr, "%s: invalid %s specified\n", p->description, name);
|
||||
exit(1);
|
||||
if (!strcmp(cfg.protocols[i].name, "tls")) {
|
||||
cfg.protocols[i].data = (void*)new_tls_data();
|
||||
if (cfg.protocols[i].sni_hostnames_len)
|
||||
tls_data_set_list(cfg.protocols[i].data, 0,
|
||||
(const char**) cfg.protocols[i].sni_hostnames,
|
||||
cfg.protocols[i].sni_hostnames_len);
|
||||
if (cfg.protocols[i].alpn_protocols_len)
|
||||
tls_data_set_list(cfg.protocols[i].data, 1,
|
||||
(const char**) cfg.protocols[i].alpn_protocols,
|
||||
cfg.protocols[i].alpn_protocols_len);
|
||||
}
|
||||
server_name_len = strlen(server_name);
|
||||
if(server_name_len > max_server_name_len)
|
||||
max_server_name_len = server_name_len;
|
||||
}
|
||||
|
||||
sni_hostname_list = calloc(num_probes + 1, ++max_server_name_len);
|
||||
CHECK_ALLOC(sni_hostname_list, "calloc");
|
||||
|
||||
for (i = 0; i < num_probes; i++) {
|
||||
config_item = config_setting_get_string_elem(config_items, i);
|
||||
if (config_item == NULL) {
|
||||
fprintf(stderr, "%s: invalid %s specified\n", p->description, name);
|
||||
exit(1);
|
||||
}
|
||||
sni_hostname_list[i] = malloc(max_server_name_len);
|
||||
CHECK_ALLOC(sni_hostname_list[i], "malloc");
|
||||
strcpy (sni_hostname_list[i], config_item);
|
||||
if(verbose) fprintf(stderr, "%s: %s[%d]: %s\n", p->description, name, i, sni_hostname_list[i]);
|
||||
}
|
||||
|
||||
p->data = (void*)tls_data_set_list(p->data, alpn, sni_hostname_list);
|
||||
}
|
||||
|
||||
static void setup_sni_alpn(struct proto *p, config_setting_t* prot)
|
||||
{
|
||||
config_setting_t *sni_hostnames, *alpn_protocols;
|
||||
|
||||
p->data = (void*)new_tls_data();
|
||||
sni_hostnames = config_setting_get_member(prot, "sni_hostnames");
|
||||
alpn_protocols = config_setting_get_member(prot, "alpn_protocols");
|
||||
|
||||
if(sni_hostnames && config_setting_is_array(sni_hostnames)) {
|
||||
setup_sni_alpn_list(p, sni_hostnames, "sni_hostnames", 0);
|
||||
}
|
||||
if(alpn_protocols && config_setting_is_array(alpn_protocols)) {
|
||||
setup_sni_alpn_list(p, alpn_protocols, "alpn_protocols", 1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Extract configuration for protocols to connect to.
|
||||
* out: newly-allocated list of protocols
|
||||
*/
|
||||
#ifdef LIBCONFIG
|
||||
static int config_protocols(config_t *config, struct proto **prots)
|
||||
{
|
||||
config_setting_t *setting, *prot, *patterns;
|
||||
const char *hostname, *port, *cfg_name;
|
||||
char* name;
|
||||
int i, num_prots;
|
||||
struct proto *p, *prev = NULL;
|
||||
|
||||
setting = config_lookup(config, "protocols");
|
||||
if (setting) {
|
||||
num_prots = config_setting_length(setting);
|
||||
for (i = 0; i < num_prots; i++) {
|
||||
p = calloc(1, sizeof(*p));
|
||||
CHECK_ALLOC(p, "calloc");
|
||||
if (i == 0) *prots = p;
|
||||
if (prev) prev->next = p;
|
||||
prev = p;
|
||||
|
||||
prot = config_setting_get_elem(setting, i);
|
||||
if ((config_setting_lookup_string(prot, "name", &cfg_name) &&
|
||||
config_setting_lookup_string(prot, "host", &hostname) &&
|
||||
config_setting_lookup_string(prot, "port", &port)
|
||||
)) {
|
||||
/* To removed in v1.21 */
|
||||
name = strdup(cfg_name);
|
||||
ssl_to_tls(name);
|
||||
/* /remove */
|
||||
p->description = name;
|
||||
config_setting_lookup_string(prot, "service", &(p->service));
|
||||
config_setting_lookup_bool(prot, "keepalive", &p->keepalive);
|
||||
config_setting_lookup_bool(prot, "fork", &p->fork);
|
||||
|
||||
if (config_setting_lookup_int(prot, "log_level", &p->log_level) == CONFIG_FALSE) {
|
||||
p->log_level = 1;
|
||||
}
|
||||
|
||||
if (resolve_split_name(&(p->saddr), hostname, port)) {
|
||||
fprintf(stderr, "line %d: cannot resolve %s:%s\n", config_setting_source_line(prot), hostname, port);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
p->probe = get_probe(name);
|
||||
if (!p->probe) {
|
||||
fprintf(stderr, "line %d: %s: probe unknown\n", config_setting_source_line(prot), name);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Probe-specific options: regex patterns */
|
||||
if (!strcmp(name, "regex")) {
|
||||
patterns = config_setting_get_member(prot, "regex_patterns");
|
||||
if (patterns && config_setting_is_array(patterns)) {
|
||||
setup_regex_probe(p, patterns);
|
||||
}
|
||||
}
|
||||
|
||||
/* Probe-specific options: SNI/ALPN */
|
||||
if (!strcmp(name, "tls")) {
|
||||
setup_sni_alpn(p, prot);
|
||||
}
|
||||
|
||||
} else {
|
||||
fprintf(stderr, "line %d: Illegal protocol description (missing name, host or port)\n", config_setting_source_line(prot));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Parses a config file
|
||||
* in: *filename
|
||||
* out: *listen, a newly-allocated linked list of listen addrinfo
|
||||
* *prots, a newly-allocated linked list of protocols
|
||||
* 1 on error, 0 on success
|
||||
*/
|
||||
#ifdef LIBCONFIG
|
||||
static int config_parse(char *filename, struct addrinfo **listen, struct proto **prots)
|
||||
{
|
||||
config_t config;
|
||||
int timeout;
|
||||
const char* str;
|
||||
|
||||
config_init(&config);
|
||||
if (config_read_file(&config, filename) == CONFIG_FALSE) {
|
||||
/* If it's a parse error then there will be a line number for the failure
|
||||
* an I/O error (such as non-existent file) will have the error line as 0
|
||||
*/
|
||||
if (config_error_line(&config) != 0) {
|
||||
fprintf(stderr, "%s:%d:%s\n",
|
||||
filename,
|
||||
config_error_line(&config),
|
||||
config_error_text(&config));
|
||||
exit(1);
|
||||
}
|
||||
fprintf(stderr, "%s:%s\n",
|
||||
filename,
|
||||
config_error_text(&config));
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(config_lookup_bool(&config, "verbose", &verbose) == CONFIG_FALSE) {
|
||||
config_lookup_int(&config, "verbose", &verbose);
|
||||
}
|
||||
|
||||
config_lookup_bool(&config, "inetd", &inetd);
|
||||
config_lookup_bool(&config, "foreground", &foreground);
|
||||
config_lookup_bool(&config, "numeric", &numeric);
|
||||
config_lookup_bool(&config, "transparent", &transparent);
|
||||
|
||||
if (config_lookup_int(&config, "timeout", (int *)&timeout) == CONFIG_TRUE) {
|
||||
probing_timeout = timeout;
|
||||
}
|
||||
|
||||
if (config_lookup_string(&config, "on-timeout", &str)) {
|
||||
set_ontimeout(str);
|
||||
}
|
||||
|
||||
config_lookup_string(&config, "user", &user_name);
|
||||
config_lookup_string(&config, "pidfile", &pid_file);
|
||||
config_lookup_string(&config, "chroot", &chroot_path);
|
||||
|
||||
config_lookup_string(&config, "syslog_facility", &facility);
|
||||
|
||||
config_listen(&config, listen);
|
||||
config_protocols(&config, prots);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Adds protocols to the list of options, so command-line parsing uses the
|
||||
* protocol definition array
|
||||
* options: array of options to add to; must be big enough
|
||||
* n_opts: number of options in *options before calling (i.e. where to append)
|
||||
* prot: array of protocols
|
||||
* n_prots: number of protocols in *prot
|
||||
* */
|
||||
static void append_protocols(struct option *options, int n_opts, struct proto *prot , int n_prots)
|
||||
{
|
||||
int o, p;
|
||||
|
||||
for (o = n_opts, p = 0; p < n_prots; o++, p++) {
|
||||
options[o].name = prot[p].description;
|
||||
options[o].has_arg = required_argument;
|
||||
options[o].flag = 0;
|
||||
options[o].val = p + PROT_SHIFT;
|
||||
}
|
||||
}
|
||||
|
||||
static void make_alloptions(void)
|
||||
{
|
||||
builtins = get_builtins();
|
||||
|
||||
/* Create all_options, composed of const_options followed by one option per
|
||||
* known protocol */
|
||||
all_options = calloc(ARRAY_SIZE(const_options) + get_num_builtins(), sizeof(struct option));
|
||||
CHECK_ALLOC(all_options, "calloc");
|
||||
memcpy(all_options, const_options, sizeof(const_options));
|
||||
append_protocols(all_options, ARRAY_SIZE(const_options) - 1, builtins, get_num_builtins());
|
||||
}
|
||||
|
||||
/* Performs a first scan of command line options to see if a configuration file
|
||||
* is specified. If there is one, parse it now before all other options (so
|
||||
* configuration file settings can be overridden from the command line).
|
||||
*
|
||||
* prots: newly-allocated list of configured protocols, if any.
|
||||
*/
|
||||
static void cmdline_config(int argc, char* argv[], struct proto** prots)
|
||||
{
|
||||
#ifdef LIBCONFIG
|
||||
int c, res;
|
||||
char *config_filename;
|
||||
#endif
|
||||
|
||||
cmd_ssl_to_tls(argc, argv); /* To remove in v1.21 */
|
||||
|
||||
make_alloptions();
|
||||
|
||||
#ifdef LIBCONFIG
|
||||
optind = 1;
|
||||
opterr = 0; /* we're missing protocol options at this stage so don't output errors */
|
||||
while ((c = getopt_long_only(argc, argv, optstr, all_options, NULL)) != -1) {
|
||||
if (c == 'v') {
|
||||
verbose++;
|
||||
}
|
||||
if (c == 'F') {
|
||||
config_filename = optarg;
|
||||
if (config_filename) {
|
||||
res = config_parse(config_filename, &addr_listen, prots);
|
||||
} else {
|
||||
/* No configuration file specified -- try default file locations */
|
||||
res = config_parse("/etc/sslh/sslh.cfg", &addr_listen, prots);
|
||||
if (!res && verbose) fprintf(stderr, "Using /etc/sslh/sslh.cfg\n");
|
||||
if (res) {
|
||||
res = config_parse("/etc/sslh.cfg", &addr_listen, prots);
|
||||
if (!res && verbose) fprintf(stderr, "Using /etc/sslh.cfg\n");
|
||||
}
|
||||
}
|
||||
if (res)
|
||||
exit(4);
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/* Parse command-line options. prots points to a list of configured protocols,
|
||||
* potentially non-allocated */
|
||||
static void parse_cmdline(int argc, char* argv[], struct proto* prots)
|
||||
{
|
||||
int c;
|
||||
struct addrinfo **a;
|
||||
struct proto *p;
|
||||
|
||||
optind = 1;
|
||||
opterr = 1;
|
||||
next_arg:
|
||||
while ((c = getopt_long_only(argc, argv, optstr, all_options, NULL)) != -1) {
|
||||
if (c == 0) continue;
|
||||
|
||||
if (c >= PROT_SHIFT) {
|
||||
if (prots)
|
||||
for (p = prots; p && p->next; p = p->next) {
|
||||
/* override if protocol was already defined by config file
|
||||
* (note it only overrides address and use builtin probe) */
|
||||
if (!strcmp(p->description, builtins[c-PROT_SHIFT].description)) {
|
||||
resolve_name(&(p->saddr), optarg);
|
||||
p->probe = builtins[c-PROT_SHIFT].probe;
|
||||
goto next_arg;
|
||||
}
|
||||
}
|
||||
/* At this stage, it's a new protocol: add it to the end of the
|
||||
* list */
|
||||
if (!prots) {
|
||||
/* No protocols yet -- create the list */
|
||||
p = prots = calloc(1, sizeof(*p));
|
||||
CHECK_ALLOC(p, "calloc");
|
||||
} else {
|
||||
p->next = calloc(1, sizeof(*p));
|
||||
CHECK_ALLOC(p->next, "calloc");
|
||||
p = p->next;
|
||||
}
|
||||
memcpy(p, &builtins[c-PROT_SHIFT], sizeof(*p));
|
||||
resolve_name(&(p->saddr), optarg);
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
|
||||
case 'F':
|
||||
/* Legal option, but do nothing, it was already processed in
|
||||
* cmdline_config() */
|
||||
#ifndef LIBCONFIG
|
||||
fprintf(stderr, "Built without libconfig support: configuration file not available.\n");
|
||||
exit(1);
|
||||
#endif
|
||||
break;
|
||||
|
||||
case 't':
|
||||
probing_timeout = atoi(optarg);
|
||||
break;
|
||||
|
||||
case OPT_ONTIMEOUT:
|
||||
set_ontimeout(optarg);
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
/* find the end of the listen list */
|
||||
for (a = &addr_listen; *a; a = &((*a)->ai_next));
|
||||
/* append the specified addresses */
|
||||
resolve_name(a, optarg);
|
||||
|
||||
break;
|
||||
|
||||
case 'V':
|
||||
printf("%s %s\n", server_type, VERSION);
|
||||
exit(0);
|
||||
|
||||
case 'u':
|
||||
user_name = optarg;
|
||||
break;
|
||||
|
||||
case 'P':
|
||||
pid_file = optarg;
|
||||
break;
|
||||
|
||||
case 'C':
|
||||
chroot_path = optarg;
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
verbose++;
|
||||
break;
|
||||
|
||||
default:
|
||||
print_usage();
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
if (!prots) {
|
||||
void config_sanity_check(struct sslhcfg_item* cfg) {
|
||||
if (!cfg->protocols_len) {
|
||||
fprintf(stderr, "At least one target protocol must be specified.\n");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
set_protocol_list(prots);
|
||||
|
||||
/* If compiling with systemd socket support no need to require listen address */
|
||||
#ifndef SYSTEMD
|
||||
if (!addr_listen && !inetd) {
|
||||
if (!cfg->listen_len && !cfg->inetd) {
|
||||
fprintf(stderr, "No listening address specified; use at least one -p option\n");
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Did command-line override foreground setting? */
|
||||
if (background)
|
||||
foreground = 0;
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
||||
int main(int argc, char *argv[], char* envp[])
|
||||
{
|
||||
|
||||
extern char *optarg;
|
||||
extern int optind;
|
||||
int res, num_addr_listen;
|
||||
struct proto* protocols = NULL;
|
||||
struct listen_endpoint *listen_sockets;
|
||||
|
||||
int *listen_sockets;
|
||||
#ifdef LIBBSD
|
||||
setproctitle_init(argc, argv, envp);
|
||||
#endif
|
||||
|
||||
/* Init defaults */
|
||||
pid_file = NULL;
|
||||
user_name = NULL;
|
||||
chroot_path = NULL;
|
||||
memset(&cfg, 0, sizeof(cfg));
|
||||
res = sslhcfg_cl_parse(argc, argv, &cfg);
|
||||
if (res) exit(6);
|
||||
if (cfg.verbose > 3)
|
||||
sslhcfg_fprint(stderr, &cfg, 0);
|
||||
|
||||
cmdline_config(argc, argv, &protocols);
|
||||
parse_cmdline(argc, argv, protocols);
|
||||
if (cfg.version) {
|
||||
printf("%s %s\n", server_type, VERSION);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (inetd)
|
||||
config_protocols();
|
||||
config_sanity_check(&cfg);
|
||||
|
||||
if (cfg.inetd)
|
||||
{
|
||||
verbose = 0;
|
||||
cfg.verbose = 0;
|
||||
start_shoveler(0);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
printsettings();
|
||||
|
||||
num_addr_listen = start_listen_sockets(&listen_sockets, addr_listen);
|
||||
num_addr_listen = start_listen_sockets(&listen_sockets);
|
||||
|
||||
#ifdef SYSTEMD
|
||||
if (num_addr_listen < 1) {
|
||||
@@ -689,7 +214,7 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!foreground) {
|
||||
if (!cfg.foreground) {
|
||||
if (fork() > 0) exit(0); /* Detach */
|
||||
|
||||
/* New session -- become group leader */
|
||||
@@ -701,16 +226,16 @@ int main(int argc, char *argv[])
|
||||
|
||||
setup_signals();
|
||||
|
||||
if (pid_file)
|
||||
write_pid_file(pid_file);
|
||||
if (cfg.pidfile)
|
||||
write_pid_file(cfg.pidfile);
|
||||
|
||||
/* Open syslog connection before we drop privs/chroot */
|
||||
setup_syslog(argv[0]);
|
||||
|
||||
if (user_name || chroot_path)
|
||||
drop_privileges(user_name, chroot_path);
|
||||
if (cfg.user || cfg.chroot)
|
||||
drop_privileges(cfg.user, cfg.chroot);
|
||||
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
printcaps();
|
||||
|
||||
main_loop(listen_sockets, num_addr_listen);
|
||||
|
||||
+356
-215
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
sslh-select: mono-processus server
|
||||
|
||||
# Copyright (C) 2007-2010 Yves Rutschle
|
||||
# Copyright (C) 2007-2021 Yves Rutschle
|
||||
#
|
||||
# This program is free software; you can redistribute it
|
||||
# and/or modify it under the terms of the GNU General Public
|
||||
@@ -20,47 +20,54 @@
|
||||
|
||||
*/
|
||||
|
||||
/* Why use select(2) rather than poll(2)?
|
||||
* No real reason except that's how it was written at first. This article:
|
||||
* https://daniel.haxx.se/docs/poll-vs-select.html suggests that over a few
|
||||
* hundred file descriptors, both become very slow, so there is little
|
||||
* incentive to move to poll() to support more than FD_SETSIZE (which is 1024
|
||||
* on many Linux. To support large numbers of descriptors, either use the fork
|
||||
* version, or we'll have to write a new version based on libev. */
|
||||
|
||||
#define __LINUX__
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "probe.h"
|
||||
#include "udp-listener.h"
|
||||
#include "collection.h"
|
||||
#include "gap.h"
|
||||
|
||||
static int debug = 0;
|
||||
|
||||
const char* server_type = "sslh-select";
|
||||
|
||||
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
||||
/* Global state for a select() loop */
|
||||
struct select_info {
|
||||
int max_fd; /* Highest fd number to pass to select() */
|
||||
|
||||
/* cnx_num_alloc is the number of connection to allocate at once (at start-up,
|
||||
* and then every time we get too many simultaneous connections: e.g. start
|
||||
* with 100 slots, then if we get more than 100 connections allocate another
|
||||
* 100 slots, and so on). We never free up connection structures. We try to
|
||||
* allocate as many structures at once as will fit in one page (which is 102
|
||||
* in sslh 1.9 on Linux on x86)
|
||||
*/
|
||||
static long cnx_num_alloc;
|
||||
int num_probing; /* Number of connections currently probing
|
||||
* We use this to know if we need to time out of
|
||||
* select() */
|
||||
gap_array* probing_list; /* Pointers to cnx that are in probing mode */
|
||||
|
||||
/* Make the file descriptor non-block */
|
||||
int set_nonblock(int fd)
|
||||
{
|
||||
int flags;
|
||||
fd_set fds_r, fds_w; /* reference fd sets (used to init working copies) */
|
||||
cnx_collection* collection; /* Collection of connections linked to this loop */
|
||||
|
||||
flags = fcntl(fd, F_GETFL);
|
||||
CHECK_RES_RETURN(flags, "fcntl");
|
||||
time_t next_timeout; /* time at which next UDP connection times out */
|
||||
};
|
||||
|
||||
flags |= O_NONBLOCK;
|
||||
|
||||
flags = fcntl(fd, F_SETFL, flags);
|
||||
CHECK_RES_RETURN(flags, "fcntl");
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
int tidy_connection(struct connection *cnx, fd_set *fds, fd_set *fds2)
|
||||
static int tidy_connection(struct connection *cnx, struct select_info* fd_info)
|
||||
{
|
||||
int i;
|
||||
fd_set* fds = &fd_info->fds_r;
|
||||
fd_set* fds2 = &fd_info->fds_w;
|
||||
|
||||
for (i = 0; i < 2; i++) {
|
||||
if (cnx->q[i].fd != -1) {
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "closing fd %d\n", cnx->q[i].fd);
|
||||
|
||||
FD_CLR(cnx->q[i].fd, fds);
|
||||
@@ -70,13 +77,13 @@ int tidy_connection(struct connection *cnx, fd_set *fds, fd_set *fds2)
|
||||
free(cnx->q[i].deferred_data);
|
||||
}
|
||||
}
|
||||
init_cnx(cnx);
|
||||
collection_remove_cnx(fd_info->collection, cnx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* if fd becomes higher than FD_SETSIZE, things won't work so well with FD_SET
|
||||
* and FD_CLR. Need to drop connections if we go above that limit */
|
||||
int fd_is_in_range(int fd) {
|
||||
static int fd_is_in_range(int fd) {
|
||||
if (fd >= FD_SETSIZE) {
|
||||
log_message(LOG_ERR, "too many open file descriptor to monitor them all -- dropping connection\n");
|
||||
return 0;
|
||||
@@ -87,73 +94,56 @@ int fd_is_in_range(int fd) {
|
||||
/* Accepts a connection from the main socket and assigns it to an empty slot.
|
||||
* If no slots are available, allocate another few. If that fails, drop the
|
||||
* connexion */
|
||||
int accept_new_connection(int listen_socket, struct connection *cnx[], int* cnx_size)
|
||||
static struct connection* accept_new_connection(int listen_socket, struct cnx_collection *collection)
|
||||
{
|
||||
int in_socket, free, i, res;
|
||||
struct connection *new;
|
||||
int in_socket, res;
|
||||
|
||||
|
||||
if (cfg.verbose) fprintf(stderr, "accepting from %d\n", listen_socket);
|
||||
|
||||
in_socket = accept(listen_socket, 0, 0);
|
||||
CHECK_RES_RETURN(in_socket, "accept");
|
||||
CHECK_RES_RETURN(in_socket, "accept", NULL);
|
||||
|
||||
if (!fd_is_in_range(in_socket)) {
|
||||
close(in_socket);
|
||||
return -1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
res = set_nonblock(in_socket);
|
||||
if (res == -1) {
|
||||
close(in_socket);
|
||||
return -1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Find an empty slot */
|
||||
for (free = 0; (free < *cnx_size) && ((*cnx)[free].q[0].fd != -1); free++) {
|
||||
/* nothing */
|
||||
struct connection* cnx = collection_alloc_cnx_from_fd(collection, in_socket);
|
||||
if (!cnx) {
|
||||
close(in_socket);
|
||||
return NULL;
|
||||
}
|
||||
if (free >= *cnx_size) {
|
||||
if (verbose)
|
||||
fprintf(stderr, "buying more slots from the slot machine.\n");
|
||||
new = realloc(*cnx, (*cnx_size + cnx_num_alloc) * sizeof((*cnx)[0]));
|
||||
if (!new) {
|
||||
log_message(LOG_ERR, "unable to realloc -- dropping connection\n");
|
||||
close(in_socket);
|
||||
return -1;
|
||||
}
|
||||
*cnx = new;
|
||||
*cnx_size += cnx_num_alloc;
|
||||
for (i = free; i < *cnx_size; i++) {
|
||||
init_cnx(&(*cnx)[i]);
|
||||
}
|
||||
}
|
||||
(*cnx)[free].q[0].fd = in_socket;
|
||||
(*cnx)[free].state = ST_PROBING;
|
||||
(*cnx)[free].probe_timeout = time(NULL) + probing_timeout;
|
||||
|
||||
if (verbose)
|
||||
fprintf(stderr, "accepted fd %d on slot %d\n", in_socket, free);
|
||||
|
||||
return in_socket;
|
||||
return cnx;
|
||||
}
|
||||
|
||||
|
||||
/* Connect queue 1 of connection to SSL; returns new file descriptor */
|
||||
int connect_queue(struct connection *cnx, fd_set *fds_r, fd_set *fds_w)
|
||||
static int connect_queue(struct connection* cnx,
|
||||
struct select_info* fd_info)
|
||||
{
|
||||
struct queue *q = &cnx->q[1];
|
||||
|
||||
q->fd = connect_addr(cnx, cnx->q[0].fd);
|
||||
q->fd = connect_addr(cnx, cnx->q[0].fd, NON_BLOCKING);
|
||||
if ((q->fd != -1) && fd_is_in_range(q->fd)) {
|
||||
log_connection(cnx);
|
||||
set_nonblock(q->fd);
|
||||
log_connection(NULL, cnx);
|
||||
flush_deferred(q);
|
||||
if (q->deferred_data) {
|
||||
FD_SET(q->fd, fds_w);
|
||||
FD_CLR(cnx->q[0].fd, fds_r);
|
||||
FD_SET(q->fd, &fd_info->fds_w);
|
||||
FD_CLR(cnx->q[0].fd, &fd_info->fds_r);
|
||||
}
|
||||
FD_SET(q->fd, fds_r);
|
||||
FD_SET(q->fd, &fd_info->fds_r);
|
||||
collection_add_fd(fd_info->collection, cnx, q->fd);
|
||||
return q->fd;
|
||||
} else {
|
||||
tidy_connection(cnx, fds_r, fds_w);
|
||||
tidy_connection(cnx, fd_info);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -161,26 +151,25 @@ int connect_queue(struct connection *cnx, fd_set *fds_r, fd_set *fds_w)
|
||||
/* shovels data from active fd to the other
|
||||
returns after one socket closed or operation would block
|
||||
*/
|
||||
void shovel(struct connection *cnx, int active_fd,
|
||||
fd_set *fds_r, fd_set *fds_w)
|
||||
static void shovel(struct connection *cnx, int active_fd, struct select_info* fd_info)
|
||||
{
|
||||
struct queue *read_q, *write_q;
|
||||
|
||||
read_q = &cnx->q[active_fd];
|
||||
write_q = &cnx->q[1-active_fd];
|
||||
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "activity on fd%d\n", read_q->fd);
|
||||
|
||||
switch(fd2fd(write_q, read_q)) {
|
||||
case -1:
|
||||
case FD_CNXCLOSED:
|
||||
tidy_connection(cnx, fds_r, fds_w);
|
||||
tidy_connection(cnx, fd_info);
|
||||
break;
|
||||
|
||||
case FD_STALLED:
|
||||
FD_SET(write_q->fd, fds_w);
|
||||
FD_CLR(read_q->fd, fds_r);
|
||||
FD_SET(write_q->fd, &fd_info->fds_w);
|
||||
FD_CLR(read_q->fd, &fd_info->fds_r);
|
||||
break;
|
||||
|
||||
default: /* Nothing */
|
||||
@@ -191,7 +180,7 @@ void shovel(struct connection *cnx, int active_fd,
|
||||
/* shovels data from one fd to the other and vice-versa
|
||||
returns after one socket closed
|
||||
*/
|
||||
void shovel_single(struct connection *cnx)
|
||||
static void shovel_single(struct connection *cnx)
|
||||
{
|
||||
fd_set fds_r, fds_w;
|
||||
int res, i;
|
||||
@@ -223,7 +212,7 @@ void shovel_single(struct connection *cnx)
|
||||
if (FD_ISSET(cnx->q[i].fd, &fds_w)) {
|
||||
res = flush_deferred(&cnx->q[i]);
|
||||
if ((res == -1) && ((errno == EPIPE) || (errno == ECONNRESET))) {
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "%s socket closed\n", i ? "server" : "client");
|
||||
return;
|
||||
}
|
||||
@@ -231,7 +220,7 @@ void shovel_single(struct connection *cnx)
|
||||
if (FD_ISSET(cnx->q[i].fd, &fds_r)) {
|
||||
res = fd2fd(&cnx->q[1-i], &cnx->q[i]);
|
||||
if (!res) {
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "socket closed\n");
|
||||
return;
|
||||
}
|
||||
@@ -242,7 +231,7 @@ void shovel_single(struct connection *cnx)
|
||||
|
||||
/* Child process that makes internal connection and proxies
|
||||
*/
|
||||
void connect_proxy(struct connection *cnx)
|
||||
static void connect_proxy(struct connection *cnx)
|
||||
{
|
||||
int in_socket;
|
||||
int out_socket;
|
||||
@@ -257,29 +246,261 @@ void connect_proxy(struct connection *cnx)
|
||||
}
|
||||
|
||||
/* Connect the target socket */
|
||||
out_socket = connect_addr(cnx, in_socket);
|
||||
out_socket = connect_addr(cnx, in_socket, BLOCKING);
|
||||
CHECK_RES_DIE(out_socket, "connect");
|
||||
|
||||
cnx->q[1].fd = out_socket;
|
||||
|
||||
log_connection(cnx);
|
||||
log_connection(NULL, cnx);
|
||||
|
||||
shovel_single(cnx);
|
||||
|
||||
close(in_socket);
|
||||
close(out_socket);
|
||||
|
||||
if (verbose)
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "connection closed down\n");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/* returns true if specified fd is initialised and present in fd_set */
|
||||
int is_fd_active(int fd, fd_set* set)
|
||||
/* Removes cnx from probing list */
|
||||
static void remove_probing_cnx(struct select_info* fd_info, struct connection* cnx)
|
||||
{
|
||||
if (fd == -1) return 0;
|
||||
return FD_ISSET(fd, set);
|
||||
gap_remove_ptr(fd_info->probing_list, cnx, fd_info->num_probing);
|
||||
fd_info->num_probing--;
|
||||
}
|
||||
|
||||
static void add_probing_cnx(struct select_info* fd_info, struct connection* cnx)
|
||||
{
|
||||
gap_set(fd_info->probing_list, fd_info->num_probing, cnx);
|
||||
fd_info->num_probing++;
|
||||
}
|
||||
|
||||
|
||||
/* Process read activity on a socket in probe state
|
||||
* IN/OUT cnx: connection data, updated if connected
|
||||
* IN/OUT info: updated if connected
|
||||
* */
|
||||
|
||||
static void probing_read_process(struct connection* cnx,
|
||||
struct select_info* fd_info)
|
||||
{
|
||||
int res;
|
||||
|
||||
/* If timed out it's SSH, otherwise the client sent
|
||||
* data so probe the protocol */
|
||||
if ((cnx->probe_timeout < time(NULL))) {
|
||||
cnx->proto = timeout_protocol();
|
||||
if (cfg.verbose)
|
||||
log_message(LOG_INFO,
|
||||
"timed out, connect to %s\n",
|
||||
cnx->proto->name);
|
||||
} else {
|
||||
res = probe_client_protocol(cnx);
|
||||
if (res == PROBE_AGAIN)
|
||||
return;
|
||||
}
|
||||
|
||||
remove_probing_cnx(fd_info, cnx);
|
||||
cnx->state = ST_SHOVELING;
|
||||
|
||||
/* libwrap check if required for this protocol */
|
||||
if (cnx->proto->service &&
|
||||
check_access_rights(cnx->q[0].fd, cnx->proto->service)) {
|
||||
tidy_connection(cnx, fd_info);
|
||||
res = -1;
|
||||
} else if (cnx->proto->fork) {
|
||||
switch (fork()) {
|
||||
case 0: /* child */
|
||||
/* TODO: close all file descriptors except 2 */
|
||||
/* free(cnx); */
|
||||
connect_proxy(cnx);
|
||||
exit(0);
|
||||
case -1: log_message(LOG_ERR, "fork failed: err %d: %s\n", errno, strerror(errno));
|
||||
break;
|
||||
default: /* parent */
|
||||
break;
|
||||
}
|
||||
tidy_connection(cnx, fd_info);
|
||||
res = -1;
|
||||
} else {
|
||||
res = connect_queue(cnx, fd_info);
|
||||
}
|
||||
|
||||
if (res >= fd_info->max_fd)
|
||||
fd_info->max_fd = res + 1;;
|
||||
}
|
||||
|
||||
|
||||
/* Returns the queue index that contains the specified file descriptor */
|
||||
int active_queue(struct connection* cnx, int fd)
|
||||
{
|
||||
if (cnx->q[0].fd == fd) return 0;
|
||||
if (cnx->q[1].fd == fd) return 1;
|
||||
|
||||
log_message(LOG_ERR, "file descriptor %d not found in connection object\n", fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Process a connection that is active in read */
|
||||
static void tcp_read_process(struct select_info* fd_info,
|
||||
int fd)
|
||||
{
|
||||
if (debug) fprintf(stderr, "cnx_read_process fd %d\n", fd);
|
||||
|
||||
cnx_collection* collection = fd_info->collection;
|
||||
struct connection* cnx = collection_get_cnx_from_fd(collection, fd);
|
||||
/* Determine active queue (0 or 1): if fd is that of q[1], active_q = 1,
|
||||
* otherwise it's 0 */
|
||||
int active_q = active_queue(cnx, fd);
|
||||
|
||||
switch (cnx->state) {
|
||||
|
||||
case ST_PROBING:
|
||||
if (active_q == 1) {
|
||||
fprintf(stderr, "Activity on fd2 while probing, impossible\n");
|
||||
dump_connection(cnx);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
probing_read_process(cnx, fd_info);
|
||||
|
||||
break;
|
||||
|
||||
case ST_SHOVELING:
|
||||
shovel(cnx, active_q, fd_info);
|
||||
break;
|
||||
|
||||
default: /* illegal */
|
||||
log_message(LOG_ERR, "Illegal connection state %d\n", cnx->state);
|
||||
dump_connection(cnx);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
static void cnx_read_process(struct select_info* fd_info, int fd)
|
||||
{
|
||||
cnx_collection* collection = fd_info->collection;
|
||||
struct connection* cnx = collection_get_cnx_from_fd(collection, fd);
|
||||
switch (cnx->type) {
|
||||
case SOCK_STREAM:
|
||||
tcp_read_process(fd_info, fd);
|
||||
break;
|
||||
|
||||
case SOCK_DGRAM:
|
||||
udp_s2c_forward(cnx);
|
||||
break;
|
||||
|
||||
default:
|
||||
log_message(LOG_ERR, "cnx_read_process: Illegal connection type %d\n", cnx->type);
|
||||
dump_connection(cnx);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Process a connection that is active in write */
|
||||
static void cnx_write_process(struct select_info* fd_info, int fd)
|
||||
{
|
||||
if (debug) fprintf(stderr, "cnx_write_process fd %d\n", fd);
|
||||
|
||||
struct connection* cnx = collection_get_cnx_from_fd(fd_info->collection, fd);
|
||||
int res;
|
||||
int queue = active_queue(cnx, fd);
|
||||
|
||||
res = flush_deferred(&cnx->q[queue]);
|
||||
if ((res == -1) && ((errno == EPIPE) || (errno == ECONNRESET))) {
|
||||
if (cnx->state == ST_PROBING) remove_probing_cnx(fd_info, cnx);
|
||||
tidy_connection(cnx, fd_info);
|
||||
} else {
|
||||
/* If no deferred data is left, stop monitoring the fd
|
||||
* for write, and restart monitoring the other one for reads*/
|
||||
if (!cnx->q[queue].deferred_data_size) {
|
||||
FD_CLR(cnx->q[queue].fd, &fd_info->fds_w);
|
||||
FD_SET(cnx->q[1-queue].fd, &fd_info->fds_r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Process a connection that accepts a socket
|
||||
* (For UDP, this means all traffic coming from remote clients)
|
||||
* */
|
||||
void cnx_accept_process(struct select_info* fd_info, struct listen_endpoint* listen_socket)
|
||||
{
|
||||
int fd = listen_socket->socketfd;
|
||||
int type = listen_socket->type;
|
||||
struct connection* cnx;
|
||||
int new_fd;
|
||||
|
||||
if (debug) fprintf(stderr, "cnx_accept_process fd %d\n", fd);
|
||||
|
||||
switch (type) {
|
||||
case SOCK_STREAM:
|
||||
cnx = accept_new_connection(fd, fd_info->collection);
|
||||
|
||||
if (cnx) {
|
||||
add_probing_cnx(fd_info, cnx);
|
||||
new_fd = cnx->q[0].fd;
|
||||
}
|
||||
break;
|
||||
|
||||
case SOCK_DGRAM:
|
||||
new_fd = udp_c2s_forward(fd, fd_info->collection, fd_info->max_fd);
|
||||
fprintf(stderr, "new_fd %d\n", new_fd);
|
||||
if (new_fd == -1)
|
||||
return;
|
||||
break;
|
||||
|
||||
default:
|
||||
log_message(LOG_ERR, "Inconsistent cnx type: %d\n", type);
|
||||
exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
FD_SET(new_fd, &fd_info->fds_r);
|
||||
if (new_fd >= fd_info->max_fd)
|
||||
fd_info->max_fd = new_fd + 1;
|
||||
|
||||
}
|
||||
|
||||
/* Check all connections to see if a UDP connections has timed out, then free
|
||||
* it. At the same time, keep track of the closest, next timeout. Only do the
|
||||
* search through connections if that timeout actually happened. If the
|
||||
* connection that would have timed out has had activity, it doesn't matter: we
|
||||
* go through connections to find the next timeout, which was needed anyway. */
|
||||
static void udp_timeouts(struct select_info* fd_info)
|
||||
{
|
||||
time_t now = time(NULL);
|
||||
|
||||
if (now < fd_info->next_timeout) return;
|
||||
|
||||
time_t next_timeout = INT_MAX;
|
||||
|
||||
for (int i = 0; i < fd_info->max_fd; i++) {
|
||||
/* if it's either in read or write set, there is a connection
|
||||
* behind that file descriptor */
|
||||
if (FD_ISSET(i, &fd_info->fds_r) || FD_ISSET(i, &fd_info->fds_w)) {
|
||||
struct connection* cnx = collection_get_cnx_from_fd(fd_info->collection, i);
|
||||
if (cnx) {
|
||||
time_t timeout = udp_timeout(cnx);
|
||||
if (!timeout) continue; /* Not a UDP connection */
|
||||
if (cnx && (timeout <= now)) {
|
||||
if (cfg.verbose > 3)
|
||||
fprintf(stderr, "timed out UDP %d\n", cnx->target_sock);
|
||||
close(cnx->target_sock);
|
||||
FD_CLR(i, &fd_info->fds_r);
|
||||
FD_CLR(i, &fd_info->fds_w);
|
||||
collection_remove_cnx(fd_info->collection, cnx);
|
||||
} else {
|
||||
if (timeout < next_timeout) next_timeout = timeout;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (next_timeout != INT_MAX)
|
||||
fd_info->next_timeout = next_timeout;
|
||||
}
|
||||
|
||||
/* Main loop: the idea is as follow:
|
||||
@@ -287,7 +508,7 @@ int is_fd_active(int fd, fd_set* set)
|
||||
* - When a file descriptor goes off, process it: read from it, write the data
|
||||
* to its corresponding pair.
|
||||
* - When a file descriptor blocks when writing, remove the read fd from fds_r,
|
||||
* move the data to a deferred buffer, and add the write fd to fds_w. Defered
|
||||
* move the data to a deferred buffer, and add the write fd to fds_w. Deferred
|
||||
* buffer is allocated dynamically.
|
||||
* - When we can write to a file descriptor that has deferred data, we try to
|
||||
* write as much as we can. Once all data is written, remove the fd from fds_w
|
||||
@@ -296,166 +517,86 @@ int is_fd_active(int fd, fd_set* set)
|
||||
* That way, each pair of file descriptor (read from one, write to the other)
|
||||
* is monitored either for read or for write, but never for both.
|
||||
*/
|
||||
void main_loop(int listen_sockets[], int num_addr_listen)
|
||||
void main_loop(struct listen_endpoint listen_sockets[], int num_addr_listen)
|
||||
{
|
||||
fd_set fds_r, fds_w; /* reference fd sets (used to init the next 2) */
|
||||
struct select_info fd_info = {0};
|
||||
fd_set readfds, writefds; /* working read and write fd sets */
|
||||
struct timeval tv;
|
||||
int max_fd, i, j, res;
|
||||
int in_socket = 0;
|
||||
struct connection *cnx;
|
||||
int num_cnx; /* Number of connections in *cnx */
|
||||
int num_probing = 0; /* Number of connections currently probing
|
||||
* We use this to know if we need to time out of
|
||||
* select() */
|
||||
int i, res;
|
||||
|
||||
FD_ZERO(&fds_r);
|
||||
FD_ZERO(&fds_w);
|
||||
fd_info.num_probing = 0;
|
||||
FD_ZERO(&fd_info.fds_r);
|
||||
FD_ZERO(&fd_info.fds_w);
|
||||
fd_info.probing_list = gap_init(0);
|
||||
|
||||
for (i = 0; i < num_addr_listen; i++) {
|
||||
FD_SET(listen_sockets[i], &fds_r);
|
||||
set_nonblock(listen_sockets[i]);
|
||||
FD_SET(listen_sockets[i].socketfd, &fd_info.fds_r);
|
||||
set_nonblock(listen_sockets[i].socketfd);
|
||||
}
|
||||
max_fd = listen_sockets[num_addr_listen-1] + 1;
|
||||
fd_info.max_fd = listen_sockets[num_addr_listen-1].socketfd + 1;
|
||||
|
||||
cnx_num_alloc = getpagesize() / sizeof(struct connection);
|
||||
|
||||
num_cnx = cnx_num_alloc; /* Start with a set pool of slots */
|
||||
cnx = malloc(num_cnx * sizeof(struct connection));
|
||||
CHECK_ALLOC(cnx, "malloc");
|
||||
for (i = 0; i < num_cnx; i++)
|
||||
init_cnx(&cnx[i]);
|
||||
fd_info.collection = collection_init(fd_info.max_fd);
|
||||
|
||||
while (1)
|
||||
{
|
||||
memset(&tv, 0, sizeof(tv));
|
||||
tv.tv_sec = probing_timeout;
|
||||
tv.tv_sec = cfg.timeout;
|
||||
|
||||
memcpy(&readfds, &fds_r, sizeof(readfds));
|
||||
memcpy(&writefds, &fds_w, sizeof(writefds));
|
||||
memcpy(&readfds, &fd_info.fds_r, sizeof(readfds));
|
||||
memcpy(&writefds, &fd_info.fds_w, sizeof(writefds));
|
||||
|
||||
if (verbose)
|
||||
fprintf(stderr, "selecting... max_fd=%d num_probing=%d\n", max_fd, num_probing);
|
||||
res = select(max_fd, &readfds, &writefds, NULL, num_probing ? &tv : NULL);
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "selecting... max_fd=%d num_probing=%d\n",
|
||||
fd_info.max_fd, fd_info.num_probing);
|
||||
res = select(fd_info.max_fd, &readfds, &writefds,
|
||||
NULL, fd_info.num_probing ? &tv : NULL);
|
||||
if (res < 0)
|
||||
perror("select");
|
||||
|
||||
|
||||
/* UDP timeouts: clear out connections after some idle time */
|
||||
udp_timeouts(&fd_info);
|
||||
|
||||
/* Check main socket for new connections */
|
||||
for (i = 0; i < num_addr_listen; i++) {
|
||||
if (FD_ISSET(listen_sockets[i], &readfds)) {
|
||||
in_socket = accept_new_connection(listen_sockets[i], &cnx, &num_cnx);
|
||||
if (in_socket > 0) {
|
||||
num_probing++;
|
||||
FD_SET(in_socket, &fds_r);
|
||||
if (in_socket >= max_fd)
|
||||
max_fd = in_socket + 1;;
|
||||
}
|
||||
if (FD_ISSET(listen_sockets[i].socketfd, &readfds)) {
|
||||
cnx_accept_process(&fd_info, &listen_sockets[i]);
|
||||
|
||||
/* don't also process it as a read socket */
|
||||
FD_CLR(listen_sockets[i].socketfd, &readfds);
|
||||
}
|
||||
}
|
||||
|
||||
/* Check all sockets for write activity */
|
||||
for (i = 0; i < num_cnx; i++) {
|
||||
if (cnx[i].q[0].fd != -1) {
|
||||
for (j = 0; j < 2; j++) {
|
||||
if (is_fd_active(cnx[i].q[j].fd, &writefds)) {
|
||||
res = flush_deferred(&cnx[i].q[j]);
|
||||
if ((res == -1) && ((errno == EPIPE) || (errno == ECONNRESET))) {
|
||||
if (cnx[i].state == ST_PROBING) num_probing--;
|
||||
tidy_connection(&cnx[i], &fds_r, &fds_w);
|
||||
if (verbose)
|
||||
fprintf(stderr, "closed slot %d\n", i);
|
||||
} else {
|
||||
/* If no deferred data is left, stop monitoring the fd
|
||||
* for write, and restart monitoring the other one for reads*/
|
||||
if (!cnx[i].q[j].deferred_data_size) {
|
||||
FD_CLR(cnx[i].q[j].fd, &fds_w);
|
||||
FD_SET(cnx[i].q[1-j].fd, &fds_r);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i = 0; i < fd_info.max_fd; i++) {
|
||||
if (FD_ISSET(i, &writefds)) {
|
||||
cnx_write_process(&fd_info, i);
|
||||
}
|
||||
}
|
||||
|
||||
/* Check sockets in probing state for timeouts */
|
||||
for (i = 0; i < fd_info.num_probing; i++) {
|
||||
struct connection* cnx = gap_get(fd_info.probing_list, i);
|
||||
if (!cnx || cnx->state != ST_PROBING) {
|
||||
log_message(LOG_ERR, "Inconsistent probing: cnx=%0xp\n", cnx);
|
||||
if (cnx)
|
||||
log_message(LOG_ERR, "Inconsistent probing: state=%d\n", cnx);
|
||||
exit(1);
|
||||
}
|
||||
if (cnx->probe_timeout < time(NULL)) {
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "timeout slot %d\n", i);
|
||||
probing_read_process(cnx, &fd_info);
|
||||
}
|
||||
}
|
||||
|
||||
/* Check all sockets for read activity */
|
||||
for (i = 0; i < num_cnx; i++) {
|
||||
for (j = 0; j < 2; j++) {
|
||||
if (is_fd_active(cnx[i].q[j].fd, &readfds) ||
|
||||
((cnx[i].state == ST_PROBING) && (cnx[i].probe_timeout < time(NULL)))) {
|
||||
if (verbose)
|
||||
fprintf(stderr, "processing fd%d slot %d\n", j, i);
|
||||
|
||||
switch (cnx[i].state) {
|
||||
|
||||
case ST_PROBING:
|
||||
if (j == 1) {
|
||||
fprintf(stderr, "Activity on fd2 while probing, impossible\n");
|
||||
dump_connection(&cnx[i]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* If timed out it's SSH, otherwise the client sent
|
||||
* data so probe the protocol */
|
||||
if ((cnx[i].probe_timeout < time(NULL))) {
|
||||
cnx[i].proto = timeout_protocol();
|
||||
if (verbose)
|
||||
log_message(LOG_INFO,
|
||||
"timed out, connect to %s\n",
|
||||
cnx[i].proto->description);
|
||||
} else {
|
||||
res = probe_client_protocol(&cnx[i]);
|
||||
if (res == PROBE_AGAIN)
|
||||
continue;
|
||||
}
|
||||
|
||||
num_probing--;
|
||||
cnx[i].state = ST_SHOVELING;
|
||||
|
||||
/* libwrap check if required for this protocol */
|
||||
if (cnx[i].proto->service &&
|
||||
check_access_rights(in_socket, cnx[i].proto->service)) {
|
||||
tidy_connection(&cnx[i], &fds_r, &fds_w);
|
||||
res = -1;
|
||||
} else if (cnx[i].proto->fork) {
|
||||
struct connection *pcnx_i = &cnx[i];
|
||||
struct connection cnx_i = *pcnx_i;
|
||||
switch (fork()) {
|
||||
case 0: /* child */
|
||||
for (i = 0; i < num_addr_listen; i++)
|
||||
close(listen_sockets[i]);
|
||||
for (i = 0; i < num_cnx; i++)
|
||||
if (&cnx[i] != pcnx_i)
|
||||
for (j = 0; j < 2; j++)
|
||||
if (cnx[i].q[j].fd != -1)
|
||||
close(cnx[i].q[j].fd);
|
||||
free(cnx);
|
||||
connect_proxy(&cnx_i);
|
||||
exit(0);
|
||||
case -1: log_message(LOG_ERR, "fork failed: err %d: %s\n", errno, strerror(errno));
|
||||
break;
|
||||
default: /* parent */
|
||||
break;
|
||||
}
|
||||
tidy_connection(&cnx[i], &fds_r, &fds_w);
|
||||
res = -1;
|
||||
} else {
|
||||
res = connect_queue(&cnx[i], &fds_r, &fds_w);
|
||||
}
|
||||
|
||||
if (res >= max_fd)
|
||||
max_fd = res + 1;;
|
||||
break;
|
||||
|
||||
case ST_SHOVELING:
|
||||
shovel(&cnx[i], j, &fds_r, &fds_w);
|
||||
break;
|
||||
|
||||
default: /* illegal */
|
||||
log_message(LOG_ERR, "Illegal connection state %d\n", cnx[i].state);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
for (i = 0; i < fd_info.max_fd; i++) {
|
||||
/* Check if it's active AND currently monitored (if a connection
|
||||
* died, it gets tidied, which closes both sockets, but readfs does
|
||||
* not know about that */
|
||||
if (FD_ISSET(i, &readfds) && FD_ISSET(i, &fd_info.fds_r)) {
|
||||
cnx_read_process(&fd_info, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
sslh [B<-F>I<config file>] [B<-t> I<num>] [B<--transparent>] [B<-p> I<listening address> [B<-p> I<listening address> ...] [B<--ssl> I<target address for SSL>] [B<--tls> I<target address for TLS>] [B<--ssh> I<target address for SSH>] [B<--openvpn> I<target address for OpenVPN>] [B<--http> I<target address for HTTP>] [B<--xmpp> I<target address for XMPP>] [B<--tinc> I<target address for TINC>] [B<--anyprot> I<default target address>] [B<--on-timeout> I<protocol name>] [B<-u> I<username>] [B<-C> I<chroot>] [B<-P> I<pidfile>] [-v] [-i] [-V] [-f] [-n]
|
||||
sslh [B<-F>I<config file>] [B<-t> I<num>] [B<--transparent>] [B<-p> I<listening address> [B<-p> I<listening address> ...] [B<--tls> I<target address for TLS>] [B<--ssh> I<target address for SSH>] [B<--openvpn> I<target address for OpenVPN>] [B<--http> I<target address for HTTP>] [B<--xmpp> I<target address for XMPP>] [B<--tinc> I<target address for TINC>] [B<--anyprot> I<default target address>] [B<--on-timeout> I<protocol name>] [B<-u> I<username>] [B<-C> I<chroot>] [B<-P> I<pidfile>] [-v] [-i] [-V] [-f] [-n]
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
@@ -84,9 +84,6 @@ Uses I<filename> as configuration file. If other
|
||||
command-line options are specified, they will override the
|
||||
configuration file's settings.
|
||||
|
||||
When using the shorthand version, make sure there should be
|
||||
no space between B<-F> and the I<filename>.
|
||||
|
||||
=item B<-t> I<num>, B<--timeout> I<num>
|
||||
|
||||
Timeout before forwarding the connection to the timeout
|
||||
@@ -117,8 +114,6 @@ IP address on which the Internet connection ends up).
|
||||
This can be specified several times to bind B<sslh> to
|
||||
several addresses.
|
||||
|
||||
=item B<--ssl> I<target address>
|
||||
|
||||
=item B<--tls> I<target address>
|
||||
|
||||
Interface and port on which to forward SSL connection,
|
||||
@@ -131,7 +126,7 @@ inside your network to just connect directly to B<httpd>.
|
||||
Also, B<sslh> probes for SSLv3 (or TLSv1) handshake and will
|
||||
reject connections from clients requesting SSLv2. This is
|
||||
compliant with RFC6176 which prohibits the usage of SSLv2. If
|
||||
you wish to accept SSLv2, use B<--default> instead.
|
||||
you wish to accept SSLv2, use B<--anyprot> instead.
|
||||
|
||||
=item B<--ssh> I<target address>
|
||||
|
||||
|
||||
+248
@@ -0,0 +1,248 @@
|
||||
# This is a configuration file for the configuration
|
||||
# generator: it is used by
|
||||
# conf2struct(http://www.rutschle.net/tech/conf2struct/README.html)
|
||||
# to generate the config file and command line parser in
|
||||
# sslh-conf.c
|
||||
|
||||
header: "sslh-conf.h";
|
||||
parser: "sslh-conf.c";
|
||||
|
||||
printer: true;
|
||||
|
||||
conffile_option: ("F", "config");
|
||||
|
||||
# List of includes to define runtime types
|
||||
# (bug in libconfig? if swallows the brackets if they start
|
||||
# the string)
|
||||
includes: (
|
||||
"probe.h",
|
||||
" <sys/types.h>",
|
||||
" <sys/socket.h>",
|
||||
" <netdb.h>"
|
||||
);
|
||||
|
||||
config: {
|
||||
name : "sslhcfg",
|
||||
type: "list",
|
||||
items: (
|
||||
{ name: "verbose"; type: "int"; default: 0; short: "v"; },
|
||||
{ name: "version"; type: "bool"; default: false;
|
||||
short: "V";
|
||||
description: "Print version information and exit"; },
|
||||
{ name: "foreground"; type: "bool"; default: false;
|
||||
short: "f";
|
||||
description: "Run in foreground instead of as a daemon"; },
|
||||
{ name: "inetd"; type: "bool"; default: false;
|
||||
short: "i";
|
||||
description: "Run in inetd mode: use stdin/stdout instead of network listen"; },
|
||||
{ name: "numeric"; type: "bool"; default: false;
|
||||
short: "n";
|
||||
description: "Print IP addresses and ports as numbers"; },
|
||||
{ name: "transparent"; type: "bool"; default: false;
|
||||
description: "Set up as a transparent proxy"; },
|
||||
{ name: "timeout"; type: "int"; default: 5;
|
||||
short: "t";
|
||||
description: "Set up timeout before connecting to default target"; },
|
||||
{ name: "user"; type: "string"; optional: true;
|
||||
short: "u";
|
||||
description: "Username to change to after set-up"; },
|
||||
{ name: "pidfile"; type: "string"; optional: true;
|
||||
short: "P"; argdesc: "<file>";
|
||||
description: "Path to file to store PID of current instance"; },
|
||||
{ name: "chroot"; type: "string"; optional: true;
|
||||
short: "C"; argdesc: "<path>";
|
||||
description: "Root to change to after set-up"; },
|
||||
{ name: "syslog_facility"; type: "string"; default: "auth";
|
||||
description: "Facility to syslog to"; },
|
||||
|
||||
{ name: "on-timeout"; type: "string"; default: "ssh";
|
||||
description: "Target to connect to when timing out"; },
|
||||
|
||||
{ name: "prefix"; type: "string"; default: ""; description: "Reserved for testing" }, # For echosrv only, not sslh
|
||||
|
||||
{ name: "listen",
|
||||
no_cl_accessors: true; # disable generation of individual cl options for each group element (we create a specific --listen option further below)
|
||||
type: "list",
|
||||
items: (
|
||||
{ name: "host"; type: "string"; var: true; },
|
||||
{ name: "port"; type: "string"; var: true; },
|
||||
{ name: "is_udp"; type: "bool"; default: false },
|
||||
{ name: "keepalive"; type: "bool"; default: false; }
|
||||
)
|
||||
},
|
||||
|
||||
{ name: "protocols",
|
||||
no_cl_accessors: true;
|
||||
type: "list",
|
||||
items: (
|
||||
{ name: "name"; type: "string"; },
|
||||
{ name: "host"; type: "string"; var: true; },
|
||||
{ name: "port"; type: "string"; var: true; },
|
||||
{ name: "service"; type: "string"; optional: true; },
|
||||
{ name: "is_udp"; type: "bool"; default: false },
|
||||
{ name: "udp_timeout"; type: "int"; default: 60 },
|
||||
{ name: "fork"; type: "bool"; default: false },
|
||||
{ name: "tfo_ok"; type: "bool"; default: false;
|
||||
description: "Set to true if this protocol supports TCP FAST OPEN" },
|
||||
{ name: "transparent"; type: "bool"; default: false;
|
||||
description: "Set to proxy this protocol transparently" },
|
||||
{ name: "log_level"; type: "int"; default: 1 },
|
||||
{ name: "keepalive"; type: "bool"; default: false },
|
||||
{ name: "sni_hostnames",
|
||||
type: "array",
|
||||
element_type: "string"
|
||||
},
|
||||
{ name: "alpn_protocols",
|
||||
type: "array",
|
||||
element_type: "string"
|
||||
},
|
||||
{ name: "regex_patterns",
|
||||
type: "array",
|
||||
element_type: "string"
|
||||
},
|
||||
{ name: "minlength"; type: "int"; optional: true },
|
||||
|
||||
# Runtime data
|
||||
{ name: "probe"; type: "runtime"; c_type: "T_PROBE*" },
|
||||
{ name: "saddr"; type: "runtime"; c_type: "struct addrinfo*" },
|
||||
{ name: "data"; type: "runtime"; c_type: "void*" }
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
# Command line for list settings: additional options that
|
||||
# can set up several settings at once. Each option will
|
||||
# create a new group setting entry if required (with
|
||||
# defaults set up)
|
||||
# This only works with string targets
|
||||
# This may not be the right abstraction at all and way too
|
||||
# sslh-centric
|
||||
cl_groups: (
|
||||
{ name: "listen"; pattern: "(.+):(\w+)"; description: "Listen on host:port";
|
||||
short: "p"; argdesc: "<host:port>";
|
||||
list: "listen";
|
||||
# no override, this just adds to the list (and thus can be specified several times)
|
||||
targets: (
|
||||
{ path: "host"; value: "$1" },
|
||||
{ path: "port"; value: "$2" }
|
||||
);
|
||||
},
|
||||
{ name: "ssh"; pattern: "(.+):(\w+)"; description: "Set up ssh target";
|
||||
list: "protocols"; # List name that we're defining with this command line option
|
||||
override: "name"; # Field in the group to override. If not found in list, add an item
|
||||
# (it's mandatory to have that field as one of the targets
|
||||
# below)
|
||||
argdesc: "<host:port>";
|
||||
targets: (
|
||||
{ path: "name"; value: "ssh" },
|
||||
{ path: "host"; value: "$1" },
|
||||
{ path: "port"; value: "$2" },
|
||||
{ path: "fork"; value: 1 },
|
||||
{ path: "log_level"; value: 1 },
|
||||
{ path: "tfo_ok"; value: 1 }
|
||||
);
|
||||
},
|
||||
{ name: "tls"; pattern: "(.+):(\w+)"; description: "Set up TLS/SSL target";
|
||||
list: "protocols";
|
||||
override: "name";
|
||||
argdesc: "<host:port>";
|
||||
targets: (
|
||||
{ path: "name"; value: "tls" },
|
||||
{ path: "host"; value: "$1" },
|
||||
{ path: "port"; value: "$2" },
|
||||
{ path: "log_level"; value: 1 },
|
||||
{ path: "tfo_ok"; value: 1 }
|
||||
);
|
||||
},
|
||||
{ name: "openvpn"; pattern: "(.+):(\w+)"; description: "Set up OpenVPN target";
|
||||
list: "protocols";
|
||||
override: "name";
|
||||
argdesc: "<host:port>";
|
||||
targets: (
|
||||
{ path: "name"; value: "openvpn" },
|
||||
{ path: "host"; value: "$1" },
|
||||
{ path: "port"; value: "$2" },
|
||||
{ path: "log_level"; value: 1 },
|
||||
{ path: "tfo_ok"; value: 1 }
|
||||
);
|
||||
},
|
||||
{ name: "tinc"; pattern: "(.+):(\w+)"; description: "Set up tinc target";
|
||||
list: "protocols";
|
||||
override: "name";
|
||||
argdesc: "<host:port>";
|
||||
targets: (
|
||||
{ path: "name"; value: "tinc" },
|
||||
{ path: "host"; value: "$1" },
|
||||
{ path: "port"; value: "$2" },
|
||||
{ path: "log_level"; value: 1 },
|
||||
{ path: "tfo_ok"; value: 1 }
|
||||
);
|
||||
},
|
||||
{ name: "xmpp"; pattern: "(.+):(\w+)"; description: "Set up XMPP target";
|
||||
list: "protocols";
|
||||
override: "name";
|
||||
argdesc: "<host:port>";
|
||||
targets: (
|
||||
{ path: "name"; value: "xmpp" },
|
||||
{ path: "host"; value: "$1" },
|
||||
{ path: "port"; value: "$2" },
|
||||
{ path: "log_level"; value: 1 }
|
||||
);
|
||||
},
|
||||
{ name: "http"; pattern: "(.+):(\w+)"; description: "Set up HTTP (plain) target";
|
||||
list: "protocols";
|
||||
override: "name";
|
||||
argdesc: "<host:port>";
|
||||
targets: (
|
||||
{ path: "name"; value: "http" },
|
||||
{ path: "host"; value: "$1" },
|
||||
{ path: "port"; value: "$2" },
|
||||
{ path: "log_level"; value: 1 }
|
||||
);
|
||||
},
|
||||
{ name: "adb"; pattern: "(.+):(\w+)"; description: "Set up ADB (Android Debug) target";
|
||||
list: "protocols";
|
||||
override: "name";
|
||||
argdesc: "<host:port>";
|
||||
targets: (
|
||||
{ path: "name"; value: "adb" },
|
||||
{ path: "host"; value: "$1" },
|
||||
{ path: "port"; value: "$2" },
|
||||
{ path: "log_level"; value: 1 }
|
||||
);
|
||||
},
|
||||
{ name: "socks5"; pattern: "(.+):(\w+)"; description: "Set up socks5 target";
|
||||
list: "protocols";
|
||||
override: "name";
|
||||
argdesc: "<host:port>";
|
||||
targets: (
|
||||
{ path: "name"; value: "socks5" },
|
||||
{ path: "host"; value: "$1" },
|
||||
{ path: "port"; value: "$2" },
|
||||
{ path: "log_level"; value: 1 }
|
||||
);
|
||||
},
|
||||
{ name: "syslog"; pattern: "(.+):(\w+)"; description: "Set up syslog target";
|
||||
list: "protocols";
|
||||
override: "name";
|
||||
argdesc: "<host:port>";
|
||||
targets: (
|
||||
{ path: "name"; value: "syslog" },
|
||||
{ path: "host"; value: "$1" },
|
||||
{ path: "port"; value: "$2" },
|
||||
{ path: "log_level"; value: 1 }
|
||||
);
|
||||
},
|
||||
{ name: "anyprot"; pattern: "(.+):(\w+)"; description: "Set up default target";
|
||||
list: "protocols";
|
||||
override: "name";
|
||||
argdesc: "<host:port>";
|
||||
targets: (
|
||||
{ path: "name"; value: "anyprot" },
|
||||
{ path: "host"; value: "$1" },
|
||||
{ path: "port"; value: "$2" },
|
||||
{ path: "log_level"; value: 1 }
|
||||
);
|
||||
}
|
||||
)
|
||||
@@ -21,7 +21,7 @@ my $sslh_port = $conf->fetch_array("listen")->[0]->{port};
|
||||
my $user = (getpwuid $<)[0]; # Run under current username
|
||||
|
||||
# Which tests do we run
|
||||
my $SSH_SHY_CNX = 1;
|
||||
my $SSH_SHY_CNX = 0;
|
||||
my $PROBES_NOFRAG = 1;
|
||||
my $PROBES_AGAIN = 1;
|
||||
my $SSL_MIX_SSH = 1;
|
||||
@@ -31,11 +31,12 @@ my $SSH_MIX_SSL = 1;
|
||||
# coverage, but do not necessarily result in an actual test
|
||||
# (e.g. some tests need to be run with valgrind to check all
|
||||
# memory management code).
|
||||
my $RB_CNX_NOSERVER = 1;
|
||||
my $RB_PARAM_NOHOST = 1;
|
||||
my $RB_WRONG_USERNAME = 1;
|
||||
my $RB_OPEN_PID_FILE = 1;
|
||||
my $RB_RESOLVE_ADDRESS = 1;
|
||||
my $RB_CNX_NOSERVER = 0;
|
||||
my $RB_PARAM_NOHOST = 0;
|
||||
my $RB_WRONG_USERNAME = 0;
|
||||
my $RB_OPEN_PID_FILE = 0;
|
||||
my $RB_RESOLVE_ADDRESS = 0;
|
||||
my $RB_CL_PARAMS = 0;
|
||||
|
||||
`lcov --directory . --zerocounters`;
|
||||
|
||||
@@ -49,6 +50,17 @@ sub verbose_exec
|
||||
}
|
||||
}
|
||||
|
||||
# We want to keep track of tests to print a report at the
|
||||
# end, so we centralise all calls to Test::More::is here
|
||||
my $cnt = 1; # test counter
|
||||
my @results;
|
||||
sub my_is {
|
||||
my ($a, $b, $desc) = @_;
|
||||
|
||||
my $res = is($a, $b, $desc);
|
||||
push @results, [$cnt++, $desc, $res];
|
||||
}
|
||||
|
||||
|
||||
|
||||
# For SNI/ALPN, build a protocol name as such:
|
||||
@@ -90,12 +102,12 @@ sub test_probe {
|
||||
$data =~ /^(.*?): /;
|
||||
my $prefix = $1;
|
||||
$data =~ s/$prefix: //g;
|
||||
print "Received: protocol $prefix data [$data]\n";
|
||||
print "Received $n bytes: protocol $prefix data [$data]\n";
|
||||
close $cnx;
|
||||
|
||||
$opts{expected} =~ s/^ssl/tls/; # to remove in 1.21
|
||||
is($prefix, $opts{expected}, "probe $opts{expected} connected correctly");
|
||||
is($data, $opts{data}, "data shoveled correctly");
|
||||
my_is($prefix, $opts{expected}, "$opts{binary}:$opts{expected}: probe connected correctly");
|
||||
my_is($data, $opts{data}, "$opts{binary}:$opts{expected}: data shoveled correctly");
|
||||
}
|
||||
|
||||
# Test all probes, with or without fragmentation
|
||||
@@ -104,7 +116,7 @@ sub test_probe {
|
||||
# available per-protocol as some probes don't support
|
||||
# fragmentation)
|
||||
sub test_probes {
|
||||
my (%opts) = @_;
|
||||
my (%in_opts) = @_;
|
||||
|
||||
my @probes = @{$conf->fetch_array("protocols")};
|
||||
foreach my $p (@probes) {
|
||||
@@ -114,9 +126,6 @@ sub test_probes {
|
||||
'http' => {
|
||||
data => "GET index.html HTTP/1.1",
|
||||
no_frag => 1 },
|
||||
'ssl' => {
|
||||
data => "\x16\x03\x01\x00\xab\x01\x00\x00\xa7\x03\x03\x89\x22\x33\x95\x43\x7a\xc3\x89\x45\x51\x12\x3c\x28\x24\x1b\x6a\x78\xbf\xbe\x95\xd8\x90\x58\xd7\x65\xf7\xbb\x2d\xb2\x8d\xa0\x75\x00\x00\x38\xc0\x2c\xc0\x30\x00\x9f\xcc\xa9\xcc\xa8\xcc\xaa\xc0\x2b\xc0\x2f\x00\x9e\xc0\x24\xc0\x28\x00\x6b\xc0\x23\xc0\x27\x00\x67\xc0\x0a\xc0\x14\x00\x39\xc0\x09\xc0\x13\x00\x33\x00\x9d\x00\x9c\x00\x3d\x00\x3c\x00\x35\x00\x2f\x00\xff\x01\x00\x00\x46\x00\x0b\x00\x04\x03\x00\x01\x02\x00\x0a\x00\x0a\x00\x08\x00\x1d\x00\x17\x00\x19\x00\x18\x00\x23\x00\x00\x00\x0d\x00\x20\x00\x1e\x06\x01\x06\x02\x06\x03\x05\x01\x05\x02\x05\x03\x04\x01\x04\x02\x04\x03\x03\x01\x03\x02\x03\x03\x02\x01\x02\x02\x02\x03\x00\x16\x00\x00\x00\x17\x00\x00hello ssl alone"
|
||||
},
|
||||
'tls' => {
|
||||
# Packet with SNI and ALPN (`openssl s_client -connect localhost:443 -alpn alpn1 -servername sni1`)
|
||||
data_sni_alpn => "\x16\x03\x01\x00\xc4\x01\x00\x00\xc0\x03\x03\x03\x19\x01\x00\x40\x14\x13\xcc\x1b\x94\xad\x20\x5d\x13\x1a\x8d\xd2\x65\x23\x70\xde\xd1\x3c\x5d\x05\x19\xcb\x27\x0d\x7c\x2c\x89\x00\x00\x38\xc0\x2c\xc0\x30\x00\x9f\xcc\xa9\xcc\xa8\xcc\xaa\xc0\x2b\xc0\x2f\x00\x9e\xc0\x24\xc0\x28\x00\x6b\xc0\x23\xc0\x27\x00\x67\xc0\x0a\xc0\x14\x00\x39\xc0\x09\xc0\x13\x00\x33\x00\x9d\x00\x9c\x00\x3d\x00\x3c\x00\x35\x00\x2f\x00\xff\x01\x00\x00\x5f\x00\x00\x00\x09\x00\x07\x00\x00\x04\$sni\x00\x0b\x00\x04\x03\x00\x01\x02\x00\x0a\x00\x0a\x00\x08\x00\x1d\x00\x17\x00\x19\x00\x18\x00\x23\x00\x00\x00\x0d\x00\x20\x00\x1e\x06\x01\x06\x02\x06\x03\x05\x01\x05\x02\x05\x03\x04\x01\x04\x02\x04\x03\x03\x01\x03\x02\x03\x03\x02\x01\x02\x02\x02\x03\x00\x10\x00\x08\x00\x06\x05\$alpn\x00\x16\x00\x00\x00\x17\x00\x00hello sni/alpn",
|
||||
@@ -128,6 +137,7 @@ sub test_probes {
|
||||
data => "\x16\x03\x01\x00\xab\x01\x00\x00\xa7\x03\x03\x89\x22\x33\x95\x43\x7a\xc3\x89\x45\x51\x12\x3c\x28\x24\x1b\x6a\x78\xbf\xbe\x95\xd8\x90\x58\xd7\x65\xf7\xbb\x2d\xb2\x8d\xa0\x75\x00\x00\x38\xc0\x2c\xc0\x30\x00\x9f\xcc\xa9\xcc\xa8\xcc\xaa\xc0\x2b\xc0\x2f\x00\x9e\xc0\x24\xc0\x28\x00\x6b\xc0\x23\xc0\x27\x00\x67\xc0\x0a\xc0\x14\x00\x39\xc0\x09\xc0\x13\x00\x33\x00\x9d\x00\x9c\x00\x3d\x00\x3c\x00\x35\x00\x2f\x00\xff\x01\x00\x00\x46\x00\x0b\x00\x04\x03\x00\x01\x02\x00\x0a\x00\x0a\x00\x08\x00\x1d\x00\x17\x00\x19\x00\x18\x00\x23\x00\x00\x00\x0d\x00\x20\x00\x1e\x06\x01\x06\x02\x06\x03\x05\x01\x05\x02\x05\x03\x04\x01\x04\x02\x04\x03\x03\x01\x03\x02\x03\x03\x02\x01\x02\x02\x02\x03\x00\x16\x00\x00\x00\x17\x00\x00hello tls alone"
|
||||
},
|
||||
'openvpn' => { data => "\x00\x00" },
|
||||
'syslog' => { data => "<42> My syslog message" },
|
||||
'tinc' => { data => "0 hello" },
|
||||
'xmpp' => {data => "I should get a real jabber connection initialisation here" },
|
||||
'adb' => { data => "CNXN....................host:..." },
|
||||
@@ -136,6 +146,7 @@ sub test_probes {
|
||||
|
||||
my $pattern = $protocols{$p->{name}}->{data};
|
||||
|
||||
my %opts = %in_opts;
|
||||
$opts{no_frag} = 1 if $protocols{$p->{name}}->{no_frag};
|
||||
|
||||
if ($p->{sni_hostnames} or $p->{alpn_protocols}) {
|
||||
@@ -160,6 +171,14 @@ sub test_probes {
|
||||
);
|
||||
}
|
||||
}
|
||||
} elsif ($p->{name} eq 'regex') {
|
||||
foreach my $test (@{$p->{test_patterns}}) {
|
||||
test_probe(
|
||||
data => $test->{pattern},
|
||||
expected => $test->{result},
|
||||
%opts
|
||||
);
|
||||
}
|
||||
} else {
|
||||
test_probe(
|
||||
data => $pattern,
|
||||
@@ -187,22 +206,25 @@ foreach my $s (@{$conf->fetch_array("protocols")}) {
|
||||
}
|
||||
|
||||
|
||||
my @binaries = ('sslh-select', 'sslh-fork');
|
||||
#my @binaries = ('sslh-select', 'sslh-fork');
|
||||
my @binaries = ('sslh-select');
|
||||
|
||||
for my $binary (@binaries) {
|
||||
warn "Testing $binary\n";
|
||||
|
||||
# Start sslh with the right plumbing
|
||||
my $sslh_pid;
|
||||
my ($sslh_pid, $valgrind);
|
||||
if (!($sslh_pid = fork)) {
|
||||
my $user = (getpwuid $<)[0]; # Run under current username
|
||||
#my $cmd = "./$binary -v -f -u $user --listen localhost:$sslh_port --ssh $ssh_address --ssl $ssl_address -P $pidfile";
|
||||
my $cmd = "./$binary -v -f -u $user -Ftest.cfg";
|
||||
my $cmd = "./$binary -v 4 -f -u $user -F test.cfg";
|
||||
#$valgrind = 1;
|
||||
#$cmd = "valgrind --leak-check=full $cmd";
|
||||
verbose_exec $cmd;
|
||||
#exec "valgrind --leak-check=full ./$binary -v -f -u $user --listen localhost:$sslh_port --ssh $ssh_address -ssl $ssl_address -P $pidfile";
|
||||
exit 0;
|
||||
}
|
||||
warn "spawned $sslh_pid\n";
|
||||
sleep 5; # valgrind can be heavy -- wait 5 seconds
|
||||
sleep 1; # Give everyone some time to start
|
||||
sleep 5 if $valgrind; # valgrind can be heavy -- wait 5 seconds
|
||||
|
||||
|
||||
my $test_data = "hello world\n";
|
||||
@@ -214,10 +236,10 @@ for my $binary (@binaries) {
|
||||
my $cnx_h = new IO::Socket::INET(PeerHost => "localhost:$sslh_port");
|
||||
warn "$!\n" unless $cnx_h;
|
||||
if (defined $cnx_h) {
|
||||
sleep 3;
|
||||
sleep 13;
|
||||
print $cnx_h $test_data;
|
||||
my $data = <$cnx_h>;
|
||||
is($data, "ssh: $test_data", "Shy SSH connection");
|
||||
my_is($data, "ssh: $test_data", "$binary: Shy SSH connection");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,11 +256,11 @@ for my $binary (@binaries) {
|
||||
sleep 3;
|
||||
print $cnx_h $test_data;
|
||||
my $data_h = <$cnx_h>;
|
||||
is($data_h, "ssh: $test_data", "SSH during SSL being established");
|
||||
my_is($data_h, "ssh: $test_data", "$binary: SSH during SSL being established");
|
||||
}
|
||||
my $data;
|
||||
my $n = sysread $cnx_l, $data, 1024;
|
||||
is($data, "tls: $ssl_test_data", "SSL connection interrupted by SSH");
|
||||
my_is($data, "tls: $ssl_test_data", "$binary: SSL connection interrupted by SSH");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -255,21 +277,21 @@ for my $binary (@binaries) {
|
||||
print $cnx_l $ssl_test_data;
|
||||
my $data;
|
||||
my $n = sysread $cnx_l, $data, 1024;
|
||||
is($data, "tls: $ssl_test_data", "SSL during SSH being established");
|
||||
my_is($data, "tls: $ssl_test_data", "$binary: SSL during SSH being established");
|
||||
}
|
||||
print $cnx_h $test_data;
|
||||
my $data = <$cnx_h>;
|
||||
is($data, "ssh: $test_data", "SSH connection interrupted by SSL");
|
||||
my_is($data, "ssh: $test_data", "$binary: SSH connection interrupted by SSL");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ($PROBES_NOFRAG) {
|
||||
test_probes(no_frag => 1);
|
||||
test_probes(no_frag => 1, binary => $binary);
|
||||
}
|
||||
|
||||
if ($PROBES_AGAIN) {
|
||||
test_probes;
|
||||
test_probes(binary => $binary);
|
||||
}
|
||||
|
||||
my $pid = `cat $pidfile`;
|
||||
@@ -283,7 +305,7 @@ if ($RB_CNX_NOSERVER) {
|
||||
print "***Test: Connecting to non-existant server\n";
|
||||
my $sslh_pid;
|
||||
if (!($sslh_pid = fork)) {
|
||||
exec "./sslh-select -v -f -u $user --listen localhost:$sslh_port --ssh localhost:$no_listen --ssl localhost:$no_listen -P $pidfile";
|
||||
exec "./sslh-select -v 3 -f -u $user --listen localhost:$sslh_port --ssh localhost:$no_listen --tls localhost:$no_listen -P $pidfile";
|
||||
}
|
||||
warn "spawned $sslh_pid\n";
|
||||
|
||||
@@ -316,13 +338,13 @@ if ($RB_PARAM_NOHOST) {
|
||||
print "***Test: No hostname in address\n";
|
||||
my $sslh_pid;
|
||||
if (!($sslh_pid = fork)) {
|
||||
exec "./sslh-select -v -f -u $user --listen $sslh_port --ssh $ssh_address --ssl $ssl_address -P $pidfile";
|
||||
exec "./sslh-select -v 3 -f -u $user --listen $sslh_port --ssh $ssh_address --tls $ssl_address -P $pidfile";
|
||||
}
|
||||
warn "spawned $sslh_pid\n";
|
||||
waitpid $sslh_pid, 0;
|
||||
my $code = $? >> 8;
|
||||
warn "exited with $code\n";
|
||||
is($code, 1, "Exit status on illegal option");
|
||||
my_is($code, 6, "Exit status on illegal option");
|
||||
}
|
||||
|
||||
# Robustness: User does not exist
|
||||
@@ -330,13 +352,13 @@ if ($RB_WRONG_USERNAME) {
|
||||
print "***Test: Changing to non-existant username\n";
|
||||
my $sslh_pid;
|
||||
if (!($sslh_pid = fork)) {
|
||||
exec "./sslh-select -v -f -u ${user}_doesnt_exist --listen localhost:$sslh_port --ssh $ssh_address --ssl $ssl_address -P $pidfile";
|
||||
exec "./sslh-select -v 3 -f -u ${user}_doesnt_exist --listen localhost:$no_listen --ssh $ssh_address --tls $ssl_address -P $pidfile";
|
||||
}
|
||||
warn "spawned $sslh_pid\n";
|
||||
waitpid $sslh_pid, 0;
|
||||
my $code = $? >> 8;
|
||||
warn "exited with $code\n";
|
||||
is($code, 2, "Exit status on non-existant username");
|
||||
my_is($code, 2, "Exit status on non-existant username");
|
||||
}
|
||||
|
||||
# Robustness: Can't open PID file
|
||||
@@ -344,14 +366,14 @@ if ($RB_OPEN_PID_FILE) {
|
||||
print "***Test: Can't open PID file\n";
|
||||
my $sslh_pid;
|
||||
if (!($sslh_pid = fork)) {
|
||||
exec "./sslh-select -v -f -u $user --listen localhost:$sslh_port --ssh $ssh_address --ssl $ssl_address -P /dont_exist/$pidfile";
|
||||
exec "./sslh-select -v 3 -f -u $user --listen localhost:$no_listen --ssh $ssh_address --tls $ssl_address -P /dont_exist/$pidfile";
|
||||
# You don't have a /dont_exist/ directory, do you?!
|
||||
}
|
||||
warn "spawned $sslh_pid\n";
|
||||
waitpid $sslh_pid, 0;
|
||||
my $code = $? >> 8;
|
||||
warn "exited with $code\n";
|
||||
is($code, 3, "Exit status if can't open PID file");
|
||||
my_is($code, 3, "Exit status if can't open PID file");
|
||||
}
|
||||
|
||||
# Robustness: Can't resolve address
|
||||
@@ -360,13 +382,62 @@ if ($RB_RESOLVE_ADDRESS) {
|
||||
my $sslh_pid;
|
||||
if (!($sslh_pid = fork)) {
|
||||
my $user = (getpwuid $<)[0]; # Run under current username
|
||||
exec "./sslh-select -v -f -u $user --listen blahblah.dontexist:9000 --ssh $ssh_address --ssl $ssl_address -P $pidfile";
|
||||
exec "./sslh-select -v 3 -f -u $user --listen blahblah.dontexist:9000 --ssh $ssh_address --tls $ssl_address -P $pidfile";
|
||||
}
|
||||
warn "spawned $sslh_pid\n";
|
||||
waitpid $sslh_pid, 0;
|
||||
my $code = $? >> 8;
|
||||
warn "exited with $code\n";
|
||||
is($code, 4, "Exit status if can't resolve address");
|
||||
my_is($code, 4, "Exit status if can't resolve address");
|
||||
}
|
||||
|
||||
# Robustness: verify all command line options work
|
||||
if ($RB_CL_PARAMS) {
|
||||
print "***Test: Command line parameters\n";
|
||||
my $sslh_pid;
|
||||
if (!($sslh_pid = fork)) {
|
||||
my $user = (getpwuid $<)[0]; # Run under current username
|
||||
# This doesn't test --inetd
|
||||
exec "./sslh-select -v 3 -f -u $user -P $pidfile".
|
||||
" -n --transparent --timeout 10 -C /tmp".
|
||||
" --syslog-facility auth --on-timeout ssh".
|
||||
" --listen localhost:$no_listen --ssh $ssh_address --tls $ssl_address".
|
||||
" --openvpn localhost:$no_listen".
|
||||
" --tinc localhost:$no_listen".
|
||||
" --xmpp localhost:$no_listen".
|
||||
" --http localhost:$no_listen".
|
||||
" --adb localhost:$no_listen".
|
||||
" --socks5 localhost:$no_listen".
|
||||
" --anyprot localhost:$no_listen";
|
||||
exit 0;
|
||||
}
|
||||
warn "spawned $sslh_pid\n";
|
||||
# It will die soon because $user cannot chroot (you
|
||||
# don't test as root, do you?)
|
||||
|
||||
waitpid $sslh_pid, 0;
|
||||
my $code = $? >> 8;
|
||||
warn "exited with $code\n";
|
||||
my_is($code, 1, "Command line arguments");
|
||||
|
||||
|
||||
print "***Test: Bad command line parameters\n";
|
||||
my $sslh_pid;
|
||||
if (!($sslh_pid = fork)) {
|
||||
my $user = (getpwuid $<)[0]; # Run under current username
|
||||
# This doesn't test --inetd
|
||||
exec "./sslh-select -v 3 -f -u $user -P $pidfile".
|
||||
" -n --transparent --timeout 10 -C /tmp".
|
||||
" --fakeoption".
|
||||
" --anyprot localhost:$no_listen";
|
||||
exit 0;
|
||||
}
|
||||
warn "spawned $sslh_pid\n";
|
||||
|
||||
waitpid $sslh_pid, 0;
|
||||
my $code = $? >> 8;
|
||||
warn "exited with $code\n";
|
||||
my_is($code, 6, "Bad command line parameters");
|
||||
}
|
||||
|
||||
`lcov --directory . --capture --output-file sslh_cov.info`;
|
||||
@@ -374,3 +445,17 @@ if ($RB_RESOLVE_ADDRESS) {
|
||||
|
||||
`killall echosrv`;
|
||||
|
||||
|
||||
format test_results_top =
|
||||
ID | Description | Status
|
||||
----+-------------------------------------------------------------------+-------
|
||||
.
|
||||
|
||||
format test_results =
|
||||
@>> | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | @>>
|
||||
$_->[0], $_->[1], $_->[2] ? "OK" : "NOK"
|
||||
.
|
||||
|
||||
format_name STDOUT "test_results";
|
||||
format_top_name STDOUT "test_results_top";
|
||||
map { write; } @results;
|
||||
@@ -14,114 +14,264 @@
|
||||
use strict;
|
||||
use IO::Socket::INET6;
|
||||
use Data::Dumper;
|
||||
use Conf::Libconfig;
|
||||
|
||||
## BEGIN TEST CONFIG
|
||||
|
||||
# Do we test sslh-select or sslh-fork?
|
||||
my $sslh_binary = "./sslh-select";
|
||||
|
||||
# How many clients to we start for each protocol?
|
||||
my $NUM_CNX = 30;
|
||||
# How many total clients to we start? Each client will pick
|
||||
# a new protocol among what's in test.cfg.
|
||||
my $NUM_CNX = 50;
|
||||
|
||||
# Delay between starting new processes when starting up. If
|
||||
# you start 200 processes in under a second, things go wrong
|
||||
# and it's not sslh's fault (typically the echosrv won't be
|
||||
# forking fast enough).
|
||||
my $start_time_delay = 1;
|
||||
|
||||
# If you test 4 protocols, you'll start $NUM_CNX * 4 clients
|
||||
# (e.g. 40), starting one every $start_time_delay seconds.
|
||||
my $start_time_delay = .5;
|
||||
|
||||
# Max times we repeat the test string: allows to test for
|
||||
# large messages.
|
||||
my $block_rpt = 4096;
|
||||
my $block_rpt = 5;
|
||||
|
||||
# Probability to stop a client after a message (e.g. with
|
||||
# .01 a client will send an average of 100 messages before
|
||||
# disconnecting).
|
||||
my $stop_client_probability = .001;
|
||||
|
||||
# What protocols we test, and on what ports
|
||||
# Just comment out protocols you don't want to use.
|
||||
my %protocols = (
|
||||
"ssh" => { address => "localhost:9001", client => client("ssh") },
|
||||
"ssl" => { address => "localhost:9002", client => client("ssl") },
|
||||
"openvpn" => {address => "localhost:9003", client => client("openvpn") },
|
||||
"tinc" => {address => "localhost:9004", client => client("tinc") },
|
||||
);
|
||||
|
||||
##END CONFIG
|
||||
|
||||
my $conf = new Conf::Libconfig;
|
||||
$conf->read_file("test.cfg");
|
||||
|
||||
|
||||
# Pick one address for TCP and one for UDP
|
||||
my @listen = @{$conf->fetch_array("listen")};
|
||||
my ($sslh_tcp_address, $sslh_udp_address);
|
||||
|
||||
foreach my $l (@listen) {
|
||||
if ($l->{is_udp}) {
|
||||
$sslh_udp_address //= "$l->{host}:$l->{port}";
|
||||
} else {
|
||||
$sslh_tcp_address //= "$l->{host}:$l->{port}";
|
||||
}
|
||||
last if defined $sslh_tcp_address and defined $sslh_udp_address;
|
||||
}
|
||||
|
||||
|
||||
# code snippets to connect to each protocol
|
||||
my %connect_params = (
|
||||
regex => {
|
||||
is_udp => 1,
|
||||
sleep => 0,
|
||||
test_data => "foo bar",
|
||||
resp_len => 12,
|
||||
},
|
||||
ssh => {
|
||||
sleep => 20, # So it times out 50% of connections
|
||||
test_data => "SSH-2.0 hello",
|
||||
resp_len => 18, # length "ssh: SSH-2.0 hello" => 18
|
||||
},
|
||||
tinc => {
|
||||
sleep => 0,
|
||||
test_data => "0 ",
|
||||
resp_len => 8, # length "tinc: 0 " => 10
|
||||
},
|
||||
openvpn => {
|
||||
sleep => 0,
|
||||
test_data => "\x00\x00",
|
||||
resp_len => 11, # length "openvpn: \x0\x0" => 11
|
||||
},
|
||||
);
|
||||
|
||||
sub connect_service {
|
||||
my ($cnx, $service) = @_;
|
||||
|
||||
my $params = $connect_params{$service};
|
||||
|
||||
sleep rand $params->{sleep};
|
||||
my $test_data = $params->{test_data};
|
||||
syswrite $cnx, $test_data;
|
||||
sleep 1;
|
||||
sysread $cnx, my $r, $params->{resp_len};
|
||||
|
||||
my $expected = "$service: $test_data";
|
||||
return ($r eq $expected);
|
||||
}
|
||||
|
||||
# We use ports 9000, 9001 and 9002 -- hope that won't clash
|
||||
# with anything...
|
||||
my $sslh_address = "localhost:9000";
|
||||
my $pidfile = "/tmp/sslh_test.pid";
|
||||
|
||||
sub client {
|
||||
my ($service) = @_;
|
||||
my ($protocol, $client_id, $fd_out) = @_;
|
||||
|
||||
return sub {
|
||||
while (1) {
|
||||
my $cnx = new IO::Socket::INET(PeerHost => $sslh_address);
|
||||
my $test_data = "$service testing " x int(rand($block_rpt)+1) . "\n";
|
||||
my $service = $protocol->{name};
|
||||
|
||||
sleep 5 if $service eq "ssh";
|
||||
if ($service eq "openvpn") {
|
||||
syswrite $cnx, "\x00\x0F\x38\n";
|
||||
my $msg;
|
||||
sysread $cnx, $msg, 14; # length "openvpn: \x0\xF\x38\n" => 14
|
||||
}
|
||||
if ($service eq "tinc") {
|
||||
syswrite $cnx, "0 \n";
|
||||
my $msg;
|
||||
sysread $cnx, $msg, 10; # length "tinc: 0 \n" => 10
|
||||
}
|
||||
while (1) {
|
||||
print $cnx $test_data;
|
||||
my $r = <$cnx>;
|
||||
($? = 1, die "$service got [$r]\n") if ($r ne "$service: $test_data");
|
||||
last if rand(1) < $stop_client_probability;
|
||||
}
|
||||
}
|
||||
exit 0;
|
||||
}
|
||||
}
|
||||
while (1) {
|
||||
my $r;
|
||||
#warn "$client_id: connect $sslh_tcp_address\n";
|
||||
my $cnx = new IO::Socket::INET(PeerHost => $sslh_tcp_address);
|
||||
die "$@\n" if (!$cnx);
|
||||
|
||||
foreach my $p (keys %protocols) {
|
||||
if (!fork) {
|
||||
exec "./echosrv --listen $protocols{$p}->{address} --prefix '$p: '";
|
||||
}
|
||||
}
|
||||
my $cnt = 0;
|
||||
|
||||
# Start sslh with the right plumbing
|
||||
my $sslh_pid;
|
||||
if (!($sslh_pid = fork)) {
|
||||
my $user = (getpwuid $<)[0]; # Run under current username
|
||||
my $prots = join " ", map "--$_ $protocols{$_}->{address}", keys %protocols;
|
||||
my $cmd = "$sslh_binary -f -t 3 -u $user --listen $sslh_address $prots -P $pidfile";
|
||||
print "$cmd\n";
|
||||
exec $cmd;
|
||||
exit 0;
|
||||
}
|
||||
warn "spawned $sslh_pid\n";
|
||||
sleep 2; # valgrind can be heavy -- wait 5 seconds
|
||||
#warn "$client_id: connecting $service\n";
|
||||
|
||||
|
||||
for (1 .. $NUM_CNX) {
|
||||
foreach my $p (keys %protocols) {
|
||||
if (!fork) {
|
||||
warn "starting $p\n";
|
||||
&{$protocols{$p}->{client}};
|
||||
if (not connect_service($cnx, $service)) {
|
||||
print $fd_out "$client_id\t0\tC\n";
|
||||
warn "$client_id: connecting failed\n";
|
||||
exit;
|
||||
}
|
||||
# Give a little time so we don't overrun the
|
||||
# listen(2) backlog.
|
||||
select undef, undef, undef, $start_time_delay;
|
||||
|
||||
#warn "$client_id: shoveling $service\n";
|
||||
|
||||
while (1) {
|
||||
my $test_data = "$service $cnt" x int(rand($block_rpt)+1) . "\n";
|
||||
print $cnx $test_data;
|
||||
$r = <$cnx>;
|
||||
my $expected= "$test_data";
|
||||
my $r_l = length $r;
|
||||
my $e_l = length $expected;
|
||||
$fd_out->autoflush;
|
||||
my $error = "";
|
||||
$error = "E" if $r ne $expected;
|
||||
print $fd_out ("$client_id\t$r_l\t$error\n");
|
||||
($? = 1, die "$service got [$r] expected [$expected]\n") if ($r ne $expected);
|
||||
if (rand(1) < $stop_client_probability) {
|
||||
print $fd_out ("$client_id\t$r_l\tD\n");
|
||||
last;
|
||||
}
|
||||
$cnt++;
|
||||
}
|
||||
}
|
||||
exit 0;
|
||||
}
|
||||
|
||||
# For now, a simple regex client
|
||||
sub udp_client {
|
||||
my ($protocol, $client_id, $fd_out) = @_;
|
||||
|
||||
warn "UDP client starts\n";
|
||||
|
||||
while (1) {
|
||||
my $cnx = new IO::Socket::INET(Proto => 'udp', PeerHost => $sslh_udp_address);
|
||||
# my $cnx; socket $cnx, PF_INET, SOCK_DGRAM, 0 or die "socket: $!\n";
|
||||
die "$@\n" if (!$cnx);
|
||||
my $cnt = 0;
|
||||
|
||||
while (1) {
|
||||
my $test_data = "foo udp $cnt"x int(rand($block_rpt)+1). "\n";
|
||||
|
||||
my $ipaddr = inet_aton("localhost");
|
||||
my $portaddr = sockaddr_in(8086, $ipaddr);
|
||||
my $res = send($cnx, $test_data, 0, $portaddr);
|
||||
if ($res != length($test_data)) {
|
||||
die "cannot sendto: $!";
|
||||
}
|
||||
|
||||
my $expected= "$protocol->{name}: $test_data";
|
||||
|
||||
my $r;
|
||||
defined(recv($cnx, $r, length $expected, 0)) or die "recv: $!\n";
|
||||
|
||||
my $r_l = length $r;
|
||||
my $e_l = length $expected;
|
||||
$fd_out->autoflush;
|
||||
my $error = "";
|
||||
$error = "M" if $r ne $expected;
|
||||
print $fd_out ("$client_id\t$r_l\t$error\n");
|
||||
($? = 1, die "udp got [$r] expected [$expected]\n") if ($r ne $expected);
|
||||
if (rand(1) < $stop_client_probability) {
|
||||
print $fd_out ("$client_id\t$r_l\tD\n");
|
||||
last;
|
||||
}
|
||||
$cnt++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach my $p (@{$conf->fetch_array("protocols")}) {
|
||||
if (!fork) {
|
||||
my $udp = $p->{is_udp} ? "--udp" : "";
|
||||
my $cmd = "./echosrv $udp -p $p->{host}:$p->{port} --prefix '$p->{name}: ' 2> /dev/null";
|
||||
warn "$cmd\n";
|
||||
exec $cmd;
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
warn "Don't forget to run sslh -F test.cfg!\n";
|
||||
|
||||
sleep 2; # Let echosrv's and sslh start
|
||||
|
||||
my ($c_in, $c_out);
|
||||
pipe $c_in, $c_out;
|
||||
|
||||
my @protocols = @{$conf->fetch_array("protocols")};
|
||||
|
||||
if (!fork) {
|
||||
# Process that starts all the clients
|
||||
for my $client_num (1 .. $NUM_CNX) {
|
||||
if (!fork) {
|
||||
my @supported_protocols = keys %connect_params;
|
||||
my $p_name = $supported_protocols[rand @supported_protocols];
|
||||
my @p = grep { $_->{name} eq $p_name } @protocols;
|
||||
my $p = shift @p;
|
||||
if ($p->{is_udp}) {
|
||||
udp_client($p, "$p->{name}$client_num", $c_out);
|
||||
} else {
|
||||
client($p, "$p->{name}$client_num", $c_out);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
# Give a little time so we don't overrun the
|
||||
# listen(2) backlog.
|
||||
select undef, undef, undef, $start_time_delay;
|
||||
}
|
||||
exit;
|
||||
} else {
|
||||
my %data;
|
||||
|
||||
# The condition here selects between pretty output or
|
||||
# raw output
|
||||
if (1) {
|
||||
my $CLEAR_LINE = "\033[2K";
|
||||
my $CURSOR_HOME = "\033[1;1H";
|
||||
my $CLEAR_SCREEN = "\033[2J";
|
||||
|
||||
# Process that retrieves client output to pretty print
|
||||
print $CLEAR_SCREEN; # Clear screen
|
||||
while (<$c_in>) {
|
||||
chop;
|
||||
my ($client_id, $r_l, $error, @rest) = split /\t/, $_;
|
||||
|
||||
$data{$client_id} = [ 0, ""] if not exists $data{$client_id};
|
||||
my ($curr_rcv) = ${$data{$client_id}}[0] + $r_l;;
|
||||
$error //= "";
|
||||
my ($curr_error) = "${$data{$client_id}}[1]$error";
|
||||
# Consolidate into... roman numerals!
|
||||
$curr_error =~ s/D{10}/X/;
|
||||
$curr_error =~ s/X{10}/C/;
|
||||
$curr_error =~ s/C{10}/M/;
|
||||
|
||||
$data{$client_id} = [ $r_l + $curr_rcv, "$curr_error$error" ];
|
||||
|
||||
|
||||
$client_id =~ /(\d+)/;
|
||||
my $i = $1;
|
||||
# print $CURSOR_HOME;
|
||||
print "\033[$i;1H$CLEAR_LINE$client_id\t$curr_rcv\t$curr_error\n";
|
||||
#foreach my $i (sort keys %data) {
|
||||
# ($r_l, $error) = @{$data{$i}};
|
||||
# print "$CLEAR_LINE$i\t$r_l\t$error\n";
|
||||
}
|
||||
} else {
|
||||
# Just print the client outputs
|
||||
while (<$c_in>) {
|
||||
print;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
warn "waiting\n";
|
||||
wait;
|
||||
warn "finished waiting\n";
|
||||
|
||||
|
||||
`killall echosrv`;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
# Configuration file for testing (use both by sslh under
|
||||
# test and the test script `t`)
|
||||
|
||||
verbose: 2;
|
||||
verbose: 4;
|
||||
foreground: true;
|
||||
inetd: false;
|
||||
numeric: false;
|
||||
numeric: true;
|
||||
transparent: false;
|
||||
timeout: 10; # Probe test writes slowly
|
||||
pidfile: "/tmp/sslh_test.pid";
|
||||
@@ -17,26 +17,43 @@ syslog_facility: "auth";
|
||||
listen:
|
||||
(
|
||||
{ host: "localhost"; port: "8080"; keepalive: true; },
|
||||
{ host: "localhost"; port: "8081"; keepalive: true; }
|
||||
{ host: "localhost"; port: "8081"; keepalive: true; },
|
||||
{ host: "ip4-localhost"; is_udp: true; port: "8086"; }
|
||||
);
|
||||
|
||||
|
||||
protocols:
|
||||
(
|
||||
{ name: "ssh"; host: "localhost"; port: "9000"; fork: true; },
|
||||
{ name: "ssh"; host: "localhost"; port: "9000"; fork: true; transparent: true; },
|
||||
{ name: "socks5"; host: "localhost"; port: "9001"; },
|
||||
{ name: "http"; host: "localhost"; port: "9002"; },
|
||||
{ name: "tinc"; host: "localhost"; port: "9003"; },
|
||||
{ name: "openvpn"; host: "localhost"; port: "9004"; },
|
||||
{ name: "xmpp"; host: "localhost"; port: "9009"; },
|
||||
{ name: "adb"; host: "localhost"; port: "9010"; },
|
||||
{ name: "syslog"; host: "localhost"; port: "9013"; },
|
||||
{ name: "regex"; host: "ip4-localhost"; is_udp: true; port: "9020";
|
||||
udp_timeout: 30;
|
||||
regex_patterns: [ "^foo" ];
|
||||
},
|
||||
{ name: "regex"; host: "localhost"; port: "9011";
|
||||
regex_patterns: [ "^foo", "^bar" ];
|
||||
minlength: 4;
|
||||
test_patterns: ( # this is used by the test script, not by sslh
|
||||
{ pattern: "foo"; result: "ssh"; }, # After timeout
|
||||
{ pattern: "fooo"; result: "regex"; },
|
||||
{ pattern: "bar"; result: "ssh"; },
|
||||
{ pattern: "barr"; result: "regex"; },
|
||||
{ pattern: "barrrr"; result: "regex"; }
|
||||
);
|
||||
},
|
||||
{ name: "tls"; host: "localhost"; port: "9021"; alpn_protocols: [ "alpn1", "alpn2" ]; sni_hostnames: [ "sni1" ]; },
|
||||
{ name: "ssl"; host: "localhost"; port: "9022"; alpn_protocols: [ "alpn1", "alpn2" ]; sni_hostnames: [ "sni2", "sni3" ]; },
|
||||
{ name: "tls"; host: "localhost"; port: "9022"; alpn_protocols: [ "alpn1", "alpn2" ]; sni_hostnames: [ "sni2", "sni3" ]; },
|
||||
{ name: "tls"; host: "localhost"; port: "9023"; alpn_protocols: [ "alpn3" ]; },
|
||||
{ name: "tls"; host: "localhost"; port: "9024"; sni_hostnames: [ "sni3" ]; },
|
||||
{ name: "ssl"; host: "localhost"; port: "9025"; },
|
||||
{ name: "tls"; host: "localhost"; port: "9025"; },
|
||||
{ name: "anyprot"; host: "localhost"; port: "9099"; }
|
||||
);
|
||||
|
||||
on-timeout: "ssh";
|
||||
on_timeout: "ssh";
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include <stdlib.h> /* malloc() */
|
||||
#include <fnmatch.h> /* fnmatch() */
|
||||
#include "tls.h"
|
||||
#include "sslh-conf.h"
|
||||
|
||||
#define TLS_HEADER_LEN 5
|
||||
#define TLS_HANDSHAKE_CONTENT_TYPE 0x16
|
||||
@@ -48,14 +49,16 @@ typedef struct {
|
||||
|
||||
struct TLSProtocol {
|
||||
TLS_MATCHMODE match_mode;
|
||||
char** sni_hostname_list;
|
||||
char** alpn_protocol_list;
|
||||
int sni_list_len;
|
||||
const char** sni_hostname_list;
|
||||
int alpn_list_len;
|
||||
const char** alpn_protocol_list;
|
||||
};
|
||||
|
||||
static int parse_extensions(const struct TLSProtocol *, const char *, size_t);
|
||||
static int parse_server_name_extension(const struct TLSProtocol *, const char *, size_t);
|
||||
static int parse_alpn_extension(const struct TLSProtocol *, const char *, size_t);
|
||||
static int has_match(char**, const char*, size_t);
|
||||
static int has_match(const char**, size_t, const char*, size_t);
|
||||
|
||||
/* Parse a TLS packet for the Server Name Indication and ALPN extension in the client
|
||||
* hello handshake, returning a status code
|
||||
@@ -79,14 +82,14 @@ parse_tls_header(const struct TLSProtocol *tls_data, const char *data, size_t da
|
||||
|
||||
tls_content_type = data[0];
|
||||
if (tls_content_type != TLS_HANDSHAKE_CONTENT_TYPE) {
|
||||
if (verbose) fprintf(stderr, "Request did not begin with TLS handshake.\n");
|
||||
if (cfg.verbose) fprintf(stderr, "Request did not begin with TLS handshake.\n");
|
||||
return TLS_EPROTOCOL;
|
||||
}
|
||||
|
||||
tls_version_major = data[1];
|
||||
tls_version_minor = data[2];
|
||||
if (tls_version_major < 3) {
|
||||
if (verbose) fprintf(stderr, "Received SSL %d.%d handshake which cannot be parsed.\n",
|
||||
if (cfg.verbose) fprintf(stderr, "Received SSL %d.%d handshake which cannot be parsed.\n",
|
||||
tls_version_major, tls_version_minor);
|
||||
|
||||
return TLS_EVERSION;
|
||||
@@ -108,7 +111,7 @@ parse_tls_header(const struct TLSProtocol *tls_data, const char *data, size_t da
|
||||
return TLS_EPROTOCOL;
|
||||
}
|
||||
if (data[pos] != TLS_HANDSHAKE_TYPE_CLIENT_HELLO) {
|
||||
if (verbose) fprintf(stderr, "Not a client hello\n");
|
||||
if (cfg.verbose) fprintf(stderr, "Not a client hello\n");
|
||||
|
||||
return TLS_EPROTOCOL;
|
||||
}
|
||||
@@ -141,7 +144,7 @@ parse_tls_header(const struct TLSProtocol *tls_data, const char *data, size_t da
|
||||
pos += 1 + len;
|
||||
|
||||
if (pos == data_len && tls_version_major == 3 && tls_version_minor == 0) {
|
||||
if (verbose) fprintf(stderr, "Received SSL 3.0 handshake without extensions\n");
|
||||
if (cfg.verbose) fprintf(stderr, "Received SSL 3.0 handshake without extensions\n");
|
||||
return TLS_EVERSION;
|
||||
}
|
||||
|
||||
@@ -219,13 +222,13 @@ parse_server_name_extension(const struct TLSProtocol *tls_data, const char *data
|
||||
|
||||
switch (data[pos]) { /* name type */
|
||||
case 0x00: /* host_name */
|
||||
if(has_match(tls_data->sni_hostname_list, data + pos + 3, len)) {
|
||||
if(has_match(tls_data->sni_hostname_list, tls_data->sni_list_len, data + pos + 3, len)) {
|
||||
return len;
|
||||
} else {
|
||||
return TLS_ENOEXT;
|
||||
}
|
||||
default:
|
||||
if (verbose) fprintf(stderr, "Unknown server name extension name type: %d\n",
|
||||
if (cfg.verbose) fprintf(stderr, "Unknown server name extension name type: %d\n",
|
||||
data[pos]);
|
||||
}
|
||||
pos += 3 + len;
|
||||
@@ -248,10 +251,10 @@ parse_alpn_extension(const struct TLSProtocol *tls_data, const char *data, size_
|
||||
if (pos + 1 + len > data_len)
|
||||
return TLS_EPROTOCOL;
|
||||
|
||||
if (len > 0 && has_match(tls_data->alpn_protocol_list, data + pos + 1, len)) {
|
||||
if (len > 0 && has_match(tls_data->alpn_protocol_list, tls_data->alpn_list_len, data + pos + 1, len)) {
|
||||
return len;
|
||||
} else if (len > 0) {
|
||||
if (verbose) fprintf(stderr, "Unknown ALPN name: %.*s\n", (int)len, data + pos + 1);
|
||||
if (cfg.verbose) fprintf(stderr, "Unknown ALPN name: %.*s\n", (int)len, data + pos + 1);
|
||||
}
|
||||
pos += 1 + len;
|
||||
}
|
||||
@@ -263,15 +266,17 @@ parse_alpn_extension(const struct TLSProtocol *tls_data, const char *data, size_
|
||||
}
|
||||
|
||||
static int
|
||||
has_match(char** list, const char* name, size_t name_len) {
|
||||
char **item;
|
||||
has_match(const char** list, size_t list_len, const char* name, size_t name_len) {
|
||||
const char **item;
|
||||
int i;
|
||||
char *name_nullterminated = malloc(name_len+1);
|
||||
CHECK_ALLOC(name_nullterminated, "malloc");
|
||||
memcpy(name_nullterminated, name, name_len);
|
||||
name_nullterminated[name_len]='\0';
|
||||
|
||||
for (item = list; *item; item++) {
|
||||
if (verbose) fprintf(stderr, "matching [%.*s] with [%s]\n", (int)name_len, name, *item);
|
||||
for (i = 0; i < list_len; i++) {
|
||||
item = &list[i];
|
||||
if (cfg.verbose) fprintf(stderr, "matching [%.*s] with [%s]\n", (int)name_len, name, *item);
|
||||
if(!fnmatch(*item, name_nullterminated, 0)) {
|
||||
free(name_nullterminated);
|
||||
return 1;
|
||||
@@ -292,12 +297,14 @@ new_tls_data() {
|
||||
}
|
||||
|
||||
struct TLSProtocol *
|
||||
tls_data_set_list(struct TLSProtocol *tls_data, int alpn, char** list) {
|
||||
tls_data_set_list(struct TLSProtocol *tls_data, int alpn, const char** list, size_t list_len) {
|
||||
if (alpn) {
|
||||
tls_data->alpn_protocol_list = list;
|
||||
tls_data->alpn_list_len = list_len;
|
||||
tls_data->match_mode.tls_match_alpn = 1;
|
||||
} else {
|
||||
tls_data->sni_hostname_list = list;
|
||||
tls_data->sni_list_len = list_len;
|
||||
tls_data->match_mode.tls_match_sni = 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ struct TLSProtocol;
|
||||
int parse_tls_header(const struct TLSProtocol *tls_data, const char *data, size_t data_len);
|
||||
|
||||
struct TLSProtocol *new_tls_data();
|
||||
struct TLSProtocol *tls_data_set_list(struct TLSProtocol *, int, char**);
|
||||
struct TLSProtocol *tls_data_set_list(struct TLSProtocol *, int, const char**, size_t);
|
||||
|
||||
#define TLS_MATCH 1
|
||||
#define TLS_NOMATCH 0
|
||||
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
udp-listener.c: handles demultplexing UDP protocols
|
||||
|
||||
# Copyright (C) 2020-2021 Yves Rutschle
|
||||
#
|
||||
# This program is free software; you can redistribute it
|
||||
# and/or modify it under the terms of the GNU General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2 of the License, or (at your option) any later
|
||||
# version.
|
||||
#
|
||||
# This program 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 General Public License for more
|
||||
# details.
|
||||
#
|
||||
# The full text for the General Public License is here:
|
||||
# http://www.gnu.org/licenses/gpl.html
|
||||
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include "probe.h"
|
||||
#include "sslh-conf.h"
|
||||
#include "udp-listener.h"
|
||||
|
||||
|
||||
/* Find if the specified source has been seen before. -1 if not found
|
||||
*
|
||||
* TODO This is linear search and needs to be changed to something better for
|
||||
* production if we have more than a dozen sources
|
||||
* Also, this assumes src_addr from recvfrom() are repeatable for a specific
|
||||
* source...
|
||||
* */
|
||||
static int known_source(cnx_collection* collection, int max_fd, struct sockaddr* addr, socklen_t addrlen)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < max_fd; i++) {
|
||||
struct connection* cnx = collection_get_cnx_from_fd(collection, i);
|
||||
if (cnx && (cnx->type == SOCK_DGRAM) && cnx->target_sock) {
|
||||
if (!memcmp(&cnx->client_addr, addr, addrlen)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Process UDP coming from outside (client towards server)
|
||||
* If it's a new source, probe; otherwise, forward to previous target
|
||||
* Returns: >= 0 sockfd of newly allocated socket, for new connections
|
||||
* -1 otherwise
|
||||
* */
|
||||
int udp_c2s_forward(int sockfd, cnx_collection* collection, int max_fd)
|
||||
{
|
||||
char addr_str[NI_MAXHOST+1+NI_MAXSERV+1];
|
||||
struct sockaddr src_addr;
|
||||
struct addrinfo addrinfo;
|
||||
struct sslhcfg_protocols_item* proto;
|
||||
struct connection* cnx;
|
||||
ssize_t len;
|
||||
socklen_t addrlen;
|
||||
int res, target, out = -1;
|
||||
char data[65536]; /* Theoritical max is 65507 (https://en.wikipedia.org/wiki/User_Datagram_Protocol).
|
||||
This will do. Dynamic allocation is possible with the MSG_PEEK flag in recvfrom(2), but that'd imply
|
||||
malloc/free overhead for each packet, when really 64K is not that much */
|
||||
|
||||
addrlen = sizeof(src_addr);
|
||||
len = recvfrom(sockfd, data, sizeof(data), 0, &src_addr, &addrlen);
|
||||
if (len < 0) {
|
||||
perror("recvfrom");
|
||||
return -1;
|
||||
}
|
||||
target = known_source(collection, max_fd, &src_addr, addrlen);
|
||||
addrinfo.ai_addr = &src_addr;
|
||||
addrinfo.ai_addrlen = addrlen;
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "received %ld UDP from %d:%s\n", len, target, sprintaddr(addr_str, sizeof(addr_str), &addrinfo));
|
||||
|
||||
if (target == -1) {
|
||||
res = probe_buffer(data, len, &proto);
|
||||
/* First version: if we can't work out the protocol from the first
|
||||
* packet, drop it. Conceivably, we could store several packets to
|
||||
* run probes on packet sets */
|
||||
if (cfg.verbose) fprintf(stderr, "UDP probed: %d\n", res);
|
||||
if (res != PROBE_MATCH) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
out = socket(proto->saddr->ai_family, SOCK_DGRAM, 0);
|
||||
struct connection* cnx = collection_alloc_cnx_from_fd(collection, out);
|
||||
if (!cnx) return -1;
|
||||
target = out;
|
||||
cnx->target_sock = out;
|
||||
cnx->proto = proto;
|
||||
cnx->type = SOCK_DGRAM;
|
||||
cnx->client_addr = src_addr;
|
||||
cnx->addrlen = addrlen;
|
||||
cnx->local_endpoint = sockfd;
|
||||
}
|
||||
cnx = collection_get_cnx_from_fd(collection, target);
|
||||
|
||||
/* at this point src is the UDP connection */
|
||||
res = sendto(cnx->target_sock, data, len, 0,
|
||||
cnx->proto->saddr->ai_addr, cnx->proto->saddr->ai_addrlen);
|
||||
cnx->last_active = time(NULL);
|
||||
fprintf(stderr, "sending %d to %s\n",
|
||||
res, sprintaddr(data, sizeof(data), cnx->proto->saddr));
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
void udp_s2c_forward(struct connection* cnx)
|
||||
{
|
||||
int sockfd = cnx->target_sock;
|
||||
char data[65536];
|
||||
int res;
|
||||
|
||||
res = recvfrom(sockfd, data, sizeof(data), 0, NULL, NULL);
|
||||
fprintf(stderr, "recvfrom %d\n", res);
|
||||
CHECK_RES_DIE(res, "udp_listener/recvfrom");
|
||||
res = sendto(cnx->local_endpoint, data, res, 0,
|
||||
&cnx->client_addr, cnx->addrlen);
|
||||
cnx->last_active = time(NULL);
|
||||
fprintf(stderr, "sendto %d to\n", res);
|
||||
}
|
||||
|
||||
|
||||
/* returns date at which this socket times out. */
|
||||
int udp_timeout(struct connection* cnx)
|
||||
{
|
||||
if (cnx->type != SOCK_DGRAM) return 0; /* Not a UDP connection */
|
||||
|
||||
return cnx->proto->udp_timeout + cnx->last_active;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef UDPLISTENER_H
|
||||
#define UDPLISTENER_H
|
||||
|
||||
#include "collection.h"
|
||||
|
||||
/* UDP listener: upon incoming packet, find where it should go
|
||||
* This is run in its own process and never returns.
|
||||
*/
|
||||
void udp_listener(struct listen_endpoint* endpoint, int num_endpoints, int active_endpoint);
|
||||
|
||||
|
||||
/* Process UDP coming from outside (client towards server)
|
||||
* If it's a new source, probe; otherwise, forward to previous target
|
||||
* Returns: >= 0 sockfd of newly allocated socket, for new connections
|
||||
* -1 otherwise
|
||||
* */
|
||||
int udp_c2s_forward(int sockfd, cnx_collection* collection, int max_fd);
|
||||
|
||||
/* Process UDP coming from inside (server towards client) */
|
||||
void udp_s2c_forward(struct connection* cnx);
|
||||
|
||||
|
||||
/* returns how many seconds before socket times out. Negative if timed out
|
||||
* already.
|
||||
*/
|
||||
int udp_timeout(struct connection* cnx);
|
||||
|
||||
#endif /* UDPLISTENER_H */
|
||||
@@ -0,0 +1,31 @@
|
||||
# Example for UDP protocols
|
||||
|
||||
# Listen sockets get a `is_udp : true` option
|
||||
# Protocol entries work exactly the same as TCP, there is no
|
||||
# difference. Currently, if mix up TCP and UDP probes, sslh will
|
||||
# try them in order and forward a UDP packet to a TCP probe
|
||||
# that matches (but using UDP; meaning, sslh will happily
|
||||
# forward a UDP packet to udp:localhost:80 if the HTTP
|
||||
# probe is specified, and matches)
|
||||
|
||||
verbose: 3;
|
||||
foreground: true;
|
||||
inetd: false;
|
||||
numeric: false;
|
||||
transparent: false;
|
||||
timeout: 10;
|
||||
pidfile: "/tmp/sslh_test.pid";
|
||||
|
||||
|
||||
# List of interfaces on which we should listen
|
||||
# Options:
|
||||
listen: (
|
||||
{ host: "localhost"; is_udp: true; port: "4443"; }
|
||||
);
|
||||
|
||||
|
||||
protocols:
|
||||
(
|
||||
{ name: "regex"; regex_patterns: [ "rutschle.net" ]; host: "ns1.rutschle.net"; port: "53"; },
|
||||
{ name: "anyprot"; host: "localhost"; port: "9099"; }
|
||||
);
|
||||
Reference in new issue
Block a user