From 5715c21a077ee1c2fe8957cb4adcea14fd2eda6b Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 20 Nov 2020 23:24:29 +0000 Subject: Refactor tls code to support ucspi-tls That includes: - new architecture: the tls binary is now a child of the app instead of the other way around - the sbearssl_run engine now takes a post-handshake callback. This allows s6-tlsc and s6-tlsd to only exec into the app when the handshake succeeds (which was already the case with libressl). - new binaries s6-tlsc-io and s6-tlsd-io encapsulate the crypto code; they init and run the engine, connecting to 4 already open fds (stdin/stdout = network, argv[1] and argv[2] = local) - s6-tlsc is now a simple wrapper around s6-tlsc-io - s6-tlsd is now a simple wrapper around s6-tlsd-io - new binary: s6-ucspitlsd, which is also a wrapper around s6-tlsd-io, but differently: the parent execs the app which should be ucspi-tls-aware, the child waits for a command from the parent and execs into s6-tlsd-io if it receives it. --- src/conn-tools/deps-exe/s6-tlsc | 6 +- src/conn-tools/deps-exe/s6-tlsc-io | 5 ++ src/conn-tools/deps-exe/s6-tlsd | 6 +- src/conn-tools/deps-exe/s6-tlsd-io | 5 ++ src/conn-tools/deps-exe/s6-ucspitlsd | 3 + src/conn-tools/deps-lib/s6tls | 4 + src/conn-tools/s6-tlsc-io.c | 128 +++++++++++++++++++++++++++++++ src/conn-tools/s6-tlsc.c | 88 +++++++++++---------- src/conn-tools/s6-tlsd-io.c | 126 ++++++++++++++++++++++++++++++ src/conn-tools/s6-tlsd.c | 80 +++++++++---------- src/conn-tools/s6-ucspitlsd.c | 114 +++++++++++++++++++++++++++ src/conn-tools/s6tls-internal.h | 18 +++++ src/conn-tools/s6tls_drop.c | 24 ++++++ src/conn-tools/s6tls_exec_tlscio.c | 53 +++++++++++++ src/conn-tools/s6tls_exec_tlsdio.c | 49 ++++++++++++ src/conn-tools/s6tls_wait_and_exec_app.c | 43 +++++++++++ 16 files changed, 654 insertions(+), 98 deletions(-) create mode 100644 src/conn-tools/deps-exe/s6-tlsc-io create mode 100644 src/conn-tools/deps-exe/s6-tlsd-io create mode 100644 src/conn-tools/deps-exe/s6-ucspitlsd create mode 100644 src/conn-tools/deps-lib/s6tls create mode 100644 src/conn-tools/s6-tlsc-io.c create mode 100644 src/conn-tools/s6-tlsd-io.c create mode 100644 src/conn-tools/s6-ucspitlsd.c create mode 100644 src/conn-tools/s6tls-internal.h create mode 100644 src/conn-tools/s6tls_drop.c create mode 100644 src/conn-tools/s6tls_exec_tlscio.c create mode 100644 src/conn-tools/s6tls_exec_tlsdio.c create mode 100644 src/conn-tools/s6tls_wait_and_exec_app.c (limited to 'src/conn-tools') diff --git a/src/conn-tools/deps-exe/s6-tlsc b/src/conn-tools/deps-exe/s6-tlsc index 6e89a02..37405fd 100644 --- a/src/conn-tools/deps-exe/s6-tlsc +++ b/src/conn-tools/deps-exe/s6-tlsc @@ -1,6 +1,2 @@ -${LIBCRYPTOSUPPORT} +libs6tls.a.xyzzy -lskarnet -${CRYPTO_LIB} -${SOCKET_LIB} -${SPAWN_LIB} -${SYSCLOCK_LIB} diff --git a/src/conn-tools/deps-exe/s6-tlsc-io b/src/conn-tools/deps-exe/s6-tlsc-io new file mode 100644 index 0000000..3e2653d --- /dev/null +++ b/src/conn-tools/deps-exe/s6-tlsc-io @@ -0,0 +1,5 @@ +${LIBCRYPTOSUPPORT} +-lskarnet +${CRYPTO_LIB} +${SOCKET_LIB} +${SYSCLOCK_LIB} diff --git a/src/conn-tools/deps-exe/s6-tlsd b/src/conn-tools/deps-exe/s6-tlsd index 6e89a02..37405fd 100644 --- a/src/conn-tools/deps-exe/s6-tlsd +++ b/src/conn-tools/deps-exe/s6-tlsd @@ -1,6 +1,2 @@ -${LIBCRYPTOSUPPORT} +libs6tls.a.xyzzy -lskarnet -${CRYPTO_LIB} -${SOCKET_LIB} -${SPAWN_LIB} -${SYSCLOCK_LIB} diff --git a/src/conn-tools/deps-exe/s6-tlsd-io b/src/conn-tools/deps-exe/s6-tlsd-io new file mode 100644 index 0000000..3e2653d --- /dev/null +++ b/src/conn-tools/deps-exe/s6-tlsd-io @@ -0,0 +1,5 @@ +${LIBCRYPTOSUPPORT} +-lskarnet +${CRYPTO_LIB} +${SOCKET_LIB} +${SYSCLOCK_LIB} diff --git a/src/conn-tools/deps-exe/s6-ucspitlsd b/src/conn-tools/deps-exe/s6-ucspitlsd new file mode 100644 index 0000000..ac1b327 --- /dev/null +++ b/src/conn-tools/deps-exe/s6-ucspitlsd @@ -0,0 +1,3 @@ +libs6tls.a.xyzzy +-lskarnet +${SOCKET_LIB} diff --git a/src/conn-tools/deps-lib/s6tls b/src/conn-tools/deps-lib/s6tls new file mode 100644 index 0000000..ad78cfd --- /dev/null +++ b/src/conn-tools/deps-lib/s6tls @@ -0,0 +1,4 @@ +s6tls_drop.o +s6tls_exec_tlscio.o +s6tls_exec_tlsdio.o +s6tls_wait_and_exec_app.o diff --git a/src/conn-tools/s6-tlsc-io.c b/src/conn-tools/s6-tlsc-io.c new file mode 100644 index 0000000..79dd25d --- /dev/null +++ b/src/conn-tools/s6-tlsc-io.c @@ -0,0 +1,128 @@ +/* ISC license. */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define HANDSHAKE_BANNER "SSL_PROTOCOL=TLSv1\0" + +static inline void doit (int *, tain_t const *tto, uint32_t, uint32_t, unsigned int, char const *, unsigned int) gccattr_noreturn ; + +#ifdef S6_NETWORKING_USE_TLS + +#include + +static inline void doit (int *fds, tain_t const *tto, uint32_t preoptions, uint32_t options, unsigned int verbosity, char const *servername, unsigned int notif) +{ + struct tls *ctx = stls_client_init_and_handshake(fds + 2, preoptions, servername) ; + if (notif) + { + if (allwrite(notif, HANDSHAKE_BANNER, sizeof(HANDSHAKE_BANNER)) < sizeof(HANDSHAKE_BANNER)) + strerr_diefu1sys(111, "write post-handshake data") ; + fd_close(notif) ; + } + stls_run(ctx, fds, tto, options, verbosity) ; +} + +#else +#ifdef S6_NETWORKING_USE_BEARSSL + +#include + +#include + +static int handshake_cb_nop (br_ssl_engine_context *ctx, sbearssl_handshake_cb_context_t *cbarg) +{ + (void)ctx ; + (void)cbarg ; + return 1 ; +} + +static int handshake_cb_sendvars (br_ssl_engine_context *ctx, sbearssl_handshake_cb_context_t *cbarg) +{ + if (allwrite(cbarg->notif, HANDSHAKE_BANNER, sizeof(HANDSHAKE_BANNER)) < sizeof(HANDSHAKE_BANNER)) + return 0 ; + fd_close(cbarg->notif) ; + return 1 ; +} + +static inline void doit (int *fds, tain_t const *tto, uint32_t preoptions, uint32_t options, unsigned int verbosity, char const *servername, unsigned int notif) +{ + if (ndelay_on(fds[0]) < 0 || ndelay_on(fds[1]) < 0) + strerr_diefu1sys(111, "set local fds non-blocking") ; + if (!random_init()) strerr_diefu1sys(111, "initialize random device") ; + sbearssl_client_init_and_run(fds, tto, preoptions, options, verbosity, servername, notif ? &handshake_cb_sendvars : &handshake_cb_nop, notif) ; +} + +#else + +#error No SSL backend configured. + +#endif +#endif + + +#define USAGE "s6-tlsc-io [ -v verbosity ] [ -d notif ] [ -S | -s ] [ -Y | -y ] [ -K timeout ] [ -k servername ] fdr fdw" +#define dieusage() strerr_dieusage(100, USAGE) + +int main (int argc, char const *const *argv, char const *const *envp) +{ + char const *servername = 0 ; + tain_t tto ; + int fds[4] = { 0, 1, 0, 1 } ; + unsigned int verbosity = 1 ; + unsigned int notif = 0 ; + uint32_t preoptions = 0 ; + uint32_t options = 1 ; + + PROG = "s6-tlsc-io" ; + { + subgetopt_t l = SUBGETOPT_ZERO ; + unsigned int t = 0 ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "d:SsYyv:K:k:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'v' : if (!uint0_scan(l.arg, &verbosity)) dieusage() ; break ; + case 'd' : if (!uint0_scan(l.arg, ¬if)) dieusage() ; break ; + case 'S' : options &= ~1 ; break ; + case 's' : options |= 1 ; break ; + case 'Y' : preoptions &= ~1 ; break ; + case 'y' : preoptions |= 1 ; break ; + case 'K' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ; + case 'k' : servername = l.arg ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + if (t) tain_from_millisecs(&tto, t) ; else tto = tain_infinite_relative ; + } + if (argc < 2) dieusage() ; + { + unsigned int u ; + if (!uint0_scan(argv[0], &u)) dieusage() ; + fds[0] = u ; + if (!uint0_scan(argv[1], &u)) dieusage() ; + fds[1] = u ; + } + + if (ndelay_on(0) < 0 || ndelay_on(1) < 0) + strerr_diefu1sys(111, "set stdin/stdout non-blocking") ; + if (sig_ignore(SIGPIPE) < 0) strerr_diefu1sys(111, "ignore SIGPIPE") ; + tain_now_set_stopwatch_g() ; + doit(fds, &tto, preoptions, options, verbosity, servername, notif) ; +} diff --git a/src/conn-tools/s6-tlsc.c b/src/conn-tools/s6-tlsc.c index d9df81c..6431ccb 100644 --- a/src/conn-tools/s6-tlsc.c +++ b/src/conn-tools/s6-tlsc.c @@ -2,67 +2,63 @@ #include #include -#include +#include + +#include #include #include #include -#include #include #include -#include - -#ifdef S6_NETWORKING_USE_TLS - -#include -#define s6tlsc stls_s6tlsc - -#else -#ifdef S6_NETWORKING_USE_BEARSSL - -#include -#define s6tlsc sbearssl_s6tlsc - -#else - -#error No SSL backend configured. - -#endif -#endif +#include "s6tls-internal.h" #define USAGE "s6-tlsc [ -S | -s ] [ -Y | -y ] [ -v verbosity ] [ -K timeout ] [ -k servername ] [ -Z | -z ] [ -6 rfd ] [ -7 wfd ] prog..." #define dieusage() strerr_dieusage(100, USAGE) -int main (int argc, char const *const *argv, char const *const *envp) +static void child (int const [3][2], int, int, uint32_t, unsigned int, unsigned int, char const *) gccattr_noreturn ; +static void child (int const p[3][2], int fdr, int fdw, uint32_t options, unsigned int verbosity, unsigned int kimeout, char const *servername) +{ + int fds[3] = { p[0][0], p[1][1], p[2][1] } ; + PROG = "s6-tlsc (child)" ; + s6tls_drop() ; + close(p[2][0]) ; + close(p[0][1]) ; + close(p[1][0]) ; + if (fd_move(0, fdr) < 0 || fd_move(1, fdw) < 0) + strerr_diefu1sys(111, "move network fds to stdin/stdout") ; + s6tls_exec_tlscio(fds, options, verbosity, kimeout, servername) ; +} + +int main (int argc, char const *const *argv) { + int fds[2] = { 6, 7 } ; char const *servername = 0 ; - tain_t tto ; unsigned int verbosity = 1 ; - uid_t uid = 0 ; - gid_t gid = 0 ; - uint32_t preoptions = 2 ; - uint32_t options = 1 ; - int fds[2] = { 6, 7 } ; + unsigned int kimeout = 0 ; + int p[3][2] ; + uint32_t options = 0 ; + int cleanenv = 1 ; + pid_t pid ; PROG = "s6-tlsc" ; { subgetopt_t l = SUBGETOPT_ZERO ; - unsigned int t = 0 ; for (;;) { int opt = subgetopt_r(argc, argv, "SsYyv:K:k:Zz6:7:", &l) ; if (opt == -1) break ; switch (opt) { - case 'S' : options &= ~(uint32_t)1 ; break ; - case 's' : options |= 1 ; break ; - case 'Y' : preoptions &= ~(uint32_t)1 ; break ; - case 'y' : preoptions |= 1 ; break ; + case 'S' : options &= ~4 ; break ; + case 's' : options |= 4 ; break ; + case 'Y' : options &= ~1 ; break ; + case 'y' : options |= 1 ; break ; case 'v' : if (!uint0_scan(l.arg, &verbosity)) dieusage() ; break ; - case 'K' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ; + case 'K' : if (!uint0_scan(l.arg, &kimeout)) dieusage() ; break ; case 'k' : servername = l.arg ; break ; - case 'Z' : preoptions &= ~(uint32_t)2 ; break ; - case 'z' : preoptions |= 2 ; break ; + case 'Z' : cleanenv = 0 ; break ; + case 'z' : cleanenv = 1 ; break ; case '6' : { unsigned int fd ; @@ -81,18 +77,20 @@ int main (int argc, char const *const *argv, char const *const *envp) } } argc -= l.ind ; argv += l.ind ; - if (t) tain_from_millisecs(&tto, t) ; else tto = tain_infinite_relative ; } if (!argc) dieusage() ; - - if (!getuid()) + fd_sanitize() ; + if (fcntl(fds[0], F_GETFD) < 0 || fcntl(fds[1], F_GETFD) < 0) + strerr_diefu1sys(111, "check network fds") ; + if (pipe(p[0]) < 0 || pipe(p[1]) < 0 || pipe(p[2]) < 0) + strerr_diefu1sys(111, "pipe") ; + pid = fork() ; + switch (pid) { - char const *x = env_get2(envp, "TLS_UID") ; - if (x && !uid0_scan(x, &uid)) strerr_dieinvalid(100, "TLS_UID") ; - x = env_get2(envp, "TLS_GID") ; - if (x && !gid0_scan(x, &gid)) strerr_dieinvalid(100, "TLS_GID") ; + case -1 : strerr_diefu1sys(111, "fork") ; + case 0 : child(p, fds[0], fds[1], options, verbosity, kimeout, servername) ; + default : break ; } - tain_now_set_stopwatch_g() ; - return s6tlsc(argv, envp, &tto, preoptions, options, uid, gid, verbosity, servername, fds) ; + s6tls_wait_and_exec_app(argv, p, pid, fds[0], fds[1], cleanenv ? 1 : 0) ; } diff --git a/src/conn-tools/s6-tlsd-io.c b/src/conn-tools/s6-tlsd-io.c new file mode 100644 index 0000000..0b42b3b --- /dev/null +++ b/src/conn-tools/s6-tlsd-io.c @@ -0,0 +1,126 @@ +/* ISC license. */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define HANDSHAKE_BANNER "SSL_PROTOCOL=TLSv1\0" + +static inline void doit (int *, tain_t const *tto, uint32_t, uint32_t, unsigned int, unsigned int) gccattr_noreturn ; + +#ifdef S6_NETWORKING_USE_TLS + +#include + +static inline void doit (int *fds, tain_t const *tto, uint32_t preoptions, uint32_t options, unsigned int verbosity, unsigned int notif) +{ + struct tls *ctx = stls_server_init_and_handshake(fds + 2, preoptions) ; + if (notif) + { + if (allwrite(notif, HANDSHAKE_BANNER, sizeof(HANDSHAKE_BANNER)) < sizeof(HANDSHAKE_BANNER)) + strerr_diefu1sys(111, "write post-handshake data") ; + fd_close(notif) ; + } + stls_run(ctx, fds, tto, options, verbosity) ; +} + +#else +#ifdef S6_NETWORKING_USE_BEARSSL + +#include + +#include + +static int handshake_cb_nop (br_ssl_engine_context *ctx, sbearssl_handshake_cb_context_t *cbarg) +{ + (void)ctx ; + (void)cbarg ; + return 1 ; +} + +static int handshake_cb_sendvars (br_ssl_engine_context *ctx, sbearssl_handshake_cb_context_t *cbarg) +{ + if (allwrite(cbarg->notif, HANDSHAKE_BANNER, sizeof(HANDSHAKE_BANNER)) < sizeof(HANDSHAKE_BANNER)) + return 0 ; + fd_close(cbarg->notif) ; + return 1 ; +} + +static inline void doit (int *fds, tain_t const *tto, uint32_t preoptions, uint32_t options, unsigned int verbosity, unsigned int notif) +{ + if (ndelay_on(fds[0]) < 0 || ndelay_on(fds[1]) < 0) + strerr_diefu1sys(111, "set local fds non-blocking") ; + if (!random_init()) strerr_diefu1sys(111, "initialize random device") ; + sbearssl_server_init_and_run(fds, tto, preoptions, options, verbosity, notif ? &handshake_cb_sendvars : &handshake_cb_nop, notif) ; +} + +#else + +#error No SSL backend configured. + +#endif +#endif + + +#define USAGE "s6-tlsd-io [ -v verbosity ] [ -d notif ] [ -S | -s ] [ -Y | -y ] [ -K timeout ] fdr fdw" +#define dieusage() strerr_dieusage(100, USAGE) + +int main (int argc, char const *const *argv, char const *const *envp) +{ + tain_t tto ; + int fds[4] = { 0, 1, 0, 1 } ; + unsigned int verbosity = 1 ; + unsigned int notif = 0 ; + uint32_t preoptions = 0 ; + uint32_t options = 1 ; + + PROG = "s6-tlsd-io" ; + { + subgetopt_t l = SUBGETOPT_ZERO ; + unsigned int t = 0 ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "d:SsYyv:K:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'v' : if (!uint0_scan(l.arg, &verbosity)) dieusage() ; break ; + case 'd' : if (!uint0_scan(l.arg, ¬if)) dieusage() ; break ; + case 'S' : options &= ~1 ; break ; + case 's' : options |= 1 ; break ; + case 'Y' : preoptions |= 1 ; preoptions &= ~2 ; break ; + case 'y' : preoptions |= 3 ; break ; + case 'K' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + if (t) tain_from_millisecs(&tto, t) ; else tto = tain_infinite_relative ; + } + if (argc < 2) dieusage() ; + { + unsigned int u ; + if (!uint0_scan(argv[0], &u)) dieusage() ; + fds[0] = u ; + if (!uint0_scan(argv[1], &u)) dieusage() ; + fds[1] = u ; + } + + if (ndelay_on(0) < 0 || ndelay_on(1) < 0) + strerr_diefu1sys(111, "set stdin/stdout non-blocking") ; + if (sig_ignore(SIGPIPE) < 0) strerr_diefu1sys(111, "ignore SIGPIPE") ; + tain_now_set_stopwatch_g() ; + doit(fds, &tto, preoptions, options, verbosity, notif) ; +} diff --git a/src/conn-tools/s6-tlsd.c b/src/conn-tools/s6-tlsd.c index 923ac47..e26ba49 100644 --- a/src/conn-tools/s6-tlsd.c +++ b/src/conn-tools/s6-tlsd.c @@ -2,79 +2,73 @@ #include #include + +#include #include #include #include -#include #include #include -#include - -#ifdef S6_NETWORKING_USE_TLS - -#include -#define s6tlsd stls_s6tlsd - -#else -#ifdef S6_NETWORKING_USE_BEARSSL - -#include -#define s6tlsd sbearssl_s6tlsd - -#else - -#error No SSL backend configured. - -#endif -#endif +#include "s6tls-internal.h" #define USAGE "s6-tlsd [ -S | -s ] [ -Y | -y ] [ -v verbosity ] [ -K timeout ] [ -Z | -z ] prog..." #define dieusage() strerr_dieusage(100, USAGE) -int main (int argc, char const *const *argv, char const *const *envp) +static void child (int const [3][2], uint32_t, unsigned int, unsigned int) gccattr_noreturn ; +static void child (int const p[3][2], uint32_t options, unsigned int verbosity, unsigned int kimeout) +{ + int fds[3] = { p[0][0], p[1][1], p[2][1] } ; + PROG = "s6-tlsd (child)" ; + close(p[2][0]) ; + close(p[0][1]) ; + close(p[1][0]) ; + s6tls_drop() ; + s6tls_exec_tlsdio(fds, options, verbosity, kimeout) ; +} + +int main (int argc, char const *const *argv) { - tain_t tto ; unsigned int verbosity = 1 ; - uid_t uid = 0 ; - gid_t gid = 0 ; - uint32_t preoptions = 2 ; - uint32_t options = 1 ; + unsigned int kimeout = 0 ; + int p[3][2] ; + uint32_t options = 0 ; + int cleanenv = 1 ; + pid_t pid ; - PROG = "s6-tlsd" ; + PROG = "s6-tlsd (parent)" ; { subgetopt_t l = SUBGETOPT_ZERO ; - unsigned int t = 0 ; for (;;) { int opt = subgetopt_r(argc, argv, "SsYyv:K:Zz", &l) ; if (opt == -1) break ; switch (opt) { - case 'S' : options &= ~(uint32_t)1 ; break ; - case 's' : options |= 1 ; break ; - case 'Y' : preoptions |= 1 ; preoptions &= ~(uint32_t)4 ; break ; - case 'y' : preoptions |= 5 ; break ; + case 'S' : options |= 4 ; break ; + case 's' : options &= ~4 ; break ; + case 'Y' : options |= 1 ; options &= ~2 ; break ; + case 'y' : options |= 3 ; break ; case 'v' : if (!uint0_scan(l.arg, &verbosity)) dieusage() ; break ; - case 'K' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ; - case 'Z' : preoptions &= ~(uint32_t)2 ; break ; - case 'z' : preoptions |= 2 ; break ; + case 'K' : if (!uint0_scan(l.arg, &kimeout)) dieusage() ; break ; + case 'Z' : cleanenv = 0 ; break ; + case 'z' : cleanenv = 1 ; break ; default : dieusage() ; } } argc -= l.ind ; argv += l.ind ; - if (t) tain_from_millisecs(&tto, t) ; else tto = tain_infinite_relative ; } if (!argc) dieusage() ; - if (!getuid()) + if (pipe(p[0]) < 0 || pipe(p[1]) < 0 || pipe(p[2]) < 0) + strerr_diefu1sys(111, "pipe") ; + pid = fork() ; + switch (pid) { - char const *x = env_get2(envp, "TLS_UID") ; - if (x && !uid0_scan(x, &uid)) strerr_dieinvalid(100, "TLS_UID") ; - x = env_get2(envp, "TLS_GID") ; - if (x && !gid0_scan(x, &gid)) strerr_dieinvalid(100, "TLS_GID") ; + case -1 : strerr_diefu1sys(111, "fork") ; + case 0 : child(p, options, verbosity, kimeout) ; + default : break ; } - tain_now_set_stopwatch_g() ; - return s6tlsd(argv, envp, &tto, preoptions, options, uid, gid, verbosity) ; + s6tls_wait_and_exec_app(argv, p, pid, 0, 1, cleanenv ? 1 : 0) ; } diff --git a/src/conn-tools/s6-ucspitlsd.c b/src/conn-tools/s6-ucspitlsd.c new file mode 100644 index 0000000..ae2ca41 --- /dev/null +++ b/src/conn-tools/s6-ucspitlsd.c @@ -0,0 +1,114 @@ +/* ISC license. */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include "s6tls-internal.h" + +#define USAGE "s6-ucspitlsd [ -S | -s ] [ -Y | -y ] [ -v verbosity ] [ -K timeout ] [ -Z | -z ] prog..." +#define dieusage() strerr_dieusage(100, USAGE) + +static inline void child (int [3][2], uint32_t, unsigned int, unsigned int) gccattr_noreturn ; +static inline void child (int p[3][2], uint32_t options, unsigned int verbosity, unsigned int kimeout) +{ + int fds[3] = { p[0][0], p[1][1], p[2][1] } ; + ssize_t r ; + char c ; + PROG = "s6-ucspitlsd" ; + close(p[2][0]) ; + close(p[0][1]) ; + close(p[1][0]) ; + s6tls_drop() ; + r = read(p[2][1], &c, 1) ; + if (r < 0) strerr_diefu1sys(111, "read from control socket") ; + if (!r) _exit(0) ; + switch (c) + { + case 'y' : + close(p[2][1]) ; + p[2][1] = 0 ; /* we know 0 is open so it's a correct invalid value */ + case 'Y' : + fd_shutdown(p[2][1], 0) ; + break ; + default : + strerr_dief1x(100, "unrecognized command on control socket") ; + } + s6tls_exec_tlsdio(fds, options, verbosity, kimeout) ; +} + +int main (int argc, char const *const *argv, char const *const *envp) +{ + unsigned int kimeout = 0 ; + unsigned int verbosity = 1 ; + uint32_t options = 0 ; + int cleanenv = 1 ; + int p[3][2] ; + + PROG = "s6-ucspitlsd (parent)" ; + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "SsYyv:K:Zz", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'S' : options |= 4 ; break ; + case 's' : options &= ~4 ; break ; + case 'Y' : options |= 1 ; options &= ~2 ; break ; + case 'y' : options |= 3 ; break ; + case 'v' : if (!uint0_scan(l.arg, &verbosity)) dieusage() ; break ; + case 'K' : if (!uint0_scan(l.arg, &kimeout)) dieusage() ; break ; + case 'Z' : cleanenv = 0 ; break ; + case 'z' : cleanenv = 1 ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + } + if (!argc) dieusage() ; + + if (ipc_pair_b(p[2]) < 0) strerr_diefu1sys(111, "ipc_pair") ; + if (pipe(p[0]) < 0 || pipe(p[1]) < 0) strerr_diefu1sys(111, "pipe") ; + + switch (fork()) + { + case -1 : strerr_diefu1sys(111, "fork") ; + case 0 : child(p, options, verbosity, kimeout) ; + default : break ; + } + + { + size_t m = 0 ; + char modif[sizeof(s6tls_envvars) + 33 + 3 * UINT_FMT] ; + close(p[2][1]) ; + close(p[1][1]) ; + close(p[0][0]) ; + if (cleanenv) + { + memcpy(modif + m, s6tls_envvars, sizeof(s6tls_envvars)) ; + m += sizeof(s6tls_envvars) ; + } + memcpy(modif + m, "SSLCTLFD=", 9) ; m += 9 ; + m += uint_fmt(modif + m, p[2][0]) ; + modif[m++] = 0 ; + memcpy(modif + m, "SSLREADFD=", 10) ; m += 10 ; + m += uint_fmt(modif + m, p[1][0]) ; + modif[m++] = 0 ; + memcpy(modif + m, "SSLWRITEFD=", 11) ; m += 11 ; + m += uint_fmt(modif + m, p[0][1]) ; + modif[m++] = 0 ; + xpathexec_r(argv, envp, env_len(envp), modif, m) ; + } +} diff --git a/src/conn-tools/s6tls-internal.h b/src/conn-tools/s6tls-internal.h new file mode 100644 index 0000000..48df60f --- /dev/null +++ b/src/conn-tools/s6tls-internal.h @@ -0,0 +1,18 @@ +/* ISC license */ + +#ifndef S6TLS_INTERNAL_H +#define S6TLS_INTERNAL_H + +#include +#include + +#include + +#define s6tls_envvars "CADIR\0CAFILE\0KEYFILE\0CERTFILE\0TLS_UID\0TLS_GID" + +extern void s6tls_drop (void) ; +extern void s6tls_exec_tlscio (int const *, uint32_t, unsigned int, unsigned int, char const *) gccattr_noreturn ; +extern void s6tls_exec_tlsdio (int const *, uint32_t, unsigned int, unsigned int) gccattr_noreturn ; +extern void s6tls_wait_and_exec_app (char const *const *, int const [3][2], pid_t, int, int, uint32_t) gccattr_noreturn ; + +#endif diff --git a/src/conn-tools/s6tls_drop.c b/src/conn-tools/s6tls_drop.c new file mode 100644 index 0000000..6b6f67f --- /dev/null +++ b/src/conn-tools/s6tls_drop.c @@ -0,0 +1,24 @@ +/* ISC license. */ + +#include +#include + +#include +#include + +#include "s6tls-internal.h" + +void s6tls_drop (void) +{ + if (!getuid()) + { + uid_t uid ; + gid_t gid ; + char const *x = getenv("TLS_UID") ; + if (x && !uid0_scan(x, &uid)) strerr_dieinvalid(100, "TLS_UID") ; + x = getenv("TLS_GID") ; + if (x && !gid0_scan(x, &gid)) strerr_dieinvalid(100, "TLS_GID") ; + if (gid && setgid(gid) < 0) strerr_diefu1sys(111, "setgid") ; + if (uid && setuid(uid) < 0) strerr_diefu1sys(111, "setuid") ; + } +} diff --git a/src/conn-tools/s6tls_exec_tlscio.c b/src/conn-tools/s6tls_exec_tlscio.c new file mode 100644 index 0000000..ad00ecd --- /dev/null +++ b/src/conn-tools/s6tls_exec_tlscio.c @@ -0,0 +1,53 @@ +/* ISC license. */ + +#include +#include +#include + +#include +#include "s6tls-internal.h" + +void s6tls_exec_tlscio (int const *fds, uint32_t options, unsigned int verbosity, unsigned int kimeout, char const *servername) +{ + char const *newargv[15] ; + unsigned int m = 0 ; + char fmtv[UINT_FMT] ; + char fmtd[UINT_FMT] ; + char fmtk[UINT_FMT] ; + char fmtr[UINT_FMT] ; + char fmtw[UINT_FMT] ; + + newargv[m++] = S6_NETWORKING_BINPREFIX "s6-tlsc-io" ; + if (verbosity != 1) + { + newargv[m++] = "-v" ; + newargv[m++] = fmtv ; + fmtv[uint_fmt(fmtv, verbosity)] = 0 ; + } + if (fds[2]) + { + newargv[m++] = "-d" ; + newargv[m++] = fmtd ; + fmtd[uint_fmt(fmtd, fds[2])] = 0 ; + } + newargv[m++] = options & 4 ? "-S" : "-s" ; + newargv[m++] = options & 1 ? "-y" : "-Y" ; + if (kimeout) + { + newargv[m++] = "-K" ; + newargv[m++] = fmtk ; + fmtk[uint_fmt(fmtk, kimeout)] = 0 ; + } + if (servername) + { + newargv[m++] = "-k" ; + newargv[m++] = servername ; + } + newargv[m++] = "--" ; + newargv[m++] = fmtr ; + fmtr[uint_fmt(fmtr, fds[0])] = 0 ; + newargv[m++] = fmtw ; + fmtw[uint_fmt(fmtw, fds[1])] = 0 ; + newargv[m++] = 0 ; + xpathexec_run(newargv[0], newargv, (char const *const *)environ) ; +} diff --git a/src/conn-tools/s6tls_exec_tlsdio.c b/src/conn-tools/s6tls_exec_tlsdio.c new file mode 100644 index 0000000..b00adb2 --- /dev/null +++ b/src/conn-tools/s6tls_exec_tlsdio.c @@ -0,0 +1,49 @@ +/* ISC license. */ + +#include +#include +#include + +#include +#include "s6tls-internal.h" + +void s6tls_exec_tlsdio (int const *fds, uint32_t options, unsigned int verbosity, unsigned int kimeout) +{ + char const *newargv[13] ; + unsigned int m = 0 ; + char fmtv[UINT_FMT] ; + char fmtd[UINT_FMT] ; + char fmtk[UINT_FMT] ; + char fmtr[UINT_FMT] ; + char fmtw[UINT_FMT] ; + + newargv[m++] = S6_NETWORKING_BINPREFIX "s6-tlsd-io" ; + if (verbosity != 1) + { + newargv[m++] = "-v" ; + newargv[m++] = fmtv ; + fmtv[uint_fmt(fmtv, verbosity)] = 0 ; + } + if (fds[2]) + { + newargv[m++] = "-d" ; + newargv[m++] = fmtd ; + fmtd[uint_fmt(fmtd, fds[2])] = 0 ; + } + newargv[m++] = options & 4 ? "-S" : "-s" ; + if (options & 1) + newargv[m++] = options & 2 ? "-y" : "-Y" ; + if (kimeout) + { + newargv[m++] = "-K" ; + newargv[m++] = fmtk ; + fmtk[uint_fmt(fmtk, kimeout)] = 0 ; + } + newargv[m++] = "--" ; + newargv[m++] = fmtr ; + fmtr[uint_fmt(fmtr, fds[0])] = 0 ; + newargv[m++] = fmtw ; + fmtw[uint_fmt(fmtw, fds[1])] = 0 ; + newargv[m++] = 0 ; + xpathexec_run(newargv[0], newargv, (char const *const *)environ) ; +} diff --git a/src/conn-tools/s6tls_wait_and_exec_app.c b/src/conn-tools/s6tls_wait_and_exec_app.c new file mode 100644 index 0000000..20828f6 --- /dev/null +++ b/src/conn-tools/s6tls_wait_and_exec_app.c @@ -0,0 +1,43 @@ +/* ISC license. */ + +#include +#include + +#include +#include +#include +#include + +#include "s6tls-internal.h" + +#define MAXENVSIZE 2048 + +void s6tls_wait_and_exec_app (char const *const *argv, int const p[3][2], pid_t pid, int fdr, int fdw, uint32_t options) +{ + char buf[sizeof(s6tls_envvars) + MAXENVSIZE] ; + size_t m = 0 ; + ssize_t r ; + close(p[2][1]) ; + close(p[1][1]) ; + close(p[0][0]) ; + if (fd_move(fdr, p[1][0]) < 0 || fd_move(fdw, p[0][1]) < 0) + strerr_diefu1sys(111, "move file descriptors") ; + if (options & 1) + { + memcpy(buf + m, s6tls_envvars, sizeof(s6tls_envvars)) ; + m += sizeof(s6tls_envvars) ; + } + r = read(p[2][0], buf + m, MAXENVSIZE) ; + if (r < 0) strerr_diefu1sys(111, "read from handshake notification pipe") ; + if (!r) + { + int wstat ; + if (wait_pid(pid, &wstat) < 0) + strerr_diefu1sys(111, "wait") ; + _exit(wait_estatus(wstat)) ; + } + if (r >= MAXENVSIZE) + strerr_dief1x(100, "SSL data too large") ; + m += r - 1 ; + xpathexec_r(argv, (char const *const *)environ, env_len((char const *const *)environ), buf, m) ; +} -- cgit v1.2.3